Skip to content

Commit

Permalink
pw_assert: Verify PW_CHECK message arguments in the API
Browse files Browse the repository at this point in the history
Check that the arguments are valid, regardless of the backend. This also
ensures that the compiler considers arguments as "used", even if the
backend does not use them.

Change-Id: I0fa70f59c95a3ac3b29973b275baaeeec237ff3f
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/244744
Docs-Not-Needed: Wyatt Hepler <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Lint: Lint 🤖 <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Reviewed-by: Ben Lawson <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Oct 28, 2024
1 parent 8153a8a commit 673e56a
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions pw_assert/public/pw_assert/internal/check_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
#define PW_CRASH PW_HANDLE_CRASH

// PW_CHECK - If condition evaluates to false, crash. Message optional.
#define PW_CHECK(condition, ...) \
do { \
if (!(condition)) { \
_pw_assert_ConditionCannotContainThePercentCharacter( \
#condition); /* cannot use '%' in PW_CHECK conditions */ \
PW_HANDLE_ASSERT_FAILURE(#condition, "" __VA_ARGS__); \
} \
#define PW_CHECK(condition, ...) \
do { \
if (!(condition)) { \
_pw_assert_ConditionCannotContainThePercentCharacter( \
#condition); /* 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(#condition, "" __VA_ARGS__); \
} \
} while (0)

#define PW_DCHECK(...) \
Expand Down Expand Up @@ -309,3 +312,13 @@ static inline void _pw_assert_ConditionCannotContainThePercentCharacter(
const char* format, ...) {
(void)format;
}

// Empty function for checking that arguments match the format string. This
// function also ensures arguments are considered "used" in PW_CHECK, even if
// the backend does not use them.
static inline void _pw_assert_CheckMessageArguments(const char* format, ...)
PW_PRINTF_FORMAT(1, 2);

static inline void _pw_assert_CheckMessageArguments(const char* format, ...) {
(void)format;
}

0 comments on commit 673e56a

Please sign in to comment.