Skip to content

Commit 082ca85

Browse files
tristan-googlecarlescufi
authored andcommitted
ztest: bug: Add friendly name helper function for all phases
The `handle_signal()` function in the new ztest API (`ztest_new.c`) uses an array of strings to lookup a friendly name for each test phase, but the array only has three elements despite there being six test phases. This can lead to an out-of-bounds issue. Replace the array with a helper function and switch statement. Signed-off-by: Tristan Honscheid <[email protected]>
1 parent 66e8eae commit 082ca85

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

subsys/testsuite/ztest/src/ztest_new.c

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,25 @@ static struct k_thread ztest_thread;
2323

2424
/* ZTEST_DMEM and ZTEST_BMEM are used for the application shared memory test */
2525

26-
ZTEST_DMEM enum {
26+
/**
27+
* @brief Each enum member represents a distinct phase of execution for the
28+
* test binary. TEST_PHASE_FRAMEWORK is active when internal ztest code
29+
* is executing; the rest refer to corresponding phases of user test
30+
* code.
31+
*/
32+
enum ztest_phase {
2733
TEST_PHASE_SETUP,
2834
TEST_PHASE_BEFORE,
2935
TEST_PHASE_TEST,
3036
TEST_PHASE_AFTER,
3137
TEST_PHASE_TEARDOWN,
3238
TEST_PHASE_FRAMEWORK
33-
} phase = TEST_PHASE_FRAMEWORK;
39+
};
40+
41+
/**
42+
* @brief Tracks the current phase that ztest is operating in.
43+
*/
44+
ZTEST_DMEM enum ztest_phase phase = TEST_PHASE_FRAMEWORK;
3445

3546
static ZTEST_BMEM int test_status;
3647

@@ -248,22 +259,42 @@ void ztest_test_pass(void)
248259
longjmp(test_pass, 1);
249260
}
250261

251-
static void handle_signal(int sig)
262+
/**
263+
* @brief Get a friendly name string for a given test phrase.
264+
*
265+
* @param phase an enum ztest_phase value describing the desired test phase
266+
* @returns a string name for `phase`
267+
*/
268+
static inline const char *get_friendly_phase_name(enum ztest_phase phase)
252269
{
253-
static const char *const phase_str[] = {
254-
"setup",
255-
"unit test",
256-
"teardown",
257-
};
270+
switch (phase) {
271+
case TEST_PHASE_SETUP:
272+
return "setup";
273+
case TEST_PHASE_BEFORE:
274+
return "before";
275+
case TEST_PHASE_TEST:
276+
return "test";
277+
case TEST_PHASE_AFTER:
278+
return "after";
279+
case TEST_PHASE_TEARDOWN:
280+
return "teardown";
281+
case TEST_PHASE_FRAMEWORK:
282+
return "framework";
283+
default:
284+
return "(unknown)";
285+
}
286+
}
258287

288+
static void handle_signal(int sig)
289+
{
259290
PRINT(" %s", strsignal(sig));
260291
switch (phase) {
261292
case TEST_PHASE_SETUP:
262293
case TEST_PHASE_BEFORE:
263294
case TEST_PHASE_TEST:
264295
case TEST_PHASE_AFTER:
265296
case TEST_PHASE_TEARDOWN:
266-
PRINT(" at %s function\n", phase_str[phase]);
297+
PRINT(" at %s function\n", get_friendly_phase_name(phase));
267298
longjmp(test_fail, 1);
268299
case TEST_PHASE_FRAMEWORK:
269300
PRINT("\n");

0 commit comments

Comments
 (0)