https://github.com/vbvictor updated https://github.com/llvm/llvm-project/pull/144274
>From dfb2abd2c2711c87a22e8f8c93aa82f2621518cb Mon Sep 17 00:00:00 2001 From: Victor Baranov <bar.victor.2...@gmail.com> Date: Sun, 15 Jun 2025 22:20:54 +0300 Subject: [PATCH 1/3] [Clang] Fix '-Wformat-overflow' FP when floats had field-width and plus prefix --- clang/docs/ReleaseNotes.rst | 4 ++++ clang/lib/Sema/SemaChecking.cpp | 5 ++++- clang/test/Sema/warn-format-overflow-truncation.c | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index bee71cf296ac3..2533f49e5aed7 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -650,6 +650,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. + - 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..5fa770d3d3c51 100644 --- a/clang/test/Sema/warn-format-overflow-truncation.c +++ b/clang/test/Sema/warn-format-overflow-truncation.c @@ -43,6 +43,11 @@ 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, "%+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 +158,9 @@ 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, "%+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}} } >From a7043b2093a73964d9b9568c300cc0c40569888c Mon Sep 17 00:00:00 2001 From: Baranov Victor <bar.victor.2...@gmail.com> Date: Mon, 16 Jun 2025 21:01:01 +0300 Subject: [PATCH 2/3] add gh issue reference to clang/docs/ReleaseNotes.rst Co-authored-by: Aaron Ballman <aa...@aaronballman.com> --- clang/docs/ReleaseNotes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 2533f49e5aed7..84bd9cb474187 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -652,7 +652,7 @@ Improvements to Clang's diagnostics - Fixed false positives in ``-Wformat-truncation`` and ``-Wformat-overflow`` diagnostics when floating-point numbers had both width field and plus or space - prefix specified. + 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 >From dc4b105a30c09cf87495e1433b42020817649af1 Mon Sep 17 00:00:00 2001 From: Victor Baranov <bar.victor.2...@gmail.com> Date: Wed, 18 Jun 2025 08:42:14 +0300 Subject: [PATCH 3/3] add tests with multiple spaces --- clang/test/Sema/warn-format-overflow-truncation.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/clang/test/Sema/warn-format-overflow-truncation.c b/clang/test/Sema/warn-format-overflow-truncation.c index 5fa770d3d3c51..eb1feaae834ec 100644 --- a/clang/test/Sema/warn-format-overflow-truncation.c +++ b/clang/test/Sema/warn-format-overflow-truncation.c @@ -46,8 +46,10 @@ void call_snprintf(double d, int n, int *ptr) { __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) { @@ -161,6 +163,8 @@ void call_sprintf(void) { 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}} } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits