Skip to content

Commit 0b91cc4

Browse files
committed
Issue 1104: Explicitly limit the printed string to 12 characters
GCC8 tries to diagnose `snprintf()` overflows but isn't quite smart enough for this case, so emits a false-positive warning. Remember that `%12s` only specifies the minimum number of bytes. GCC8 conservatively assumes this might result in writing the full length of `date2`. (Which will never be longer than 12 bytes, but GCC8 apparently can't reason about `strftime` format specifiers yet.) Changing the specifier here to `%12.12s` explicitly truncates to 12 bytes and should help the compiler understand that this will never overflow. While I'm here, correct a minor typo in the previous line; it used `sizeof(date)` instead of `sizeof(date2)`. (Both are the same size, so this had no functional impact.)
1 parent cef9730 commit 0b91cc4

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

cpio/test/test_option_t.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ DEFINE_TEST(test_option_t)
8888
setlocale(LC_ALL, "");
8989
#endif
9090
#if defined(_WIN32) && !defined(__CYGWIN__)
91-
strftime(date2, sizeof(date), "%b %d %Y", localtime(&mtime));
92-
_snprintf(date, sizeof(date)-1, "%12s file", date2);
91+
strftime(date2, sizeof(date2)-1, "%b %d %Y", localtime(&mtime));
92+
_snprintf(date, sizeof(date)-1, "%12.12s file", date2);
9393
#else
94-
strftime(date2, sizeof(date), "%b %e %Y", localtime(&mtime));
95-
snprintf(date, sizeof(date)-1, "%12s file", date2);
94+
strftime(date2, sizeof(date2)-1, "%b %e %Y", localtime(&mtime));
95+
snprintf(date, sizeof(date)-1, "%12.12s file", date2);
9696
#endif
9797
assertEqualMem(p + 42, date, strlen(date));
9898
free(p);

0 commit comments

Comments
 (0)