https://github.com/vbvictor updated 
https://github.com/llvm/llvm-project/pull/144274

>From 98fc81696400be2ea990d867375530ef3f544b82 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/2] [Clang] Fix '-Wformat-overflow' FP when floats had
 field-width and plus prefix

---
 clang/docs/ReleaseNotes.rst                       |  3 +++
 clang/lib/Sema/SemaChecking.cpp                   |  5 ++++-
 clang/test/Sema/warn-format-overflow-truncation.c | 10 ++++++++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 33ee8a53b5f37..63a530f6ed622 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -629,6 +629,9 @@ 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.
 
 Improvements to Clang's time-trace
 ----------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 69276ce418fa6..8501ce681e903 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 ab5d45bd83bcd8bf5c5e5703df71710e4a554878 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/2] 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 63a530f6ed622..741e39471569f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -631,7 +631,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)
 
 Improvements to Clang's time-trace
 ----------------------------------

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to