summaryrefslogtreecommitdiff
path: root/test/printf.c
diff options
context:
space:
mode:
authorEvan Green <evgreen@chromium.org>2019-08-02 14:32:24 -0700
committerCommit Bot <commit-bot@chromium.org>2019-10-05 00:47:44 +0000
commita1216326c5d58af300b7c6f24c8597a232ced131 (patch)
treed151d7b7eb59ce8213cfb450b2dfc7379f817590 /test/printf.c
parentb63e2a87a75dce8941d087c8736c5a35544ab3b0 (diff)
downloadchrome-ec-a1216326c5d58af300b7c6f24c8597a232ced131.tar.gz
printf: Convert %b to %pb
In order to turn on compile-time printf format checking, non-standard specifiers like %b (binary) must be removed. Convert that into %pb, which takes a pointer to a structure containing the value to print, and how many bits to print. Use the convenience macro BINARY_VALUE() to package these values up into a struct whose pointer is passed to printf(). Technically this is slightly more limited functionality than we used to support given all the possible flags, field width, and precision. However every existing instance in our codebase was using %0NNb, where NN is some number. If more variants are needed, the parameters structure can be expanded in the future. BUG=chromium:984041 TEST=make -j buildall BRANCH=None Change-Id: I8ef995dcf97af688fbca98ab6ff59b84092b69e3 Signed-off-by: Evan Green <evgreen@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1733100 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'test/printf.c')
-rw-r--r--test/printf.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/test/printf.c b/test/printf.c
index c758d9bccb..97bb00c4be 100644
--- a/test/printf.c
+++ b/test/printf.c
@@ -186,30 +186,38 @@ test_static int test_vsnprintf_int(void)
T(expect_success("5e", "%x", 0X5E));
T(expect_success("5E", "%X", 0X5E));
- T(expect_success("0", "%b", 0));
- T(expect_success("1011110", "%b", 0X5E));
-
return EC_SUCCESS;
}
test_static int test_vsnprintf_pointers(void)
{
void *ptr = (void *)0x55005E00;
+ unsigned int val = 0;
T(expect_success("55005e00", "%pP", ptr));
T(expect_success(err_str, "%P", ptr));
/* %p by itself is invalid */
T(expect(EC_ERROR_INVAL, NO_BYTES_TOUCHED,
- /* given -1 as output size limit */
false, -1, "%p"));
/* %p with an unknown suffix is invalid */
T(expect(EC_ERROR_INVAL, NO_BYTES_TOUCHED,
- /* given -1 as output size limit */
false, -1, "%p "));
/* %p with an unknown suffix is invalid */
T(expect(EC_ERROR_INVAL, NO_BYTES_TOUCHED,
- /* given -1 as output size limit */
false, -1, "%pQ"));
+
+ /* Test %pb, binary format */
+ T(expect_success("0", "%pb", BINARY_VALUE(val, 0)));
+ val = 0x5E;
+ T(expect_success("1011110", "%pb", BINARY_VALUE(val, 0)));
+ T(expect_success("0000000001011110", "%pb", BINARY_VALUE(val, 16)));
+ val = 0x12345678;
+ T(expect_success("10010001101000101011001111000", "%pb",
+ BINARY_VALUE(val, 0)));
+ val = 0xFEDCBA90;
+ /* Test a number that makes the longest string possible */
+ T(expect_success("11111110110111001011101010010000", "%pb",
+ BINARY_VALUE(val, 0)));
return EC_SUCCESS;
}