e87dd3
From 7ab5d6bd1f8b018c73341f50a395405dee6873d8 Mon Sep 17 00:00:00 2001
e87dd3
From: rpm-build <rpm-build>
e87dd3
Date: Wed, 15 Mar 2017 14:26:04 +0100
e87dd3
Subject: [PATCH] Coverity fixes
e87dd3
e87dd3
---
e87dd3
 src/dbus.c        |  2 +-
e87dd3
 src/dhcp-common.c |  2 +-
e87dd3
 src/dhcp.c        |  4 ++--
e87dd3
 src/dnsmasq.h     |  2 +-
e87dd3
 src/edns0.c       |  2 ++
e87dd3
 src/inotify.c     |  9 ++++++---
e87dd3
 src/lease.c       |  4 ++--
e87dd3
 src/network.c     |  8 ++++----
e87dd3
 src/option.c      | 16 +++++++++++-----
e87dd3
 src/tftp.c        |  2 +-
e87dd3
 src/util.c        |  2 +-
e87dd3
 11 files changed, 32 insertions(+), 21 deletions(-)
e87dd3
e87dd3
diff --git a/src/dbus.c b/src/dbus.c
e87dd3
index 2e1a48e..f27ec3e 100644
e87dd3
--- a/src/dbus.c
e87dd3
+++ b/src/dbus.c
e87dd3
@@ -550,7 +550,7 @@ static DBusMessage *dbus_add_lease(DBusMessage* message)
e87dd3
 					 "Invalid IP address '%s'", ipaddr);
e87dd3
    
e87dd3
   hw_len = parse_hex((char*)hwaddr, dhcp_chaddr, DHCP_CHADDR_MAX, NULL, &hw_type);
e87dd3
-  if (hw_type == 0 && hw_len != 0)
e87dd3
+  if (hw_type == 0 && hw_len > 0)
e87dd3
     hw_type = ARPHRD_ETHER;
e87dd3
   
e87dd3
   lease_set_hwaddr(lease, dhcp_chaddr, clid, hw_len, hw_type,
e87dd3
diff --git a/src/dhcp-common.c b/src/dhcp-common.c
e87dd3
index 08528e8..ebf06b6 100644
e87dd3
--- a/src/dhcp-common.c
e87dd3
+++ b/src/dhcp-common.c
e87dd3
@@ -487,7 +487,7 @@ void  bindtodevice(char *device, int fd)
e87dd3
 {
e87dd3
   struct ifreq ifr;
e87dd3
   
e87dd3
-  strcpy(ifr.ifr_name, device);
e87dd3
+  strncpy(ifr.ifr_name, device, IF_NAMESIZE-1);
e87dd3
   /* only allowed by root. */
e87dd3
   if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) == -1 &&
e87dd3
       errno != EPERM)
e87dd3
diff --git a/src/dhcp.c b/src/dhcp.c
e87dd3
index 10f1fb9..37bb71e 100644
e87dd3
--- a/src/dhcp.c
e87dd3
+++ b/src/dhcp.c
e87dd3
@@ -246,7 +246,7 @@ void dhcp_packet(time_t now, int pxe_fd)
e87dd3
 	      }
e87dd3
 	    else 
e87dd3
 	      {
e87dd3
-		strncpy(ifr.ifr_name,  bridge->iface, IF_NAMESIZE);
e87dd3
+		strncpy(ifr.ifr_name,  bridge->iface, IF_NAMESIZE-1);
e87dd3
 		break;
e87dd3
 	      }
e87dd3
 	  }
e87dd3
@@ -270,7 +270,7 @@ void dhcp_packet(time_t now, int pxe_fd)
e87dd3
       is_relay_reply = 1; 
e87dd3
       iov.iov_len = sz;
e87dd3
 #ifdef HAVE_LINUX_NETWORK
e87dd3
-      strncpy(arp_req.arp_dev, ifr.ifr_name, 16);
e87dd3
+      strncpy(arp_req.arp_dev, ifr.ifr_name, IF_NAMESIZE-1);
e87dd3
 #endif 
e87dd3
     }
e87dd3
   else
e87dd3
diff --git a/src/dnsmasq.h b/src/dnsmasq.h
e87dd3
index dfd15aa..1179492 100644
e87dd3
--- a/src/dnsmasq.h
e87dd3
+++ b/src/dnsmasq.h
e87dd3
@@ -180,7 +180,7 @@ struct event_desc {
e87dd3
 #define EC_INIT_OFFSET 10
e87dd3
 
e87dd3
 /* Trust the compiler dead-code eliminator.... */
e87dd3
-#define option_bool(x) (((x) < 32) ? daemon->options & (1u << (x)) : daemon->options2 & (1u << ((x) - 32)))
e87dd3
+#define option_bool(x) (((x) < 32) ? daemon->options & (1u << ((x)&0x1F)) : daemon->options2 & (1u << ((x) - 32)))
e87dd3
 
e87dd3
 #define OPT_BOGUSPRIV      0
e87dd3
 #define OPT_FILTER         1
e87dd3
diff --git a/src/edns0.c b/src/edns0.c
e87dd3
index c7a101e..d2b514b 100644
e87dd3
--- a/src/edns0.c
e87dd3
+++ b/src/edns0.c
e87dd3
@@ -263,6 +263,8 @@ static size_t add_dns_client(struct dns_header *header, size_t plen, unsigned ch
e87dd3
 	  encode[8] = 0;
e87dd3
 	}
e87dd3
     }
e87dd3
+  else
e87dd3
+    encode[0] = '\0';
e87dd3
 
e87dd3
   return add_pseudoheader(header, plen, limit, PACKETSZ, EDNS0_OPTION_NOMDEVICEID, (unsigned char *)encode, strlen(encode), 0, replace); 
e87dd3
 }
e87dd3
diff --git a/src/inotify.c b/src/inotify.c
e87dd3
index 603ce9d..fcc0d97 100644
e87dd3
--- a/src/inotify.c
e87dd3
+++ b/src/inotify.c
e87dd3
@@ -224,17 +224,20 @@ int inotify_check(time_t now)
e87dd3
       
e87dd3
       if (rc <= 0)
e87dd3
 	break;
e87dd3
+      else
e87dd3
+        inotify_buffer[rc] = '\0';
e87dd3
       
e87dd3
       for (p = inotify_buffer; rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event); p += sizeof(struct inotify_event) + in->len) 
e87dd3
 	{
e87dd3
 	  in = (struct inotify_event*)p;
e87dd3
-	  
e87dd3
+
e87dd3
 	  for (res = daemon->resolv_files; res; res = res->next)
e87dd3
-	    if (res->wd == in->wd && in->len != 0 && strcmp(res->file, in->name) == 0)
e87dd3
+	    if (res->wd == in->wd && in->len != 0 && strncmp(res->file, in->name, NAME_MAX) == 0)
e87dd3
 	      hit = 1;
e87dd3
 
e87dd3
 	  /* ignore emacs backups and dotfiles */
e87dd3
-	  if (in->len == 0 || 
e87dd3
+	  if (in->len == 0 ||
e87dd3
+	      in->len > NAME_MAX+1 ||
e87dd3
 	      in->name[in->len - 1] == '~' ||
e87dd3
 	      (in->name[0] == '#' && in->name[in->len - 1] == '#') ||
e87dd3
 	      in->name[0] == '.')
e87dd3
diff --git a/src/lease.c b/src/lease.c
e87dd3
index 20cac90..9ad106d 100644
e87dd3
--- a/src/lease.c
e87dd3
+++ b/src/lease.c
e87dd3
@@ -827,9 +827,9 @@ void lease_set_hwaddr(struct dhcp_lease *lease, const unsigned char *hwaddr,
e87dd3
 
e87dd3
   if (hw_len != lease->hwaddr_len ||
e87dd3
       hw_type != lease->hwaddr_type || 
e87dd3
-      (hw_len != 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
e87dd3
+      (hw_len > 0 && memcmp(lease->hwaddr, hwaddr, hw_len) != 0))
e87dd3
     {
e87dd3
-      if (hw_len != 0)
e87dd3
+      if (hw_len > 0)
e87dd3
 	memcpy(lease->hwaddr, hwaddr, hw_len);
e87dd3
       lease->hwaddr_len = hw_len;
e87dd3
       lease->hwaddr_type = hw_type;
e87dd3
diff --git a/src/network.c b/src/network.c
e87dd3
index 6119039..fcd9d8d 100644
e87dd3
--- a/src/network.c
e87dd3
+++ b/src/network.c
e87dd3
@@ -188,7 +188,7 @@ int loopback_exception(int fd, int family, struct all_addr *addr, char *name)
e87dd3
   struct ifreq ifr;
e87dd3
   struct irec *iface;
e87dd3
 
e87dd3
-  strncpy(ifr.ifr_name, name, IF_NAMESIZE);
e87dd3
+  strncpy(ifr.ifr_name, name, IF_NAMESIZE-1);
e87dd3
   if (ioctl(fd, SIOCGIFFLAGS, &ifr) != -1 &&
e87dd3
       ifr.ifr_flags & IFF_LOOPBACK)
e87dd3
     {
e87dd3
@@ -1206,7 +1206,7 @@ int local_bind(int fd, union mysockaddr *addr, char *intname, int is_tcp)
e87dd3
     return 0;
e87dd3
     
e87dd3
 #if defined(SO_BINDTODEVICE)
e87dd3
-  if (intname[0] != 0 &&
e87dd3
+  if (intname && intname[0] != 0 &&
e87dd3
       setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, intname, IF_NAMESIZE) == -1)
e87dd3
     return 0;
e87dd3
 #endif
e87dd3
@@ -1245,7 +1245,7 @@ static struct serverfd *allocate_sfd(union mysockaddr *addr, char *intname)
e87dd3
   /* may have a suitable one already */
e87dd3
   for (sfd = daemon->sfds; sfd; sfd = sfd->next )
e87dd3
     if (sockaddr_isequal(&sfd->source_addr, addr) &&
e87dd3
-	strcmp(intname, sfd->interface) == 0 &&
e87dd3
+	intname && strcmp(intname, sfd->interface) == 0 &&
e87dd3
 	ifindex == sfd->ifindex) 
e87dd3
       return sfd;
e87dd3
   
e87dd3
@@ -1437,7 +1437,7 @@ void add_update_server(int flags,
e87dd3
 	serv->flags |= SERV_HAS_DOMAIN;
e87dd3
       
e87dd3
       if (interface)
e87dd3
-	strcpy(serv->interface, interface);      
e87dd3
+	strncpy(serv->interface, interface, sizeof(serv->interface)-1);
e87dd3
       if (addr)
e87dd3
 	serv->addr = *addr;
e87dd3
       if (source_addr)
e87dd3
diff --git a/src/option.c b/src/option.c
e87dd3
index 5503b79..eb78b1a 100644
e87dd3
--- a/src/option.c
e87dd3
+++ b/src/option.c
e87dd3
@@ -3929,13 +3929,15 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
e87dd3
       
e87dd3
     case LOPT_HOST_REC: /* --host-record */
e87dd3
       {
e87dd3
-	struct host_record *new = opt_malloc(sizeof(struct host_record));
e87dd3
-	memset(new, 0, sizeof(struct host_record));
e87dd3
-	new->ttl = -1;
e87dd3
+	struct host_record *new;
e87dd3
 
e87dd3
 	if (!arg || !(comma = split(arg)))
e87dd3
 	  ret_err(_("Bad host-record"));
e87dd3
 	
e87dd3
+	new = opt_malloc(sizeof(struct host_record));
e87dd3
+	memset(new, 0, sizeof(struct host_record));
e87dd3
+	new->ttl = -1;
e87dd3
+
e87dd3
 	while (arg)
e87dd3
 	  {
e87dd3
 	    struct all_addr addr;
e87dd3
@@ -3956,10 +3958,11 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
e87dd3
 	      {
e87dd3
 		int nomem;
e87dd3
 		char *canon = canonicalise(arg, &nomem);
e87dd3
-		struct name_list *nl = opt_malloc(sizeof(struct name_list));
e87dd3
+		struct name_list *nl;
e87dd3
 		if (!canon)
e87dd3
 		  ret_err(_("Bad name in host-record"));
e87dd3
 
e87dd3
+		nl = opt_malloc(sizeof(struct name_list));
e87dd3
 		nl->name = canon;
e87dd3
 		/* keep order, so that PTR record goes to first name */
e87dd3
 		nl->next = NULL;
e87dd3
@@ -4023,7 +4026,10 @@ static int one_opt(int option, char *arg, char *errstr, char *gen_err, int comma
e87dd3
 	    !atoi_check8(algo, &new->algo) ||
e87dd3
 	    !atoi_check8(digest, &new->digest_type) ||
e87dd3
 	    !(new->name = canonicalise_opt(arg)))
e87dd3
-	  ret_err(_("bad trust anchor"));
e87dd3
+	  {
e87dd3
+	    free(new);
e87dd3
+	    ret_err(_("bad trust anchor"));
e87dd3
+	  }
e87dd3
 	    
e87dd3
 	/* Upper bound on length */
e87dd3
 	len = (2*strlen(keyhex))+1;
e87dd3
diff --git a/src/tftp.c b/src/tftp.c
e87dd3
index 5e4a32a..bd8c622 100644
e87dd3
--- a/src/tftp.c
e87dd3
+++ b/src/tftp.c
e87dd3
@@ -234,7 +234,7 @@ void tftp_request(struct listener *listen, time_t now)
e87dd3
 #endif
e87dd3
 	}
e87dd3
 
e87dd3
-      strncpy(ifr.ifr_name, name, IF_NAMESIZE);
e87dd3
+      strncpy(ifr.ifr_name, name, IF_NAMESIZE-1);
e87dd3
       if (ioctl(listen->tftpfd, SIOCGIFMTU, &ifr) != -1)
e87dd3
 	{
e87dd3
 	  mtu = ifr.ifr_mtu;  
e87dd3
diff --git a/src/util.c b/src/util.c
e87dd3
index 93b24f5..1a9f228 100644
e87dd3
--- a/src/util.c
e87dd3
+++ b/src/util.c
e87dd3
@@ -491,7 +491,7 @@ int parse_hex(char *in, unsigned char *out, int maxlen,
e87dd3
 		  int j, bytes = (1 + (r - in))/2;
e87dd3
 		  for (j = 0; j < bytes; j++)
e87dd3
 		    { 
e87dd3
-		      char sav = sav;
e87dd3
+		      char sav;
e87dd3
 		      if (j < bytes - 1)
e87dd3
 			{
e87dd3
 			  sav = in[(j+1)*2];
e87dd3
-- 
e87dd3
2.9.3
e87dd3