Skip to content

Commit

Permalink
pw_assert: Verify PW_CHECK message arguments in PW_CRASH and comparisons
Browse files Browse the repository at this point in the history
Verify the message arguments in PW_CRASH and the PW_CHECK_* comparison
macros, in addition to the standard PW_CHECK macro.

Change-Id: I71c59acdfb558052a2b533e2662a286e8e177ece
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/245133
Commit-Queue: Auto-Submit <[email protected]>
Docs-Not-Needed: Wyatt Hepler <[email protected]>
Reviewed-by: Ben Lawson <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Lint: Lint 🤖 <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Oct 28, 2024
1 parent 752146e commit 03eff3c
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions pw_assert/public/pw_assert/internal/check_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
#include "pw_preprocessor/compiler.h"

// PW_CRASH - Crash the system, with a message.
#define PW_CRASH PW_HANDLE_CRASH
#define PW_CRASH(...) \
do { \
if (0) { /* Check args but don't execute to avoid multiple evaluation */ \
_pw_assert_CheckMessageArguments(" " __VA_ARGS__); \
} \
PW_HANDLE_CRASH(__VA_ARGS__); \
} while (0)

// PW_CHECK - If condition evaluates to false, crash. Message optional.
#define PW_CHECK(condition, ...) \
Expand Down Expand Up @@ -237,34 +243,40 @@ constexpr T ConvertToType(const U& value) {
// hitting the CHECK backend. This controls whether evaluated values are
// captured.
#if PW_ASSERT_CAPTURE_VALUES
#define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
...) \
\
_pw_assert_ConditionCannotContainThePercentCharacter( \
arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
#define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
...) \
\
_pw_assert_ConditionCannotContainThePercentCharacter( \
arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
if (0) { /* Check args but don't execute to avoid multiple evaluation */ \
_pw_assert_CheckMessageArguments(" " __VA_ARGS__); \
} \
PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
__VA_ARGS__)
#else
#define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
...) \
_pw_assert_ConditionCannotContainThePercentCharacter( \
arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
PW_HANDLE_ASSERT_FAILURE(arg_a_str " " comparison_op_str " " arg_b_str, \
#define _PW_CHECK_BINARY_ARG_HANDLER(arg_a_str, \
arg_a_val, \
comparison_op_str, \
arg_b_str, \
arg_b_val, \
type_fmt, \
...) \
_pw_assert_ConditionCannotContainThePercentCharacter( \
arg_a_str arg_b_str); /* cannot use '%' in PW_CHECK conditions */ \
if (0) { /* Check args but don't execute to avoid multiple evaluation */ \
_pw_assert_CheckMessageArguments(" " __VA_ARGS__); \
} \
PW_HANDLE_ASSERT_FAILURE(arg_a_str " " comparison_op_str " " arg_b_str, \
__VA_ARGS__)
#endif // PW_ASSERT_CAPTURE_VALUES

Expand Down

0 comments on commit 03eff3c

Please sign in to comment.