changeset 1518:4bfbd8b96f66 draft

Various bugfixes (mostly resource leaks) from Ashwini Sharma's static analysis, plus occasional tweak by me while reviewing them.
author Rob Landley <rob@landley.net>
date Thu, 09 Oct 2014 13:43:32 -0500
parents 7dacf2eda737
children bb4cae772039
files toys/lsb/killall.c toys/other/ifconfig.c toys/other/insmod.c toys/other/losetup.c toys/posix/comm.c toys/posix/cp.c toys/posix/id.c
diffstat 7 files changed, 14 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/toys/lsb/killall.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/lsb/killall.c	Thu Oct 09 13:43:32 2014 -0500
@@ -40,7 +40,7 @@
   if (pid == TT.cur_pid) return 0;
 
   if (toys.optflags & FLAG_i) {
-    sprintf(toybuf, "Signal %4000s(%d) ?", name, (int)pid);
+    snprintf(toybuf, sizeof(toybuf), "Signal %s(%d) ?", name, (int)pid);
     if (!yesno(toybuf, 0)) return 0;
   }
 
--- a/toys/other/ifconfig.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/other/ifconfig.c	Thu Oct 09 13:43:32 2014 -0500
@@ -6,7 +6,7 @@
  *
  * Not in SUSv4.
 
-USE_IFCONFIG(NEWTOY(ifconfig, "?a", TOYFLAG_BIN))
+USE_IFCONFIG(NEWTOY(ifconfig, "^?a", TOYFLAG_BIN))
 
 config IFCONFIG
   bool "ifconfig"
--- a/toys/other/insmod.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/other/insmod.c	Thu Oct 09 13:43:32 2014 -0500
@@ -22,7 +22,7 @@
 {
   char * buf = NULL;
   int len, res, i;
-  int fd = xopen(toys.optargs[0], O_RDONLY);
+  int fd = xopen(*toys.optargs, O_RDONLY);
 
   len = fdlength(fd);
   buf = xmalloc(len);
@@ -30,13 +30,17 @@
 
   i = 1;
   while(toys.optargs[i] &&
-    strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf)) {
+    strlen(toybuf) + strlen(toys.optargs[i]) + 2 < sizeof(toybuf))
+  {
     strcat(toybuf, toys.optargs[i++]);
     strcat(toybuf, " ");
   }
 
   res = init_module(buf, len, toybuf);
-  if (CFG_TOYBOX_FREE && buf != toybuf) free(buf);
+  if (CFG_TOYBOX_FREE) {
+    if (buf != toybuf) free(buf);
+    close(fd);
+  }
 
   if (res) perror_exit("failed to load %s", toys.optargs[0]);
 }
--- a/toys/other/losetup.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/other/losetup.c	Thu Oct 09 13:43:32 2014 -0500
@@ -86,7 +86,7 @@
   // Stat the loop device to see if there's a current association.
   memset(loop, 0, sizeof(struct loop_info64));
   if (-1 == lfd || ioctl(lfd, LOOP_GET_STATUS64, loop)) {
-    if (errno == ENXIO && (flags & (FLAG_a|FLAG_j))) return;
+    if (errno == ENXIO && (flags & (FLAG_a|FLAG_j))) goto done;
     if (errno != ENXIO || !file) {
       perror_msg("%s", device ? device : "-f");
       goto done;
--- a/toys/posix/comm.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/posix/comm.c	Thu Oct 09 13:43:32 2014 -0500
@@ -77,5 +77,5 @@
     line[i] = get_line(file[i]);
   }
 
-  if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i--) xclose(file[i]);
+  if (CFG_TOYBOX_FREE) for (i = 0; i < 2; i++) xclose(file[i]);
 }
--- a/toys/posix/cp.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/posix/cp.c	Thu Oct 09 13:43:32 2014 -0500
@@ -119,7 +119,7 @@
   } else {
 
     // -d is only the same as -r for symlinks, not for directories
-    if (S_ISLNK(try->st.st_mode) & (flags & FLAG_d)) flags |= FLAG_r;
+    if (S_ISLNK(try->st.st_mode) && (flags & FLAG_d)) flags |= FLAG_r;
 
     // Detect recursive copies via repeated top node (cp -R .. .) or
     // identical source/target (fun with hardlinks).
--- a/toys/posix/id.c	Thu Oct 09 12:17:36 2014 -0500
+++ b/toys/posix/id.c	Thu Oct 09 13:43:32 2014 -0500
@@ -9,7 +9,7 @@
 USE_ID(NEWTOY(id, ">1nGgru[!Ggu]", TOYFLAG_BIN))
 USE_GROUPS(OLDTOY(groups, id, NULL, TOYFLAG_USR|TOYFLAG_BIN))
 USE_LOGNAME(OLDTOY(logname, id, ">0", TOYFLAG_BIN))
-USE_LOGNAME(OLDTOY(whoami, id, ">0", TOYFLAG_BIN))
+USE_WHOAMI(OLDTOY(whoami, id, ">0", TOYFLAG_BIN))
 
 config ID
   bool "id"
@@ -132,7 +132,7 @@
 
 void id_main(void)
 {
-  // FLAG macros can be 0 if "id" command enabled, so snapshot them here.
+  // FLAG macros can be 0 if "id" command not enabled, so snapshot them here.
   if (FLAG_u) TT.do_u = toys.optflags & FLAG_u;
   if (FLAG_n) TT.do_n = toys.optflags & FLAG_n;
   if (FLAG_G) TT.do_G = toys.optflags & FLAG_G;