[clang] [clang] __builtin_os_log_format has incorrect PrintfFormat Attribute argument (PR #178320)
Ziqing Luo via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 29 10:16:34 PST 2026
https://github.com/ziqingluo-90 updated https://github.com/llvm/llvm-project/pull/178320
>From d3ad7ac147634500c15c92ef9cf9b1bb0356da1f Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Tue, 27 Jan 2026 15:41:48 -0800
Subject: [PATCH 1/4] [clang] __builtin_os_log_format has incorrect
PrintfFormat Attribute argument
The incorrect format attribute argument causes false positive
-Wunsafe-buffer-usage-in-format-attr-call warnings.
The format string is the 2nd argument of __builtin_os_log_format, thus
has index 1 in 0-based indexing.
rdar://169043228
---
clang/include/clang/Basic/Builtins.td | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index dcf07d659cb15..1bfee45feb84f 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -4970,8 +4970,7 @@ def OSLogFormatBufferSize : Builtin {
def OSLogFormat : Builtin {
let Spellings = ["__builtin_os_log_format"];
- // FIXME: The printf attribute looks suspiciously like it should be argument #1.
- let Attributes = [PrintfFormat<0>, NoThrow, CustomTypeChecking];
+ let Attributes = [PrintfFormat<1>, NoThrow, CustomTypeChecking];
let Prototype = "void*(void*, char const*, ...)";
}
>From 8012d6c9718a928e13c18d6a9c4d35792290b1e0 Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Wed, 28 Jan 2026 16:52:19 -0800
Subject: [PATCH 2/4] add a test
---
...safe-buffer-usage-format-attr-builtins.cpp | 33 +++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
new file mode 100644
index 0000000000000..efa10096775ae
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wformat \
+// RUN: -verify=expected-format %s
+// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage -Wunsafe-buffer-usage-in-format-attr-call \
+// RUN: -verify %s
+
+namespace std {
+ template<typename T>
+ struct basic_string {
+ T* p;
+ T *c_str();
+ T *data();
+ unsigned size_bytes();
+ unsigned size();
+ };
+
+ typedef basic_string<char> string;
+} // namespace std
+
+// PR#178320 corrects the format attribute arguments for
+// '__builtin_os_log_format' so that
+// '-Wunsafe-buffer-usage-in-format-attr-call' behaves correctly on
+// them. For '-Wformat', the check for '__builtin_os_log_format' is
+// hand-craft without using the attribute. So they are still fine.
+
+void test_format_attr(char * Str, std::string StdStr) {
+ __builtin_os_log_format(nullptr, "hello", Str); // expected-format-warning{{data argument not used by format string}}
+ __builtin_os_log_format(nullptr, "hello %s", StdStr.c_str());
+ __builtin_os_log_format(nullptr, "hello %s", Str); // expected-warning{{formatting function '__builtin_os_log_format' is unsafe}} \
+ expected-note{{string argument is not guaranteed to be null-terminated}}
+
+ __builtin_os_log_format(nullptr, "hello %"); // expected-format-warning{{incomplete format specifier}}
+ __builtin_os_log_format(nullptr, "hello %d", .42); // expected-format-warning{{format specifies type 'int' but the argument has type 'double'}}
+}
>From eee98588a3b11d6b0c985bb5f5d2e258c50b77da Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Wed, 28 Jan 2026 17:16:30 -0800
Subject: [PATCH 3/4] add release note
---
clang/docs/ReleaseNotes.rst | 1 +
1 file changed, 1 insertion(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index f0d3d81f14e43..c1cd683847ae3 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -210,6 +210,7 @@ Bug Fixes to AST Handling
Miscellaneous Bug Fixes
^^^^^^^^^^^^^^^^^^^^^^^
+- Fixed the arguments of the format attribute on ``__builtin_os_log_format``. Previously, they were off by 1.
Miscellaneous Clang Crashes Fixed
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>From 5abe8ea1a094abf4238850ccf47666175dc82fc7 Mon Sep 17 00:00:00 2001
From: Ziqing Luo <ziqing_luo at apple.com>
Date: Thu, 29 Jan 2026 10:15:17 -0800
Subject: [PATCH 4/4] simplify test command
---
.../warn-unsafe-buffer-usage-format-attr-builtins.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
index efa10096775ae..71426aa9ef992 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
@@ -1,7 +1,5 @@
-// RUN: %clang_cc1 -std=c++20 -Wno-all -Wformat \
-// RUN: -verify=expected-format %s
-// RUN: %clang_cc1 -std=c++20 -Wno-all -Wunsafe-buffer-usage -Wunsafe-buffer-usage-in-format-attr-call \
-// RUN: -verify %s
+// RUN: %clang_cc1 -Wformat -verify=expected-format %s
+// RUN: %clang_cc1 -Wunsafe-buffer-usage -Wunsafe-buffer-usage-in-format-attr-call -verify=expected,expected-format %s
namespace std {
template<typename T>
More information about the cfe-commits
mailing list