The printf(3) man page says "The functions snprintf() and vsnprintf() do
not write more than size bytes (including the terminating null byte
('\0')). If the output was truncated due to this limit, then the return
value is the number of characters (excluding the terminating null byte)
which would have been written to the final string if enough space had been
available." The last part of this spec (returning the number of
characters which would have been written to the string if enough space
had been available) was not implemented in minimal-printf. This PR
changes that by redirecting all the processed characters through the
"minimal_printf_putchar" helper function. This function will not write
to the buffer if there's no space left, but it will always increment the
number of written characters, in order to match the above description.
minimal_printf_putchar also contains checks for overflows, so these
checks are no longer needed in the rest of the code.
Other changes:
- In some cases, mbed_minimal_formatted_string didn't put the string
terminator in the output buffer. This PR ensures that this always
happens.
- Fixed a bug in printed hexadecimal numbers introduced by a previous
commit.
- Added buffer overflow tests for snprintf.
In the implementation, don't always display double hex digits when
printing with "%X". This is in line with the behaviour observed both
in mbed OS's printf (Newlib) and Linux's printf (glibc).
In the tests, always compare the baseline result with the result
returned by the minimal printf implementation, instead of comparing
with a constant value.
The minimal-printf implementation supports a number of length modifiers
(j, z and t) that are not supported by the native mbed OS libc
implementation. The compliance test has tests for these modifiers, which
means that it isn't possible to check the output of mbed-printf against
a known good implementation (libc's printf) when running on mbed OS.
This, in turn, can give the impression that the tests for these
modifiers pass, when that might not be the case. To address this issue,
this PR removes the tests for these modifiers in mbed OS.
This PR was created because some of the tests for these modifiers
actually fail in Linux, for example:
```
>>> Running case #3: 'printf %u'...
hhu: 0
hhu: 0
hhu: 255
hhu: 255
hu: 0
hu: 0
hu: 65535
hu: 65535
u: 0
u: 0
u: 4294967295
u: 4294967295
lu: 0
lu: 0
lu: 4294967295
lu: 4294967295
llu: 0
llu: 0
llu: 18446744073709551615
llu: 18446744073709551615
ju: 0
ju: 0
ju: 4294967295
ju: 18446744073709551615
:188::FAIL: Expected 7 Was 16
>>> 'printf %u': 0 passed, 1 failed with reason 'Assertion Failed'
```