diff options
author | Heinrich Schuchardt <heinrich.schuchardt@canonical.com> | 2021-11-15 19:06:55 +0100 |
---|---|---|
committer | Heinrich Schuchardt <heinrich.schuchardt@canonical.com> | 2021-11-20 10:53:00 +0100 |
commit | 0c9e8bf2bb617e08d9e07f219fe2bcf6d6de0ab4 (patch) | |
tree | 45edaf67133dee5b076304f5f5beb0bdee8e8f05 | |
parent | ac7606af7d4a91d6bad0e5de51b72a513e6ae440 (diff) | |
download | u-boot-0c9e8bf2bb617e08d9e07f219fe2bcf6d6de0ab4.zip u-boot-0c9e8bf2bb617e08d9e07f219fe2bcf6d6de0ab4.tar.gz u-boot-0c9e8bf2bb617e08d9e07f219fe2bcf6d6de0ab4.tar.bz2 |
test: test truncation in snprintf()
Test that the return value of snprintf() is correct in the case of
truncation.
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
-rw-r--r-- | test/print_ut.c | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/test/print_ut.c b/test/print_ut.c index 152a8c3..7b2e7bb 100644 --- a/test/print_ut.c +++ b/test/print_ut.c @@ -31,6 +31,7 @@ static int print_guid(struct unit_test_state *uts) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; char str[40]; + int ret; sprintf(str, "%pUb", guid); ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str)); @@ -40,6 +41,9 @@ static int print_guid(struct unit_test_state *uts) ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str)); sprintf(str, "%pUL", guid); ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str)); + ret = snprintf(str, 4, "%pUL", guid); + ut_asserteq(0, str[3]); + ut_asserteq(36, ret); return 0; } @@ -349,6 +353,20 @@ static int print_itoa(struct unit_test_state *uts) } PRINT_TEST(print_itoa, 0); +static int snprint(struct unit_test_state *uts) +{ + char buf[10] = "xxxxxxxxx"; + int ret; + + ret = snprintf(buf, 4, "%s:%s", "abc", "def"); + ut_asserteq(0, buf[3]); + ut_asserteq(7, ret); + ret = snprintf(buf, 4, "%s:%d", "abc", 9999); + ut_asserteq(8, ret); + return 0; +} +PRINT_TEST(snprint, 0); + static int print_xtoa(struct unit_test_state *uts) { ut_asserteq_str("7f", simple_xtoa(127)); |