]> Savannah Git Hosting - gnulib.git/commitdiff
nstrftime: improve handling of invalid formats
authorPaul Eggert <eggert@cs.ucla.edu>
Tue, 28 Dec 2021 23:28:08 +0000 (15:28 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Tue, 28 Dec 2021 23:29:37 +0000 (15:29 -0800)
* lib/nstrftime.c (__strftime_internal): Without this change,
‘date +'%0_-+^#1%A'’ would output ‘%A’; with it, it outputs
something like ‘%0_-+^#1Tuesday’ which is easier to debug and to
write wrappers for (such as the nstrftime/fprintftime usage in GNU
‘date’).

ChangeLog
lib/nstrftime.c

index 74a988abd233e29b678b582a32bac22b26d979f3..ed9e63b8516c32737fa7b3dd7c2de8e4976ca679 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2021-12-28  Paul Eggert  <eggert@cs.ucla.edu>
+
+       nstrftime: improve handling of invalid formats
+       * lib/nstrftime.c (__strftime_internal): Without this change,
+       ‘date +'%0_-+^#1%A'’ would output ‘%A’; with it, it outputs
+       something like ‘%0_-+^#1Tuesday’ which is easier to debug and to
+       write wrappers for (such as the nstrftime/fprintftime usage in GNU
+       ‘date’).
+
 2021-12-28  Bruno Haible  <bruno@clisp.org>
 
        unilbrk: Update handling of regional indicators for Unicode 10.0.0.
index 25baf76c60f53c82b72fbb03c4bfd44ca6e602db..cc0b34bf998f89d123d1dafa70c99ec88cbdac20 100644 (file)
@@ -651,6 +651,8 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
 
 #endif /* ! DO_MULTIBYTE */
 
+      char const *percent = f;
+
       /* Check for flags that can modify a format.  */
       while (1)
         {
@@ -752,8 +754,8 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
           while (0)
 
         case L_('%'):
-          if (modifier != 0)
-            goto bad_format;
+          if (f - 1 != percent)
+            goto bad_percent;
           add1 (*f);
           break;
 
@@ -1467,6 +1469,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
           }
 
         case L_('\0'):          /* GNU extension: % at end of format.  */
+        bad_percent:
             --f;
             FALLTHROUGH;
         default:
@@ -1474,12 +1477,7 @@ __strftime_internal (STREAM_OR_CHAR_T *s, STRFTIME_ARG (size_t maxsize)
              since this is most likely the right thing to do if a
              multibyte string has been misparsed.  */
         bad_format:
-          {
-            int flen;
-            for (flen = 1; f[1 - flen] != L_('%'); flen++)
-              continue;
-            cpy (flen, &f[1 - flen]);
-          }
+          cpy (f - percent + 1, percent);
           break;
         }
     }