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: | 715 |
| Nodes: | 16 (0 / 16) |
| Uptime: | 163:30:28 |
| Calls: | 12,095 |
| Calls today: | 3 |
| Files: | 15,000 |
| Messages: | 6,517,787 |