1 From 7ab5d6bd1f8b018c73341f50a395405dee6873d8 Mon Sep 17 00:00:00 2001
2 From: rpm-build <rpm-build>
3 Date: Wed, 15 Mar 2017 14:26:04 +0100
4 Subject: [PATCH] Coverity fixes
8 src/dhcp-common.c | 2 +-
12 src/inotify.c | 9 ++++++---
14 src/network.c | 8 ++++----
15 src/option.c | 16 +++++++++++-----
18 11 files changed, 32 insertions(+), 21 deletions(-)
20 diff --git a/src/dbus.c b/src/dbus.c
21 index 2e1a48e..f27ec3e 100644
24 @@ -550,7 +550,7 @@ static DBusMessage *dbus_add_lease(DBusMessage* message)
25 "Invalid IP address '%s'", ipaddr);
27 hw_len = parse_hex((char*)hwaddr, dhcp_chaddr, DHCP_CHADDR_MAX, NULL, &hw_type);
28 - if (hw_type == 0 && hw_len != 0)
29 + if (hw_type == 0 && hw_len > 0)
30 hw_type = ARPHRD_ETHER;
32 lease_set_hwaddr(lease, dhcp_chaddr, clid, hw_len, hw_type,
33 diff --git a/src/dhcp-common.c b/src/dhcp-common.c
34 index 08528e8..ebf06b6 100644
35 --- a/src/dhcp-common.c
36 +++ b/src/dhcp-common.c
37 @@ -487,7 +487,7 @@ void bindtodevice(char *device, int fd)
41 - strcpy(ifr.ifr_name, device);
42 + strncpy(ifr.ifr_name, device, IF_NAMESIZE-1);
43 /* only allowed by root. */
44 if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) == -1 &&
46 diff --git a/src/dhcp.c b/src/dhcp.c
47 index 10f1fb9..37bb71e 100644
50 @@ -246,7 +246,7 @@ void dhcp_packet(time_t now, int pxe_fd)
54 - strncpy(ifr.ifr_name, bridge->iface, IF_NAMESIZE);
55 + strncpy(ifr.ifr_name, bridge->iface, IF_NAMESIZE-1);
59 @@ -270,7 +270,7 @@ void dhcp_packet(time_t now, int pxe_fd)
62 #ifdef HAVE_LINUX_NETWORK
63 - strncpy(arp_req.arp_dev, ifr.ifr_name, 16);
64 + strncpy(arp_req.arp_dev, ifr.ifr_name, IF_NAMESIZE-1);
68 diff --git a/src/dnsmasq.h b/src/dnsmasq.h
69 index dfd15aa..1179492 100644
72 @@ -180,7 +180,7 @@ struct event_desc {
73 #define EC_INIT_OFFSET 10
75 /* Trust the compiler dead-code eliminator.... */
76 -#define option_bool(x) (((x) < 32) ? daemon->options & (1u << (x)) : daemon->options2 & (1u << ((x) - 32)))
77 +#define option_bool(x) (((x) < 32) ? daemon->options & (1u << ((x)&0x1F)) : daemon->options2 & (1u << ((x) - 32)))
79 #define OPT_BOGUSPRIV 0
81 diff --git a/src/edns0.c b/src/edns0.c
82 index c7a101e..d2b514b 100644
85 @@ -263,6 +263,8 @@ static size_t add_dns_client(struct dns_header *header, size_t plen, unsigned ch
92 return add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_NOMDEVICEID, (unsigned char *)encode, strlen(encode), 0, replace);
94 diff --git a/src/inotify.c b/src/inotify.c
95 index 603ce9d..fcc0d97 100644
98 @@ -224,17 +224,20 @@ int inotify_check(time_t now)
103 + inotify_buffer[rc] = '\0';
105 for (p = inotify_buffer; rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event); p += sizeof(struct inotify_event) + in->len)
107 in = (struct inotify_event*)p;
110 for (res = daemon->resolv_files; res; res = res->next)
111 - if (res->wd == in->wd && in->len != 0 && strcmp(res->file, in->name) == 0)
112 + if (res->wd == in->wd && in->len != 0 && strncmp(res->file, in->name, NAME_MAX) == 0)
115 /* ignore emacs backups and dotfiles */
116 - if (in->len == 0 ||
117 + if (in->len == 0 ||
118 + in->len > NAME_MAX+1 ||
119 in->name[in->len - 1] == '~' ||
120 (in->name[0] == '#' && in->name[in->len - 1] == '#') ||
122 diff --git a/src/lease.c b/src/lease.c
123 index 20cac90..9ad106d 100644
126 @@ -827,9 +827,9 @@ void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
128 if (hw_len != lease->hwaddr_len ||
129 hw_type != lease->hwaddr_type ||
130 - (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
131 + (hw_len > 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
135 memcpy(lease->hwaddr, hwaddr, hw_len);
136 lease->hwaddr_len = hw_len;
137 lease->hwaddr_type = hw_type;
138 diff --git a/src/network.c b/src/network.c
139 index 6119039..fcd9d8d 100644
142 @@ -188,7 +188,7 @@ int loopback_exception(int fd, int family, struct all_addr *addr, char *name)
146 - strncpy(ifr.ifr_name, name, IF_NAMESIZE);
147 + strncpy(ifr.ifr_name, name, IF_NAMESIZE-1);
148 if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1 &&
149 ifr.ifr_flags & IFF_LOOPBACK)
151 @@ -1206,7 +1206,7 @@ int local_bind(int fd, union mysockaddr *addr, char *intname, int is_tcp)
154 #if defined(SO_BINDTODEVICE)
155 - if (intname[0] != 0 &&
156 + if (intname && intname[0] != 0 &&
157 setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1)
160 @@ -1245,7 +1245,7 @@ static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname)
161 /* may have a suitable one already */
162 for (sfd = daemon->sfds; sfd; sfd = sfd->next )
163 if (sockaddr_isequal(&sfd->source_addr, addr) &&
164 - strcmp(intname, sfd->interface) == 0 &&
165 + intname && strcmp(intname, sfd->interface) == 0 &&
166 ifindex == sfd->ifindex)
169 @@ -1437,7 +1437,7 @@ void add_update_server(int flags,
170 serv->flags |= SERV_HAS_DOMAIN;
173 - strcpy(serv->interface, interface);
174 + strncpy(serv->interface, interface, sizeof(serv->interface)-1);
178 diff --git a/src/option.c b/src/option.c
179 index 5503b79..eb78b1a 100644
182 @@ -3929,13 +3929,15 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
184 case LOPT_HOST_REC: /* --host-record */
186 - struct host_record *new = opt_malloc(sizeof(struct host_record));
187 - memset(new, 0, sizeof(struct host_record));
189 + struct host_record *new;
191 if (!arg || !(comma = split(arg)))
192 ret_err(_("Bad host-record"));
194 + new = opt_malloc(sizeof(struct host_record));
195 + memset(new, 0, sizeof(struct host_record));
200 struct all_addr addr;
201 @@ -3956,10 +3958,11 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
204 char *canon = canonicalise(arg, &nomem);
205 - struct name_list *nl = opt_malloc(sizeof(struct name_list));
206 + struct name_list *nl;
208 ret_err(_("Bad name in host-record"));
210 + nl = opt_malloc(sizeof(struct name_list));
212 /* keep order, so that PTR record goes to first name */
214 @@ -4023,7 +4026,10 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
215 !atoi_check8(algo, &new->algo) ||
216 !atoi_check8(digest, &new->digest_type) ||
217 !(new->name = canonicalise_opt(arg)))
218 - ret_err(_("bad trust anchor"));
221 + ret_err(_("bad trust anchor"));
224 /* Upper bound on length */
225 len = (2*strlen(keyhex))+1;
226 diff --git a/src/tftp.c b/src/tftp.c
227 index 5e4a32a..bd8c622 100644
230 @@ -234,7 +234,7 @@ void tftp_request(struct listener *listen, time_t now)
234 - strncpy(ifr.ifr_name, name, IF_NAMESIZE);
235 + strncpy(ifr.ifr_name, name, IF_NAMESIZE-1);
236 if (ioctl(listen->tftpfd, SIOCGIFMTU, &ifr) != -1)
239 diff --git a/src/util.c b/src/util.c
240 index 93b24f5..1a9f228 100644
243 @@ -491,7 +491,7 @@ int parse_hex(char *in, unsigned char *out, int maxlen,
244 int j, bytes = (1 + (r - in))/2;
245 for (j = 0; j < bytes; j++)