diff options
author | Nicolas Boichat <drinkcat@chromium.org> | 2018-06-27 14:53:17 +0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2018-06-28 07:06:45 -0700 |
commit | cacc1c8ac679b66876cf3e65c58f2bf874498050 (patch) | |
tree | 46a4dd00938bb027543309855bcc129e6eeca06b | |
parent | 02b12a8b42d92f35e574ca954c1fe553fbfacd2a (diff) | |
download | chrome-ec-cacc1c8ac679b66876cf3e65c58f2bf874498050.tar.gz |
common/printf: snprintf: Return number of bytes on success
As indicated in the man page:
"""
Upon successful return, these functions return the number of
characters printed (excluding the null byte used to end output to
strings).
"""
There are no users of the return value currently in the EC code,
but this matters when doing fuzzing, as libFuzzer calls
std::to_string, which expects the correct return value.
BRANCH=none
BUG=chromium:854975
TEST=make buildfuzztests -j && ASAN_OPTIONS="log_path=stderr" \
build/host/usb_pd_fuzz/usb_pd_fuzz.exe -jobs=10
actually creates 10 output files.
TEST=make run-utils_str -j
Change-Id: If6a040f690dd847f4c88c3b8566554afdfbabc32
Signed-off-by: Nicolas Boichat <drinkcat@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1116625
Reviewed-by: Vincent Palatin <vpalatin@chromium.org>
-rw-r--r-- | common/printf.c | 2 | ||||
-rw-r--r-- | test/utils_str.c | 10 |
2 files changed, 11 insertions, 1 deletions
diff --git a/common/printf.c b/common/printf.c index 1034058a50..37a0f42058 100644 --- a/common/printf.c +++ b/common/printf.c @@ -378,5 +378,5 @@ int vsnprintf(char *str, int size, const char *format, va_list args) /* Terminate string */ *ctx.str = '\0'; - return rv; + return (rv == EC_SUCCESS) ? (ctx.str - str) : -rv; } diff --git a/test/utils_str.c b/test/utils_str.c index 23890639e2..81c600bc03 100644 --- a/test/utils_str.c +++ b/test/utils_str.c @@ -8,6 +8,7 @@ #include "common.h" #include "console.h" #include "system.h" +#include "printf.h" #include "test_util.h" #include "timer.h" #include "util.h" @@ -141,6 +142,14 @@ static int test_atoi(void) (atoi("\t111") == 111)); } +static int test_snprintf(void) +{ + char buffer[32]; + + TEST_CHECK(snprintf(buffer, sizeof(buffer), "%u", 1234) == 4); + TEST_CHECK(strncmp(buffer, "1234", sizeof(buffer))); +} + void run_test(void) { test_reset(); @@ -157,6 +166,7 @@ void run_test(void) RUN_TEST(test_strcasecmp); RUN_TEST(test_strncasecmp); RUN_TEST(test_atoi); + RUN_TEST(test_snprintf); test_print_result(); } |