Przeglądaj źródła

Merge branch 'misc-warnings'

* misc-warnings:
  * Fix off-by-1 NULL after realpath() calls
  * Fix warnings in compile utils
  * Handle eof in readuserfile() properly
  * Ignore errors from bad write()/read()/fread()
  * Fix more warnings about unused results (warn_unused_result)
  * Remove unecesary double htonl() calls
  * Fix socket leak in readuserfile() when the file is in an invalid format
  * Fix multiple warnings about unused results (warn_unused_result)

Conflicts:
	src/users.c
Bryan Drewery 16 lat temu
rodzic
commit
38710702b9
15 zmienionych plików z 92 dodań i 44 usunięć
  1. 3 1
      src/adns.c
  2. 2 7
      src/bg.c
  3. 7 2
      src/conf.c
  4. 15 5
      src/dcc.c
  5. 6 2
      src/dccutil.c
  6. 5 3
      src/main.c
  7. 1 2
      src/makehelp.c
  8. 1 2
      src/makeres.c
  9. 1 2
      src/misc.c
  10. 3 1
      src/misc_file.c
  11. 1 1
      src/mod/server.mod/server.c
  12. 17 4
      src/mod/transfer.mod/transfer.c
  13. 3 1
      src/net.c
  14. 20 8
      src/shell.c
  15. 7 3
      src/stringfix.c

+ 3 - 1
src/adns.c

@@ -378,7 +378,9 @@ void egg_dns_send(char *query, int len)
           sdprintf("SETTING TIMEOUT to %d", async_server_timeout);
           dcc[dns_idx].timeval = now;
         }
-        write(dcc[dns_idx].sock, query, len);
+        if (write(dcc[dns_idx].sock, query, len) == -1) {
+	  ;
+	}
 //	sockbuf_write(dns_idx, query, len);
 }
 

+ 2 - 7
src/bg.c

@@ -62,7 +62,7 @@ int close_tty()
   return 0;
 }
 
-static int my_daemon(int nochdir, int noclose)
+static int my_daemon()
 {
   switch (fork()) {
     case -1:
@@ -76,11 +76,6 @@ static int my_daemon(int nochdir, int noclose)
   if (setsid() == -1)
     return (-1);
 
-  if (!nochdir)
-    (void) chdir("/");
-
-  if (!noclose)
-    close_tty();
   return (0);
 }
 
@@ -88,7 +83,7 @@ static int my_daemon(int nochdir, int noclose)
 pid_t
 do_fork()
 {
-  if (my_daemon(1, 1))
+  if (my_daemon())
     fatal(strerror(errno), 0);
 
   pid_t pid = getpid();

+ 7 - 2
src/conf.c

@@ -414,7 +414,10 @@ checkpid(const char *nick, conf_bot *bot)
   if ((f = fopen(buf, "r"))) {
     char *bufp = NULL, *pids = NULL;
 
-    fgets(buf, sizeof(buf), f);
+    if (!fgets(buf, sizeof(buf), f)) {
+      fclose(f);
+      return 0;
+    }
     fclose(f);
     remove_crlf(buf);
 
@@ -1108,7 +1111,9 @@ bin_to_conf(bool error)
   }
 
   char datadir[PATH_MAX] = "";
-  realpath(conf.datadir, datadir);
+  if (!realpath(conf.datadir, datadir))
+    werr(ERR_DATADIR);
+
   str_redup(&conf.datadir, datadir);
   if (!mkdir_p(conf.datadir) && error)
     werr(ERR_DATADIR);

+ 15 - 5
src/dcc.c

@@ -112,22 +112,32 @@ strip_telnet(int sock, char *buf, int *len)
         /* WILL X -> response: DONT X */
         /* except WILL ECHO which we just smile and ignore */
         if (*(p + 1) != TLN_ECHO) {
-          write(sock, TLN_IAC_C TLN_DONT_C, 2);
-          write(sock, p + 1, 1);
+          if (write(sock, TLN_IAC_C TLN_DONT_C, 2) == -1) {
+            ;
+          }
+          if (write(sock, p + 1, 1) == -1) {
+            ;
+          }
         }
       }
       if (*p == TLN_DO) {
         /* DO X -> response: WONT X */
         /* except DO ECHO which we just smile and ignore */
         if (*(p + 1) != TLN_ECHO) {
-          write(sock, TLN_IAC_C TLN_WONT_C, 2);
-          write(sock, p + 1, 1);
+          if (write(sock, TLN_IAC_C TLN_WONT_C, 2) == -1) {
+            ;
+          }
+          if (write(sock, p + 1, 1) == -1) {
+            ;
+          }
         }
       }
       if (*p == TLN_AYT) {
         /* "are you there?" */
         /* response is: "hell yes!" */
-        write(sock, "\r\nHell, yes!\r\n", 14);
+        if (write(sock, "\r\nHell, yes!\r\n", 14) == -1) {
+          ;
+        }
       }
       /* Anything else can probably be ignored */
       p += mark - 1;

+ 6 - 2
src/dccutil.c

@@ -1031,12 +1031,16 @@ identd_open(const char *sourceIp, const char *destIp)
         free(p);
       }
 
-      ftruncate(fileno(f), 0);
-      fwrite(outbuf, 1, strlen(outbuf), f);
+      if (ftruncate(fileno(f), 0))
+        goto failure;
+
+      if (!fwrite(outbuf, 1, strlen(outbuf), f))
+        goto failure;
 
       //And make a record in the oidentd.conf to spoof the ident request from this port->dest-prot
       fprintf(f, "to %s from %s { reply \"%s\" }\n", destIp, sourceIp, origbotname);
       
+failure:
       fflush(f);
       flock(fileno(f), LOCK_UN);
       fclose(f);

+ 5 - 3
src/main.c

@@ -106,7 +106,7 @@ uid_t   myuid;
 pid_t   mypid;
 bool	term_z = 0;		/* Foreground: use the terminal as a party line? */
 int 	updating = 0; 		/* this is set when the binary is called from itself. */
-char 	tempdir[DIRMAX] = "";
+char 	tempdir[PATH_MAX] = "";
 char 	*binname = NULL;
 time_t	online_since;		/* Unix-time that the bot loaded up */
 time_t  restart_time;
@@ -171,7 +171,8 @@ static char *getfullbinname(const char *argv_zero)
   bin[strlen(bin)] = 0;
 #endif /* CYGWIN_HACKS */
   /* Fix for symlinked binaries */
-  realpath(bin, buf);
+  if (!realpath(bin, buf))
+    fatal(STR("realpath() failed"), 0);
   size_t len = strlen(buf);
   bin = (char *) my_realloc(bin, len + 1);
   strlcpy(bin, buf, len + 1);
@@ -745,7 +746,8 @@ printf("out: %s\n", out);
   }
 
   binname = getfullbinname(argv[0]);
-  chdir(dirname(binname));
+  if (chdir(dirname(binname)))
+    werr(ERR_BINSTAT);
 
   /* Find a temporary tempdir until we load binary data */
   /* setup initial tempdir as /tmp until we read in tmpdir from conf */

+ 1 - 2
src/makehelp.c

@@ -44,8 +44,7 @@ char *step_thru_file(FILE *fd)
   }
   retStr = NULL;
   while (!feof(fd)) {
-    fgets(tempBuf, sizeof(tempBuf), fd);
-    if (!feof(fd)) {
+    if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
       if (retStr == NULL) {
         retStr = (char *) calloc(1, strlen(tempBuf) + 2);
         strcpy(retStr, tempBuf);

+ 1 - 2
src/makeres.c

@@ -60,8 +60,7 @@ char *step_thru_file(FILE *fd)
   }
   retStr = NULL;
   while (!feof(fd)) {
-    fgets(tempBuf, sizeof(tempBuf), fd);
-    if (!feof(fd)) {
+    if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
       if (retStr == NULL) {
         retStr = (char *) calloc(1, strlen(tempBuf) + 2);
         strcpy(retStr, tempBuf);

+ 1 - 2
src/misc.c

@@ -1310,8 +1310,7 @@ char *step_thru_file(FILE *fd)
   size_t ret_siz = 0;
 
   while (!feof(fd)) {
-    fgets(tempBuf, sizeof(tempBuf), fd);
-    if (!feof(fd)) {
+    if (fgets(tempBuf, sizeof(tempBuf), fd) && !feof(fd)) {
       if (retStr == NULL) {
         ret_siz = strlen(tempBuf) + 2;
         retStr = (char *) my_calloc(1, ret_siz);

+ 3 - 1
src/misc_file.c

@@ -322,7 +322,9 @@ bool Tempfile::FindDir()
   };
 
   for (int i = 0; dirs[i]; i++) {
-    realpath(dirs[i], tempdir);
+    if (!realpath(dirs[i], tempdir))
+      continue;
+    tempdir[(DIRMAX < PATH_MAX ? DIRMAX : PATH_MAX) - 1] = 0;
     size_t len = strlen(tempdir);
     tempdir[len] = '/';
     tempdir[len + 1] = '\0';

+ 1 - 1
src/mod/server.mod/server.c

@@ -935,7 +935,7 @@ static void dcc_chat_hostresolved(int i)
 
   simple_snprintf(buf, sizeof buf, "%d", dcc[i].port);
 
-  simple_snprintf(ip, sizeof ip, "%lu", iptolong(htonl(dcc[i].addr)));
+  simple_snprintf(ip, sizeof ip, "%lu", (0xffffffff & ((long) dcc[i].addr)));
 #ifdef USE_IPV6
   dcc[i].sock = getsock(0, AF_INET);
 #else

+ 17 - 4
src/mod/transfer.mod/transfer.c

@@ -95,9 +95,10 @@ static unsigned long pump_file_to_sock(FILE *file, long sock,
   if (bf) {
     do {
       actual_size = pending_data >= buf_len ? buf_len : pending_data;
-      fread(bf, actual_size, 1, file);
-      tputs(sock, bf, actual_size);
-      pending_data -= actual_size;
+      if (fread(bf, actual_size, 1, file)) {
+        tputs(sock, bf, actual_size);
+        pending_data -= actual_size;
+      }
     } while (!sock_has_data(SOCK_DATA_OUTGOING, sock) && pending_data != 0);
     free(bf);
   }
@@ -462,7 +463,19 @@ void dcc_send(int idx, char *buf, int len)
   size_t siz = 0;
   unsigned long sent;
 
-  fwrite(buf, len, 1, dcc[idx].u.xfer->f);
+  if (!fwrite(buf, len, 1, dcc[idx].u.xfer->f)) {
+    putlog(LOG_FILES, "*", "Problem writing file");
+    fclose(dcc[idx].u.xfer->f);
+    siz = strlen(tempdir) + strlen(dcc[idx].u.xfer->filename) + 1;
+    b = (char *) my_calloc(1, siz);
+    strlcpy(b, tempdir, siz);
+    strlcat(b, dcc[idx].u.xfer->filename, siz);
+    unlink(b);
+    free(b);
+    killsock(dcc[idx].sock);
+    lostdcc(idx);
+    return;
+  }
 
   fflush(dcc[idx].u.xfer->f);
   fsync(fileno(dcc[idx].u.xfer->f));

+ 3 - 1
src/net.c

@@ -1430,7 +1430,9 @@ void tputs(register int z, char *s, size_t len)
     return;			
 
   if (((z == STDOUT) || (z == STDERR)) && (!backgrd || use_stderr)) {
-    write(z, s, len);
+    if (write(z, s, len) == -1) {
+      ;
+    }
     return;
   }
 

+ 20 - 8
src/shell.c

@@ -416,7 +416,8 @@ int shell_exec(char *cmdline, char *input, char **output, char **erroutput)
       } else {
         buf = (char *) my_calloc(1, fs + 1);
         fseek(err->f, 0, SEEK_SET);
-        fread(buf, 1, fs, err->f);
+        if (!fread(buf, 1, fs, err->f))
+          fs = 0;
         buf[fs] = 0;
         (*erroutput) = buf;
       }
@@ -432,7 +433,8 @@ int shell_exec(char *cmdline, char *input, char **output, char **erroutput)
       } else {
         buf = (char *) my_calloc(1, fs + 1);
         fseek(out->f, 0, SEEK_SET);
-        fread(buf, 1, fs, out->f);
+        if (!fread(buf, 1, fs, out->f))
+          fs = 0;
         buf[fs] = 0;
         (*output) = buf;
       }
@@ -726,7 +728,7 @@ void baduname(char *confhas, char *myuname) {
 
 char *homedir(bool useconf)
 {
-  static char homedir_buf[DIRMAX] = "";
+  static char homedir_buf[PATH_MAX] = "";
 
   if (!homedir_buf[0]) {
     char tmp[DIRMAX] = "";
@@ -747,8 +749,13 @@ char *homedir(bool useconf)
 #endif /* CYGWIN_HACKS */
     }
     ContextNote(STR("realpath()"));
-    if (tmp[0])
-      realpath(tmp, homedir_buf); /* this will convert lame home dirs of /home/blah->/usr/home/blah */
+    if (tmp[0]) {
+      if (!realpath(tmp, homedir_buf)) { /* this will convert lame home dirs of /home/blah->/usr/home/blah */
+        homedir_buf[0] = 0;
+        return NULL;
+      }
+      homedir_buf[DIRMAX < PATH_MAX ? DIRMAX - 1 : PATH_MAX - 1] = 0;
+    }
     ContextNote(STR("realpath(): Success"));
   }
   return homedir_buf[0] ? homedir_buf : NULL;
@@ -858,12 +865,14 @@ char *move_bin(const char *ipath, const char *file, bool run)
 
   /* move the binary to the correct place */
   static char newbin[DIRMAX] = "";
-  char real[DIRMAX] = "";
+  char real[PATH_MAX] = "";
 
   simple_snprintf(newbin, sizeof newbin, "%s%s%s", path, path[strlen(path) - 1] == '/' ? "" : "/", file);
 
   ContextNote(STR("realpath()"));
-  realpath(binname, real);            /* get the realpath of binname */
+  if (!realpath(binname, real))            /* get the realpath of binname */
+    fatal(STR("Unable to determine binary path."), 0);
+  real[(DIRMAX < PATH_MAX ? DIRMAX : PATH_MAX) - 1] = 0;
   ContextNote(STR("realpath(): Success"));
   /* running from wrong dir, or wrong bin name.. lets try to fix that :) */
   sdprintf(STR("binname: %s"), binname);
@@ -894,7 +903,10 @@ char *move_bin(const char *ipath, const char *file, bool run)
       if (run) {
         simple_snprintf(newbin, sizeof newbin, "%s%s%s", 
                         path, path[strlen(path) - 1] == '/' ? "" : "/", shell_escape(file));
-        system(newbin);
+        if (system(newbin) == -1) {
+          printf("Unable to automatically start new binary.\n");
+          exit(1);
+        }
         sdprintf(STR("exiting to let new binary run..."));
         exit(0);
       }

+ 7 - 3
src/stringfix.c

@@ -103,7 +103,7 @@ void processline(char *line)
   char tmpin[WTF] = "", tmpout[WTF] = "", *in = NULL, *out = NULL;
 
   strcpy(tmpin, line); 
-  memset((char *) &tmpin[strlen(tmpin)], 20, 0);
+  memset((char *) &tmpin[strlen(tmpin)], 0, 20);
   in = tmpin;
   out = tmpout;
   if (*in) {
@@ -144,7 +144,9 @@ int main(int argc, char *argv[])
   insize = ftell(f);
   fseek(f, 0, SEEK_SET);
   buf = (char *) calloc(1, insize + 1);
-  fread(buf, 1, insize, f);
+  if (!fread(buf, 1, insize, f)) {
+    ;
+  }
   fclose(f);
   buf[insize] = 0;
 
@@ -159,7 +161,9 @@ int main(int argc, char *argv[])
   }
 
   if ((f = fopen(argv[2], "w"))) {
-    fwrite(outbuf, strlen(outbuf), 1, f);
+    if (!fwrite(outbuf, strlen(outbuf), 1, f)) {
+      ;
+    }
     fclose(f);
   }
   /*  printf(outbuf); */