I don't think there is much ambiguity in the standard (at least when
compared to other areas). This is a glibc documentation issue
(the non-default _FORTIFY_SOURCE=2 mode is not standards-conforming).
size_t mbstowcs(wchar_t *restrict pwcs, const char *restrict s, size_t n);
According to my interpretation of the specification of mbstowcs (and wcstombs) in the C standard, these functions are not allowed to write
beyond a terminating 0. The question was discussed in comp.lang.c today.
So far, it seems everyone agrees.
However, Ubuntu does not. On Ubuntu 18.04 with all GCC versions I tried (5.5.0, 6.5.0, 7.5.0, 8.4.0), these functions will write up to n bytes,
i.e. beyond a 0 in the string.
On the other hand GCC and LLVM on Debian, and LLVM on Ubuntu will not
write after a terminating 0.
The GNU/Linux manual page states
"size_t mbstowcs(wchar_t *dest, const char *src, size_t n); […]
The programmer must ensure that there is room for at least n wide
characters at dest."
So clearly, people interpret the specification of these C functions differently.
I guess we need a proposal to make WG14 decide and clarify?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 462 |
Nodes: | 16 (2 / 14) |
Uptime: | 72:10:40 |
Calls: | 9,374 |
Calls today: | 1 |
Files: | 13,549 |
Messages: | 6,088,333 |