[libc-commits] [libc] [libc][printf] Fix %s precision handling (PR #233098)

Michael Jones via libc-commits libc-commits at lists.llvm.org
Mon Oct 19 15:16:29 PDT 2026


================
@@ -40,11 +40,13 @@ LIBC_INLINE int write_char_string_as_chars(Writer<mode, char> *writer,
   }
 #endif // LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
 
-  string_len = internal::string_length(str_ptr);
-
-  if (to_conv.precision >= 0 &&
-      static_cast<size_t>(to_conv.precision) < string_len)
-    string_len = to_conv.precision;
+  // If a precision is specified, it doesn't have to be null-terminated.
+  if (to_conv.precision >= 0) {
+    while (string_len < static_cast<size_t>(to_conv.precision) &&
+           str_ptr[string_len] != '\0')
+      string_len++;
+  } else
+    string_len = internal::string_length(str_ptr);
----------------
michaelrj-google wrote:

nit: if one side of an if/else needs braces then they should be there for both sides to make it easier to read

https://github.com/llvm/llvm-project/pull/233098


More information about the libc-commits mailing list