Skip to content

Commit 96b9b2e

Browse files
[Clang] Fix '-Wformat-overflow' FP when floats had field-width and plus prefix (#144274)
If field width is specified, the sign/space is already accounted for within the field width, so no additional size is needed. Fixes #143951. --------- Co-authored-by: Aaron Ballman <[email protected]>
1 parent 90da616 commit 96b9b2e

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ Improvements to Clang's diagnostics
655655
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
656656
#GH36703, #GH32903, #GH23312, #GH69874.
657657

658+
- Fixed false positives in ``-Wformat-truncation`` and ``-Wformat-overflow``
659+
diagnostics when floating-point numbers had both width field and plus or space
660+
prefix specified. (#GH143951)
661+
658662
- Clang now avoids issuing `-Wreturn-type` warnings in some cases where
659663
the final statement of a non-void function is a `throw` expression, or
660664
a call to a function that is trivially known to always throw (i.e., its

clang/lib/Sema/SemaChecking.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,10 @@ class EstimateSizeFormatHandler
10121012
break;
10131013
}
10141014

1015-
Size += FS.hasPlusPrefix() || FS.hasSpacePrefix();
1015+
// If field width is specified, the sign/space is already accounted for
1016+
// within the field width, so no additional size is needed.
1017+
if ((FS.hasPlusPrefix() || FS.hasSpacePrefix()) && FieldWidth == 0)
1018+
Size += 1;
10161019

10171020
if (FS.hasAlternativeForm()) {
10181021
switch (FS.getConversionSpecifier().getKind()) {

clang/test/Sema/warn-format-overflow-truncation.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ void call_snprintf(double d, int n, int *ptr) {
4343
__builtin_snprintf(node_name, sizeof(node_name), "%pOFn", ptr); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
4444
__builtin_snprintf(node_name, sizeof(node_name), "12345%pOFn", ptr); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 12}}
4545
__builtin_snprintf(node_name, sizeof(node_name), "123456%pOFn", ptr); // nonkprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 13}}
46+
__builtin_snprintf(buf, 6, "%5.1f", 9.f);
47+
__builtin_snprintf(buf, 6, "%+5.1f", 9.f);
48+
__builtin_snprintf(buf, 6, "% 5.1f", 9.f);
49+
__builtin_snprintf(buf, 6, "% 5.1f", 9.f);
50+
__builtin_snprintf(buf, 6, "%+6.1f", 9.f); // kprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
51+
__builtin_snprintf(buf, 6, "% 6.1f", 9.f); // kprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
52+
__builtin_snprintf(buf, 6, "% 6.1f", 9.f); // kprintf-warning {{'snprintf' will always be truncated; specified size is 6, but format string expands to at least 7}}
4653
}
4754

4855
void call_vsnprintf(void) {
@@ -153,4 +160,11 @@ void call_sprintf(void) {
153160
sprintf(buf, "%+.3f", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
154161
sprintf(buf, "%.0e", 9.f);
155162
sprintf(buf, "5%.1e", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 8}}
163+
sprintf(buf, "%5.1f", 9.f);
164+
sprintf(buf, "%+5.1f", 9.f);
165+
sprintf(buf, "% 5.1f", 9.f);
166+
sprintf(buf, "% 5.1f", 9.f);
167+
sprintf(buf, "%+6.1f", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
168+
sprintf(buf, "% 6.1f", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
169+
sprintf(buf, "% 6.1f", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
156170
}

0 commit comments

Comments
 (0)