Issue 245060
Summary [libc] swprintf/vswprintf return the untruncated length instead of a negative value when the output does not fit
Labels libc
Assignees
Reporter SohamDutta02
    ---
 
`swprintf` and `vswprintf` (added in #243207 and #243606) return the full, untruncated output length when the output does not fit in the buffer. That is `snprintf`'s behavior. The C standard gives `swprintf` different semantics (C99 7.24.2.3p3, C11/C17 7.29.2.3p3):
 
> The `swprintf` function returns the number of wide characters written in the array, not counting the terminating null wide character, or a negative value if an encoding error occurred or if `n` or more wide characters were requested to be written.
 
POSIX says the same and additionally requires `errno` to be set. glibc returns -1 with `errno = E2BIG`.
 
### Reproducer
 
```c
#include <stdio.h>
#include <wchar.h>
 
int main(void) {
  wchar_t buf[5];
  int n = swprintf(buf, 5, L"%ls", L"1234567890");
  printf("swprintf(buf, 5, ...) = %d, buf = \"%ls\"\n", n, buf);
  n = swprintf(NULL, 0, L"%ls", L"hello");
  printf("swprintf(NULL, 0, ...) = %d\n", n);
 return 0;
}
```
 
LLVM libc (main @ 2252e5dc213d, x86_64 Linux):
 
```
swprintf(buf, 5, ...) = 10, buf = "1234"
swprintf(NULL, 0, ...) = 5
```
 
glibc:
 
```
swprintf(buf, 5, ...) = -1, buf = "1234"
swprintf(NULL, 0, ...) = -1
```
 
`vswprintf` behaves the same way.
 
### Why it matters
 
Unlike `snprintf`, `swprintf` gives no way to learn the required size, so the portable idiom is "a negative result means it didn't fit; otherwise the buffer holds exactly `n` characters". With the current behavior, truncation is reported as success, and the returned count can be larger than the buffer. Code that uses the count as a length then reads or writes past the end of the array. For example:
 
```c
wchar_t buf[16];
int n = swprintf(buf, 16, L"user=%ls", name);
if (n < 0)
 return handle_too_long();
buf[n] = L'\n';
```
 
With `name = L"abcdefghijklm"`, glibc returns -1 and the error path runs. LLVM libc returns 18, and AddressSanitizer reports:
 
```
ERROR: AddressSanitizer: stack-buffer-overflow
WRITE of size 4 at ... thread T0
  [32, 96) 'buf' <== Memory access at offset 104 overflows this variable
```
 
### Cause
 
Both functions use `printf_core::make_drop_overflow_writer` and then return `ret_val.value()`, the number of characters the format produced. The truncation case is never turned into an error:
 
- `libc/src/wchar/swprintf.cpp` lines 43 and 67
- `libc/src/wchar/vswprintf.cpp` lines 40 and 64
The unit tests encode the `snprintf` semantics, so CI passes:
 
- `libc/test/src/wchar/swprintf_test.cpp` lines 3101, 3105, 3109, 3113 and 3126. For example, `swprintf(buff, 5, L"%s", "1234567890")` is expected to return 10. The `nullptr, 0` case carries the comment "passing null as the output pointer is allowed as long as bufsz is 0", which is `snprintf`'s rule (C 7.21.6.5); the standard has no such provision for `swprintf`.
- `libc/test/src/wchar/vswprintf_test.cpp` lines 100, 104, 108, 112 and 125.
### Suggested fix
 
After formatting, if the produced length is `>= bufsz` (this includes `bufsz == 0`), set `errno` (glibc uses `E2BIG`) and return -1. Keep writing the truncated, null-terminated output when `bufsz > 0`, as now. Then update the test expectations above to expect a negative result.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to