diff options
| author | jvoisin | 2026-04-30 17:42:29 +0200 |
|---|---|---|
| committer | jvoisin | 2026-04-30 17:42:29 +0200 |
| commit | f9239e2c0f0be9856322727887a45333683940a6 (patch) | |
| tree | 714b611965666c4072fef6218e7a794dff1884cb /include/wchar.h | |
| parent | 6040b4a27409968c764353a98c45d972cfd89a8a (diff) | |
Fix a bug in wcsnrtombs
__d is a char * destination buffer, so __b is already the byte capacity.
Dividing by sizeof(wchar_t) makes no sense here, it was likely copy-pasted
from mbsnrtowcs (where the destination is wchar_t *). The first branch also
fails to limit __n (the byte write cap) to __b, so overflows are possible when
a wide character produces multi-byte output. The second branch (else) correctly
limits __n to __b.
This commit replaces the broken two-branch logic with the simple correct
pattern matching wcsrtombs, and adds two tests two prove that nothing broke.
Diffstat (limited to 'include/wchar.h')
| -rw-r--r-- | include/wchar.h | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/include/wchar.h b/include/wchar.h index a840f1a..0842115 100644 --- a/include/wchar.h +++ b/include/wchar.h | |||
| @@ -190,16 +190,9 @@ _FORTIFY_FN(wcsnrtombs) size_t wcsnrtombs(char * _FORTIFY_POS0 __d, | |||
| 190 | size_t __b = __bos(__d, 0); | 190 | size_t __b = __bos(__d, 0); |
| 191 | size_t __r; | 191 | size_t __r; |
| 192 | 192 | ||
| 193 | if (__wn > __n / sizeof(wchar_t)) { | 193 | __r = __orig_wcsnrtombs(__d, __s, __wn, __n > __b ? __b : __n, __st); |
| 194 | __b /= sizeof(wchar_t); | 194 | if (__b < __n && __d && *__s && __r != (size_t)-1) |
| 195 | __r = __orig_wcsnrtombs(__d, __s, __wn > __b ? __b : __wn, __n, __st); | 195 | __builtin_trap(); |
| 196 | if (__b < __wn && __d && *__s && __r != (size_t)-1) | ||
| 197 | __builtin_trap(); | ||
| 198 | } else { | ||
| 199 | __r = __orig_wcsnrtombs(__d, __s, __wn, __n > __b ? __b : __n, __st); | ||
| 200 | if (__b < __n && __d && *__s && __r != (size_t)-1) | ||
| 201 | __builtin_trap(); | ||
| 202 | } | ||
| 203 | return __r; | 196 | return __r; |
| 204 | } | 197 | } |
| 205 | #endif | 198 | #endif |
