Skip to content

Commit

Permalink
smf: fix null pointer for unitized state machine
Browse files Browse the repository at this point in the history
Before calling smf_set_initial, setting a state by
smf_set_state crashes with a null pointer exception.

Signed-off-by: Jan Kohlwey <[email protected]>
  • Loading branch information
Janbk committed Oct 23, 2024
1 parent f087f52 commit d7af94d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/smf/smf.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ static bool smf_execute_all_exit_actions(struct smf_ctx *const ctx, const struct
struct internal_ctx *const internal = (void *)&ctx->internal;

for (const struct smf_state *to_execute = ctx->current;
to_execute != NULL && to_execute != topmost;
to_execute = to_execute->parent) {
to_execute != NULL && to_execute != topmost; to_execute = to_execute->parent) {
if (to_execute->exit) {
to_execute->exit(ctx);

Expand Down Expand Up @@ -342,7 +341,7 @@ void smf_set_state(struct smf_ctx *const ctx, const struct smf_state *new_state)
}
#else
/* Flat state machines have a very simple transition: */
if (ctx->current->exit) {
if (ctx->current != NULL && ctx->current->exit) {
internal->is_exit = true;
ctx->current->exit(ctx);
/* No need to continue if terminate was set in the exit action */
Expand Down Expand Up @@ -389,6 +388,11 @@ int32_t smf_run_state(struct smf_ctx *const ctx)
return ctx->terminate_val;
}

/* No current state */
if (ctx->current == NULL) {
return -EINVAL;
}

#ifdef CONFIG_SMF_ANCESTOR_SUPPORT
ctx->executing = ctx->current;
#endif
Expand Down

0 comments on commit d7af94d

Please sign in to comment.