Author: Ziqing Luo
Date: 2026-02-02T11:51:18-08:00
New Revision: a86202e82f040a62a346e173a9d3d1826146b9b2

URL: 
https://github.com/llvm/llvm-project/commit/a86202e82f040a62a346e173a9d3d1826146b9b2
DIFF: 
https://github.com/llvm/llvm-project/commit/a86202e82f040a62a346e173a9d3d1826146b9b2.diff

LOG: [clang] __builtin_os_log_format has incorrect PrintfFormat Attribute 
argument (#178320)

The format string is the 2nd argument of __builtin_os_log_format, thus
has index 1 instead of 0 in 0-based indexing.
The incorrect format attribute argument causes false positive
-Wunsafe-buffer-usage-in-format-attr-call warnings.

rdar://169043228

Added: 
    clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/Basic/Builtins.td

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 1b4c9ae3003b1..7dc6881ed43e6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -242,6 +242,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
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/include/clang/Basic/Builtins.td 
b/clang/include/clang/Basic/Builtins.td
index a410a138836eb..9a21d863127a2 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*, ...)";
 }
 

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..71426aa9ef992
--- /dev/null
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-format-attr-builtins.cpp
@@ -0,0 +1,31 @@
+// 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>
+  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'}} 
+}


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to