changeset 1433:00c20f410c46 draft

Patches to commands for issues reported from static analysis tool. portability.h.patch - it is for O_CLOEXEC, as compiler complained of it. Makefile.patch - for cleaning generated/*.o files and libopts.dat file [Fixup to uniq.c from Rob.]
author Ashwini Sharma <ak.ashwini1981@gmail.com>
date Tue, 12 Aug 2014 07:09:01 -0500
parents cd5c62119391
children 08a34594b2c6
files lib/portability.h toys/other/ifconfig.c toys/pending/arp.c toys/pending/fsck.c toys/pending/ftpget.c toys/pending/modprobe.c toys/pending/tar.c toys/posix/uniq.c
diffstat 8 files changed, 35 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/lib/portability.h	Mon Aug 11 20:15:19 2014 -0500
+++ b/lib/portability.h	Tue Aug 12 07:09:01 2014 -0500
@@ -170,6 +170,10 @@
 #define O_NOFOLLOW 0
 #endif
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 02000000
+#endif
+
 #if defined(__SIZEOF_DOUBLE__) && defined(__SIZEOF_LONG__) \
     && __SIZEOF_DOUBLE__ <= __SIZEOF_LONG__
 typedef double FLOAT;
--- a/toys/other/ifconfig.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/other/ifconfig.c	Tue Aug 12 07:09:01 2014 -0500
@@ -421,7 +421,7 @@
       }
 
       if ((p-ptr) != count || *hw_addr)
-        error_exit("bad hw-addr '%s'", hw_addr ? hw_addr : "");
+        error_exit("bad hw-addr '%s'", *argv);
 
       // the linux kernel's "struct sockaddr" (include/linux/socket.h in the
       // kernel source) only has 14 bytes of sa_data, and an infiniband address
--- a/toys/pending/arp.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/pending/arp.c	Tue Aug 12 07:09:01 2014 -0500
@@ -175,7 +175,7 @@
   flags = ATF_PERM | ATF_COM;
   if (toys.optargs[2]) check_flags(&flags, (toys.optargs+2));
   req.arp_flags = flags;
-  strncpy(req.arp_dev, TT.device, sizeof(TT.device));
+  strncpy(req.arp_dev, TT.device, sizeof(req.arp_dev));
   xioctl(TT.sockfd, SIOCSARP, &req);
   
   if (toys.optflags & FLAG_v) xprintf("Entry set for %s\n", toys.optargs[0]);
@@ -204,7 +204,7 @@
   flags = ATF_PERM;
   if (toys.optargs[1]) check_flags(&flags, (toys.optargs+1));
   req.arp_flags = flags;
-  strncpy(req.arp_dev, TT.device, sizeof(TT.device));
+  strncpy(req.arp_dev, TT.device, sizeof(req.arp_dev));
   xioctl(TT.sockfd, SIOCDARP, &req);
   
   if (toys.optflags & FLAG_v) xprintf("Delete entry for  %s\n", toys.optargs[0]);
@@ -301,6 +301,7 @@
   
   if (CFG_TOYBOX_FREE) {
     free(host_ip);
+    free(buf);
     xclose(fd);
   }
 }
--- a/toys/pending/fsck.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/pending/fsck.c	Tue Aug 12 07:09:01 2014 -0500
@@ -216,10 +216,13 @@
   }
 
   if (toys.optflags & FLAG_N) {
+    for (j=0;j<i;j++) free(args[i]);
     free(args);
     return;
   } else { 
     if ((pid = fork()) < 0) {
+      for (j=0;j<i;j++) free(args[i]);
+      free(args);
       perror_msg(args[0]);
       return; 
     }
@@ -383,6 +386,7 @@
   if (!TT.devices || (toys.optflags & FLAG_A)) {
     toys.exitval = scan_all();
     if (CFG_TOYBOX_FREE) goto free_all;
+    return; //if CFG_TOYBOX_FREE is not set, exit.
   }
 
   dev = TT.devices;
--- a/toys/pending/ftpget.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/pending/ftpget.c	Tue Aug 12 07:09:01 2014 -0500
@@ -153,21 +153,29 @@
   unsigned portnum;
 
   //vsftpd reply like:- "227 Entering Passive Mode (125,19,39,117,43,39)".
-  if (get_ftp_response("PASV", NULL) != PASSIVE_MODE) close_stream("PASV");
+  if (get_ftp_response("PASV", NULL) != PASSIVE_MODE) goto close_stream;
 
   //Response is "NNN <some text> (N1,N2,N3,N4,P1,P2) garbage.
   //Server's IP is N1.N2.N3.N4
   //Server's port for data connection is P1*256+P2.
-  if ((pch = strrchr(toybuf, ')'))) *pch = '\0';
-  if ((pch = strrchr(toybuf, ','))) *pch = '\0';
+  if (!(pch = strrchr(toybuf, ')'))) goto close_stream;
+  *pch = '\0';
+  if (!(pch = strrchr(toybuf, ','))) goto close_stream;
+  *pch = '\0';
+
   portnum = atolx_range(pch + 1, 0, 255);
 
-  if ((pch = strrchr(toybuf, ','))) *pch = '\0';
+  if (!(pch = strrchr(toybuf, ','))) goto close_stream;
+  *pch = '\0';
   portnum = portnum + (atolx_range(pch + 1, 0, 255) * 256);
   setport(htons(portnum));
 
   if (TT.isget && get_ftp_response("SIZE", r_filename) != FTPFILE_STATUS)
     TT.c = 0;
+  return;
+
+close_stream:
+  close_stream("PASV");
 }
 
 /*
--- a/toys/pending/modprobe.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/pending/modprobe.c	Tue Aug 12 07:09:01 2014 -0500
@@ -199,7 +199,7 @@
   }
   line[len] = '\0';
   *li = xstrdup(line);
-  if (line) free(line);
+  free(line);
   if (nxtline) free(nxtline);
   return len;
 }
@@ -302,6 +302,7 @@
     }
     free(line);
   }
+  fclose(fe);
   return ret;
 }
 
--- a/toys/pending/tar.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/pending/tar.c	Tue Aug 12 07:09:01 2014 -0500
@@ -84,8 +84,8 @@
   cnt = size/512 + (rem?1:0);
 
   for (i = 0; i < cnt; i++) {
-    rd = (((i == cnt-1) && rem)? rem:512);
-    if (readall(src, toybuf, rd) != rd) error_exit("short read");
+    rd = (i == cnt-1 && rem) ? rem : 512;
+    xreadall(src, toybuf, rd);
     writeall(dst, toybuf, rd);
   }
 }
@@ -93,11 +93,10 @@
 //convert to octal
 static void itoo(char *str, int len, off_t val)
 {
-  char *t, tmp[sizeof(off_t)*3+1]; //1 for NUL termination
+  char *t, tmp[sizeof(off_t)*3+1];
   int cnt  = sprintf(tmp, "%0*llo", len, val);
 
-  t = tmp;
-  t += (cnt - len);
+  t = tmp + cnt - len;
   if (*t == '0') t++;
   memcpy(str, t, len);
 }
@@ -547,7 +546,9 @@
       break;
     }
   }
-  return ((value)?xstrdup(value) : NULL);
+  if (value) value = xstrdup(value);
+  free(buf);
+  return value;
 }
 
 static void tar_skip(struct archive_handler *tar, int sz)
--- a/toys/posix/uniq.c	Mon Aug 11 20:15:19 2014 -0500
+++ b/toys/posix/uniq.c	Tue Aug 12 07:09:01 2014 -0500
@@ -113,6 +113,8 @@
   print_line(outfile, prevline);
 
   if (CFG_TOYBOX_FREE) {
+    if (outfile != stdout) fclose(outfile);
+    if (infile != stdin) fclose(infile);
     free(prevline);
     free(thisline);
   }