]> git.ipfire.org Git - thirdparty/tar.git/commitdiff
Pacify gcc 14 -Wanalyzer-infinite-loop
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 27 Jul 2024 07:05:49 +0000 (00:05 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sat, 27 Jul 2024 07:26:42 +0000 (00:26 -0700)
* gnulib.modules: Add stddef, for ‘unreachable’.
* src/compare.c (dumpdir_cmp): Tell GCC that the default case
is unreachable.  Make just one pass through the string,
instead of two passes (one via strcmp, another via strlen).

gnulib.modules
src/compare.c

index 0e6cec61558fb035486dcfbbe0a68dcfca8ec353..8cbc8dba7e9607f96a10eb31f273072eb8a57594 100644 (file)
@@ -92,6 +92,7 @@ setenv
 snprintf
 stat-time
 stdbool
+stddef
 stdint
 stpcpy
 stdopen
index d08220a2061baba22815d3e436969796c4e273c3..348554e300c46c514415b2d4c701101f90612294 100644 (file)
@@ -334,34 +334,37 @@ diff_special (void)
 static int
 dumpdir_cmp (const char *a, const char *b)
 {
-  size_t len;
-
   while (*a)
     switch (*a)
       {
       case 'Y':
       case 'N':
-       if (!strchr ("YN", *b))
-         return 1;
-       if (strcmp(a + 1, b + 1))
+       /* If the null-terminated strings A and B are equal, other
+          than possibly A's first byte being 'Y' where B's is 'N' or
+          vice versa, advance A and B past the strings.
+          Otherwise, return 1.  */
+       if (! (*b == 'Y' || *b == 'N'))
          return 1;
-       len = strlen (a) + 1;
-       a += len;
-       b += len;
-       break;
-
+       a++, b++;
+       FALLTHROUGH;
       case 'D':
-       if (strcmp(a, b))
+       /* If the null-terminated strings A and B are equal, advance A
+          and B past them.  Otherwise, return 1.  */
+       while (*a)
+         if (*a++ != *b++)
+           return 1;
+       if (*b)
          return 1;
-       len = strlen (a) + 1;
-       a += len;
-       b += len;
+       a++, b++;
        break;
 
       case 'R':
       case 'T':
       case 'X':
        return *b;
+
+      default:
+       unreachable ();
       }
   return *b;
 }