diff --git a/lib/fko_funcs.c b/lib/fko_funcs.c index 6e12c5c3..8c75d67b 100644 --- a/lib/fko_funcs.c +++ b/lib/fko_funcs.c @@ -184,6 +184,7 @@ fko_new_with_data(fko_ctx_t *r_ctx, char *enc_msg, char *dec_key) if(res != FKO_SUCCESS) { fko_destroy(ctx); + *r_ctx = NULL; /* Make sure the caller ctx is null just in case */ return(res); } } diff --git a/lib/fko_state.h b/lib/fko_state.h index ea22eacc..dacc0e0e 100644 --- a/lib/fko_state.h +++ b/lib/fko_state.h @@ -80,7 +80,7 @@ typedef enum { /* Macros used for determining ctx initialization state. */ -#define CTX_INITIALIZED(ctx) (ctx->initval == FKO_CTX_INITIALIZED) +#define CTX_INITIALIZED(ctx) (ctx != NULL && ctx->initval == FKO_CTX_INITIALIZED) #endif /* FKO_STATE_H */ diff --git a/server/incoming_spa.c b/server/incoming_spa.c index ea45324c..5bdbf72a 100644 --- a/server/incoming_spa.c +++ b/server/incoming_spa.c @@ -33,7 +33,11 @@ int incoming_spa(fko_srv_options_t *opts) { - fko_ctx_t ctx; + /* Always a good idea to initialize ctx to null if it will be used + * repeatedly (especially when using fko_new_with_data(). + */ + fko_ctx_t ctx = NULL; + char *spa_ip_demark; char spa_msg_src_ip[16]; char spa_msg_remain[1024]; /* --DSS should not have arbitrary limit */ @@ -372,7 +376,9 @@ display_ctx(ctx); clean_and_bail: - fko_destroy(ctx); + if(ctx != NULL) + fko_destroy(ctx); + return(res); }