Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 30533d0

Browse files
authored
[libc][docs] Document printf NULL handling for %s/%n (#180067)
- Fix printf_behavior to describe NULL %s as printing "(null)" when nullptr checks are enabled - Point undefined_behavior NULL %s/%n entry at LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS - Add Sphinx labels to link directly to the flag and Conversion sections
1 parent 4066ef6 commit 30533d0

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

libc/docs/dev/printf_behavior.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ conversions will be ignored. This reduces code size. This will be set by default
7878
on windows platforms as current printf implementation does not support UTF-16 wide
7979
characters.
8080

81+
.. _printf_no_nullptr_checks:
82+
8183
LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
8284
----------------------------------
83-
When set, this flag disables the nullptr checks in %n and %s.
85+
When set, this flag disables the nullptr checks in %n and %s; passing a null
86+
pointer is undefined behavior. See :ref:`printf_conversion` for the behavior
87+
when nullptr checks are enabled.
8488

8589
LIBC_COPT_PRINTF_CONV_ATLAS
8690
---------------------------
@@ -188,6 +192,8 @@ If a number passed as a bit width is less than or equal to zero, the conversion
188192
is considered invalid. If the provided bit width is larger than the width of
189193
uintmax_t, it will be clamped to the width of uintmax_t.
190194

195+
.. _printf_conversion:
196+
191197
----------
192198
Conversion
193199
----------
@@ -199,7 +205,7 @@ If a conversion specification ends in %, then it will be treated as if it is
199205
"%%", ignoring all options.
200206

201207
If a null pointer is passed to a %s conversion specification and null pointer
202-
checks are enabled, it will be treated as if the provided string is "null".
208+
checks are enabled, it will be treated as if the provided string is "(null)".
203209

204210
If a null pointer is passed to a %n conversion specification and null pointer
205211
checks are enabled, the conversion will fail and printf will return a negative

libc/docs/dev/undefined_behavior.rst

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,11 @@ Path without Leading Slashs in shm_open
7575
----------------------------------------
7676
POSIX.1 leaves that when the name of a shared memory object does not begin with a slash, the behavior is implementation defined. In such cases, the shm_open in LLVM libc is implemented to behave as if the name began with a slash.
7777

78-
Handling of NULL arguments to the 's' format specifier
79-
------------------------------------------------------
80-
The C standard does not specify behavior for ``printf("%s", NULL)``. We will
81-
print the string literal ``(null)`` unless using the
82-
``LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS`` option described in :ref:`printf
83-
behavior<printf_behavior>`.
84-
TODO: Move this to printf_behavior.
78+
Handling of NULL arguments to the 's' and 'n' format specifiers
79+
---------------------------------------------------------------
80+
The C standard does not specify behavior for ``printf("%s", NULL)`` or
81+
``printf("%n", NULL)``. For LLVM-libc, see
82+
:ref:`LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS <printf_no_nullptr_checks>` for details.
8583

8684
Unknown Math Rounding Direction
8785
-------------------------------

0 commit comments

Comments
 (0)