[clang] 96b9b2e - [Clang] Fix '-Wformat-overflow' FP when floats had field-width and plus prefix (#144274)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 30 12:43:51 PDT 2025
Author: Baranov Victor
Date: 2025-06-30T22:43:47+03:00
New Revision: 96b9b2e21de466490ba733a23e8bb6cbd39ad58c
URL: https://github.com/llvm/llvm-project/commit/96b9b2e21de466490ba733a23e8bb6cbd39ad58c
DIFF: https://github.com/llvm/llvm-project/commit/96b9b2e21de466490ba733a23e8bb6cbd39ad58c.diff
LOG: [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 https://github.com/llvm/llvm-project/issues/143951.
---------
Co-authored-by: Aaron Ballman <aaron at aaronballman.com>
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaChecking.cpp
clang/test/Sema/warn-format-overflow-truncation.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d3e7ea0e73219..e6c8f9df22170 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -655,6 +655,10 @@ Improvements to Clang's diagnostics
#GH69470, #GH59391, #GH58172, #GH46215, #GH45915, #GH45891, #GH44490,
#GH36703, #GH32903, #GH23312, #GH69874.
+- Fixed false positives in ``-Wformat-truncation`` and ``-Wformat-overflow``
+ diagnostics when floating-point numbers had both width field and plus or space
+ prefix specified. (#GH143951)
+
- Clang now avoids issuing `-Wreturn-type` warnings in some cases where
the final statement of a non-void function is a `throw` expression, or
a call to a function that is trivially known to always throw (i.e., its
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 51b22a5e122e4..dd5b710d7e1d4 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1012,7 +1012,10 @@ class EstimateSizeFormatHandler
break;
}
- Size += FS.hasPlusPrefix() || FS.hasSpacePrefix();
+ // If field width is specified, the sign/space is already accounted for
+ // within the field width, so no additional size is needed.
+ if ((FS.hasPlusPrefix() || FS.hasSpacePrefix()) && FieldWidth == 0)
+ Size += 1;
if (FS.hasAlternativeForm()) {
switch (FS.getConversionSpecifier().getKind()) {
diff --git a/clang/test/Sema/warn-format-overflow-truncation.c b/clang/test/Sema/warn-format-overflow-truncation.c
index c64a1ed8aaa05..eb1feaae834ec 100644
--- a/clang/test/Sema/warn-format-overflow-truncation.c
+++ b/clang/test/Sema/warn-format-overflow-truncation.c
@@ -43,6 +43,13 @@ void call_snprintf(double d, int n, int *ptr) {
__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}}
__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}}
__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}}
+ __builtin_snprintf(buf, 6, "%5.1f", 9.f);
+ __builtin_snprintf(buf, 6, "%+5.1f", 9.f);
+ __builtin_snprintf(buf, 6, "% 5.1f", 9.f);
+ __builtin_snprintf(buf, 6, "% 5.1f", 9.f);
+ __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}}
+ __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}}
+ __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}}
}
void call_vsnprintf(void) {
@@ -153,4 +160,11 @@ void call_sprintf(void) {
sprintf(buf, "%+.3f", 9.f); // kprintf-warning {{'sprintf' will always overflow; destination buffer has size 6, but format string expands to at least 7}}
sprintf(buf, "%.0e", 9.f);
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}}
+ sprintf(buf, "%5.1f", 9.f);
+ sprintf(buf, "%+5.1f", 9.f);
+ sprintf(buf, "% 5.1f", 9.f);
+ sprintf(buf, "% 5.1f", 9.f);
+ 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}}
+ 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}}
+ 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}}
}
More information about the cfe-commits
mailing list