From: Roy Marples Date: Mon, 28 Jan 2008 17:47:54 +0000 (+0000) Subject: Use sizeof with the variable, not the declaration more. X-Git-Tag: v3.2.3~52 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7fcc9e648bfa464c142a6a28e2431b19ac744cdd;p=thirdparty%2Fdhcpcd.git Use sizeof with the variable, not the declaration more. --- diff --git a/arp.c b/arp.c index 01ce1e78..542fe07a 100644 --- a/arp.c +++ b/arp.c @@ -77,7 +77,7 @@ static int send_arp (const interface_t *iface, int op, struct in_addr sip, const unsigned char *taddr, struct in_addr tip) { struct arphdr *arp; - size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (struct in_addr)); + size_t arpsize = arphdr_len2 (iface->hwlen, sizeof (sip)); caddr_t tha; int retval; @@ -85,7 +85,7 @@ static int send_arp (const interface_t *iface, int op, struct in_addr sip, arp->ar_hrd = htons (iface->family); arp->ar_pro = htons (ETHERTYPE_IP); arp->ar_hln = iface->hwlen; - arp->ar_pln = sizeof (struct in_addr); + arp->ar_pln = sizeof (sip); arp->ar_op = htons (op); memcpy (ar_sha (arp), iface->hwaddr, (size_t) arp->ar_hln); memcpy (ar_spa (arp), &sip, (size_t) arp->ar_pln); @@ -134,7 +134,7 @@ int arp_claim (interface_t *iface, struct in_addr address) if (! open_socket (iface, ETHERTYPE_ARP)) return (-1); - memset (&null_address, 0, sizeof (struct in_addr)); + memset (&null_address, 0, sizeof (null_address)); buffer = xmalloc (iface->buffer_length); reply = xmalloc (iface->buffer_length); @@ -247,7 +247,7 @@ int arp_claim (interface_t *iface, struct in_addr address) /* Protocol must be IP. */ if (reply->ar_pro != htons (ETHERTYPE_IP)) continue; - if (reply->ar_pln != sizeof (struct in_addr)) + if (reply->ar_pln != sizeof (address)) continue; if ((unsigned) bytes < sizeof (reply) + 2 * (4 + reply->ar_hln)) diff --git a/client.c b/client.c index 061a183c..6b0c22fe 100644 --- a/client.c +++ b/client.c @@ -921,7 +921,7 @@ static int handle_packet (state_t *state, const options_t *options) The benefit is that if we get >1 DHCP packet in our buffer and the first one fails for any reason, we can use the next. */ - memset (&message, 0, sizeof (struct dhcpmessage_t)); + memset (&message, 0, sizeof (message)); new_dhcp = xmalloc (sizeof (*new_dhcp)); do { diff --git a/configure.c b/configure.c index 476b38b6..47a0e263 100644 --- a/configure.c +++ b/configure.c @@ -423,7 +423,7 @@ static char *lookuphostname (char *hostname, const dhcp_t *dhcp, logger (LOG_DEBUG, "Looking up hostname via DNS"); addr = xmalloc (sizeof (char) * NI_MAXHOST); - salen = sizeof (struct sockaddr); + salen = sizeof (su.sa); memset (&su.sa, 0, salen); su.sin.sin_family = AF_INET; memcpy (&su.sin.sin_addr, &dhcp->address, sizeof (su.sin.sin_addr)); diff --git a/dhcp.c b/dhcp.c index 42b99154..0f49c49b 100644 --- a/dhcp.c +++ b/dhcp.c @@ -385,8 +385,8 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp, logger (LOG_DEBUG, "sending %s with xid 0x%x", dhcp_message (type), xid); retval = send_packet (iface, ETHERTYPE_IP, (unsigned char *) packet, - message_length + sizeof (struct ip) + - sizeof (struct udphdr)); + message_length + + sizeof (packet->ip) + sizeof (packet->udp)); free (packet); return (retval); } @@ -535,10 +535,9 @@ static bool dhcp_add_address (address_t **address, for (i = 0; i < length; i += 4) { if (*address == NULL) { - *address = xzalloc (sizeof (address_t)); - p = *address; + p = *address = xzalloc (sizeof (*p)); } else { - p->next = xzalloc (sizeof (address_t)); + p->next = xzalloc (sizeof (*p)); p = p->next; } diff --git a/info.c b/info.c index 1c8302ee..e24d87bc 100644 --- a/info.c +++ b/info.c @@ -322,7 +322,7 @@ static bool parse_addresses (address_t **address, char *value, const char *var) bool retval = true; while ((token = strsep (&p, " "))) { - address_t *a = xzalloc (sizeof (address_t)); + address_t *a = xzalloc (sizeof (*a)); if (inet_aton (token, &a->address) == 0) { logger (LOG_ERR, "%s: invalid address `%s'", var, token); diff --git a/interface.c b/interface.c index b1e6542a..1aaf48ae 100644 --- a/interface.c +++ b/interface.c @@ -240,7 +240,7 @@ static int _do_interface (const char *ifname, ifr = ifreqs.ifr; #ifdef __linux__ - p += sizeof (struct ifreq); + p += sizeof (*ifr); #else p += sizeof (ifr->ifr_name) + MAX (ifr->ifr_addr.sa_len, sizeof (struct sockaddr)); @@ -583,12 +583,6 @@ static int do_route (const char *ifname, int change, int del) { int s; - struct rtm - { - struct rt_msghdr hdr; - char buffer[sizeof (struct sockaddr_storage) * 3]; - } rtm; - char *bp = rtm.buffer; static int seq; union sockunion { struct sockaddr sa; @@ -599,7 +593,12 @@ static int do_route (const char *ifname, struct sockaddr_dl sdl; struct sockaddr_storage ss; } su; - + struct rtm + { + struct rt_msghdr hdr; + char buffer[sizeof (su) * 3]; + } rtm; + char *bp = rtm.buffer; size_t l; if (! ifname) @@ -623,10 +622,10 @@ static int do_route (const char *ifname, rtm.hdr.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; #define ADDADDR(_addr) \ - memset (&su, 0, sizeof (struct sockaddr_storage)); \ + memset (&su, 0, sizeof (su)); \ su.sin.sin_family = AF_INET; \ - su.sin.sin_len = sizeof (struct sockaddr_in); \ - memcpy (&su.sin.sin_addr, &_addr, sizeof (struct in_addr)); \ + su.sin.sin_len = sizeof (su.sin.sin_addr); \ + memcpy (&su.sin.sin_addr, &_addr, sizeof (su.sin.sin_addr)); \ l = SA_SIZE (&(su.sa)); \ memcpy (bp, &(su), l); \ bp += l; @@ -645,8 +644,8 @@ static int do_route (const char *ifname, hwaddr = xmalloc (sizeof (unsigned char) * HWADDR_LEN); _do_interface (ifname, hwaddr, &hwlen, NULL, false, false); - memset (&su, 0, sizeof (struct sockaddr_storage)); - su.sdl.sdl_len = sizeof (struct sockaddr_dl); + memset (&su, 0, sizeof (su)); + su.sdl.sdl_len = sizeof (su.sdl); su.sdl.sdl_family = AF_LINK; su.sdl.sdl_nlen = strlen (ifname); memcpy (&su.sdl.sdl_data, ifname, (size_t) su.sdl.sdl_nlen); @@ -791,7 +790,7 @@ static int send_netlink(struct nlmsghdr *hdr) goto eexit; } - if ((unsigned) l < sizeof (struct nlmsgerr)) { + if ((unsigned) l < sizeof (*err)) { logger (LOG_ERR, "netlink: error truncated"); goto eexit; } diff --git a/socket.c b/socket.c index 16daceec..a3af6d24 100644 --- a/socket.c +++ b/socket.c @@ -425,8 +425,8 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data, } pay; pay.buffer = payload; len = ntohs (pay.packet->ip.ip_len) - - sizeof (struct ip) - - sizeof (struct udphdr); + sizeof (pay.packet->ip) - + sizeof (pay.packet->udp); memcpy (data, &pay.packet->dhcp, len); have_data = true; } @@ -595,7 +595,9 @@ ssize_t get_packet (const interface_t *iface, unsigned char *data, return (bytes); } - if ((unsigned) bytes < (sizeof (struct ip) + sizeof (struct udphdr))) { + if ((unsigned) bytes < (sizeof (pay.packet->ip) + + sizeof (pay.packet->udp))) + { logger (LOG_DEBUG, "message too short, ignoring"); return (-1); }