Skip to content

Commit

Permalink
kunit: Assign strings to 'const char*' in STREQ assertions
Browse files Browse the repository at this point in the history
Currently, the KUNIT_EXPECT_STREQ() and related macros assign both
string arguments to variables of their own type (via typeof()). This
seems to be to prevent the macro argument from being evaluated multiple
times.

However, this doesn't work if one of these is a fixed-length character
array, rather than a character pointer, as (for example) char[16] will
always allocate a new string.

By always using 'const char*' (the type strcmp expects), we're always
just taking a pointer to the string, which works even with character
arrays.

Signed-off-by: David Gow <[email protected]>
Reviewed-by: Daniel Latypov <[email protected]>
Reviewed-by: Brendan Higgins <[email protected]>
Signed-off-by: Shuah Khan <[email protected]>
  • Loading branch information
sulix authored and shuahkh committed Jun 23, 2021
1 parent 6e62dfa commit 3747b5c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -1128,8 +1128,8 @@ do { \
fmt, \
...) \
do { \
typeof(left) __left = (left); \
typeof(right) __right = (right); \
const char *__left = (left); \
const char *__right = (right); \
\
KUNIT_ASSERTION(test, \
strcmp(__left, __right) op 0, \
Expand Down

0 comments on commit 3747b5c

Please sign in to comment.