]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
tests: Check invalid input in sentence-1-prg
authorDaiki Ueno <ueno@gnu.org>
Wed, 1 Jun 2016 06:54:42 +0000 (15:54 +0900)
committerDaiki Ueno <ueno@gnu.org>
Wed, 1 Jun 2016 06:56:40 +0000 (15:56 +0900)
* gettext-tools/tests/sentence-1-prg.c (main): Check input buffer
overflow, while it is not possible with the current test case.

gettext-tools/tests/sentence-1-prg.c

index e3d3360a692d00bd9e9cf78fb334b02089ac2cbd..84675e082c0794dd814f9d03689afcb6ed1ece90 100644 (file)
 int
 main (int argc, char **argv)
 {
-  char buffer[1024];
-
   while (1)
     {
+      char buffer[1024];
       const char *result;
       ucs4_t ending_char;
-      char *p;
+      char *p, *newline;
+
+      memset (buffer, 0, sizeof buffer);
 
-      if (!fgets (buffer, sizeof buffer, stdin))
+      /* Read REQUIRED_SPACES parameter.  */
+      if (!fgets (buffer, sizeof (buffer) - 1, stdin))
         break;
 
+      newline = strchr (buffer, '\n');
+      if (!newline)
+        return 1;
+      *newline = '\0';
+
       sentence_end_required_spaces = atoi (buffer);
 
-      memset (buffer, 0, sizeof buffer);
+      /* Collect lines until an empty line is read.  */
       p = buffer;
       while (1)
         {
-          p = fgets (p, sizeof buffer - (buffer - p), stdin);
-          if (p == NULL)
+          p = fgets (p, sizeof (buffer) - (p - buffer) - 1, stdin);
+          if (!p)
             break;
+
           if (*p == '\n')
             break;
-          p = strchr (p, '\n') + 1;
+
+          newline = strchr (p, '\n');
+          if (!newline)
+            return 1;
+
+          p = newline + 1;
+          *p = '\0';
         }
+
       if (p == NULL)
         break;
 
-      *(p - 1) = '\0';
-
+      *newline = '\0';
       result = sentence_end (buffer, &ending_char);
       printf ("%X\n%s\n\n", ending_char, result);
     }