Просмотр исходного кода

check_game: fix segfaults when qstat errors

check_game could segfault under (at least) two conditions:
when qstat wasn't installed or when an invalid flag was used.
Both of these resulted in nothing in stdout. check_game then
promptly tries to read this and we get the segfault. By adding
a check and also checking stderr, we safely avoid the segfault.
Emmett Kapsner 1 день назад
Родитель
Сommit
39795f637f
1 измененных файлов с 8 добавлено и 2 удалено
  1. 8 2
      plugins/check_game.c

+ 8 - 2
plugins/check_game.c

@@ -70,6 +70,7 @@ main (int argc, char **argv)
   char *p, *ret[QSTAT_MAX_RETURN_ARGS];
   size_t i = 0;
   output chld_out;
+  output chld_err;
 
   setlocale (LC_ALL, "");
   bindtextdomain (PACKAGE, LOCALEDIR);
@@ -95,7 +96,7 @@ main (int argc, char **argv)
 
   /* run the command. historically, this plugin ignores output on stderr,
    * as well as return status of the qstat program */
-  (void)np_runcmd(command_line, &chld_out, NULL, 0);
+  (void)np_runcmd(command_line, &chld_out, &chld_err, 0);
 
   /* sanity check */
   /* was thinking about running qstat without any options, capturing the
@@ -107,11 +108,16 @@ main (int argc, char **argv)
      In the end, I figured I'd simply let an error occur & then trap it
    */
 
-  if (!strncmp (chld_out.line[0], "unknown option", 14)) {
+  if (chld_err.line != NULL && !strncmp (chld_err.line[0], "unknown option", 14)) {
     printf (_("CRITICAL - Host type parameter incorrect!\n"));
     result = STATE_CRITICAL;
     return result;
   }
+  else if (chld_out.line == NULL) {
+    printf (_("CRITICAL - Error when running qstat!\n"));
+    result = STATE_CRITICAL;
+    return result;
+  }
 
   p = (char *) strtok (chld_out.line[0], QSTAT_DATA_DELIMITER);
   while (p != NULL) {