Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
Add minimal printf #11051
This PR imports https://github.com/ARMmbed/minimal-printf into mbed-os.
Minimal-printf supports floating point and stream printing by default. It has the following restrictions:
Pelion Device Management Client example compiled with GCC_ARM, release profile compared to current master (with std printf):
Pull request type
To replace the standard implementation of the printf functions with the ones from the minimal-printf library, the custom
Use mbed_app.json to enable floating points. Automatically ignore flags and width specifiers.
This commit adds mostly integer (and buffer) overflow checks for the current buffer index (`result` variable).
- Improved comments to explain the checks on 'result'. - Check for non-NULL format specifier.
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' ```
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 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.
I have squashed all the commits done for the integration in mbed-os and review comments into the last two commits. All previous commits come from the original library and it would beb useful to keep them.
All comments have now been addressed.
Run 5 seemed to have failed due to a license issue
Job restarted and now successful.
My issue is that this conflates two independent things - "minimal printf" and "simplified stdin/out retargetting", and attempts to short-circuit the retargetting in printf itself, without actually removing it.
This PR fails with a very simple test program:
The minimal printf should not be trying to work around the retargetting - it should just use
If you want to simplify the retargetting, do it properly in a separate PR - that could make it so that
And the two options - simplified printf and simplified retargetting should be totally independent of each other. There's no reason to link them.