]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Avoid undefined behaviour.
authorBruno Haible <bruno@clisp.org>
Tue, 19 Nov 2024 08:47:14 +0000 (09:47 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 19 Nov 2024 08:47:14 +0000 (09:47 +0100)
* gettext-tools/src/quote.h (scan_quoted): Add comment. Avoid undefined
behaviour if length==0.

gettext-tools/src/quote.h

index 64195b5a11f752b62cc4c5b4a40318e18d5dcbb0..f8693a974f1b066bfec739aa2f19148647dbe5dd 100644 (file)
@@ -1,5 +1,5 @@
 /* Handle quoted segments of a string.
-   Copyright (C) 2014-2016 Free Software Foundation, Inc.
+   Copyright (C) 2014-2024 Free Software Foundation, Inc.
    Written by Daiki Ueno <ueno@gnu.org>, 2015.
 
    This program is free software: you can redistribute it and/or modify
 extern "C" {
 #endif
 
+
+/* Iterates over the quoted segments of the given string (starting at INPUT,
+   with LENGTH bytes) and invokes
+     CALLBACK (quote, segment_start, segment_length, DATA)
+   on each.  The definition of "quoted segment" is as in po/quot.sed.
+   It also invokes
+     CALLBACK ('\0', segment_start, segment_length, DATA)
+   for the segments in-between (outside quotes).  */
 static void
 scan_quoted (const char *input, size_t length,
-             void (* callback) (char quote, const char *quoted,
-                                size_t quoted_length,
+             void (* callback) (char quote,
+                                const char *segment_start,
+                                size_t segment_length,
                                 void *data),
              void *data)
 {
@@ -38,14 +47,14 @@ scan_quoted (const char *input, size_t length,
   /* START shall point to the beginning of a quoted segment, END
      points to the end of the entire input string.  */
   start = input;
-  end = &input[length - 1];
+  end = input + length;
   
   /* True if we have seen a character which could be an opening
      quotation mark.  Note that we can't determine if it is really an
      opening quotation mark until we see a closing quotation mark.  */
   seen_opening = false;
 
-  for (p = start; p <= end; p++)
+  for (p = start; p < end; p++)
     {
       switch (*p)
         {
@@ -104,9 +113,9 @@ scan_quoted (const char *input, size_t length,
                        the right quote is followed by a space.  */
                   || (*start == '\''
                       && (((start > input && *(start - 1) == ' ')
-                           && (p == end || *(p + 1) == '\n' || *(p + 1) == ' '))
+                           && (p + 1 == end || p[1] == '\n' || p[1] == ' '))
                           || ((start == input || *(start - 1) == '\n')
-                              && p < end && *(p + 1) == ' '))))
+                              && p + 1 < end && p[1] == ' '))))
                 {
                   callback ('\'', start + 1, p - (start + 1), data);
                   start = p + 1;