Skip to content

Commit d9b5fe3

Browse files
committed
evrpc: proper NULL checks (API function return value added)
Note, that in order to do this evrpc_hook_add_meta() should have return value, so this is a minor ABI change, which should not affect C ABI, but still worth to mention. Anyway this will be done in 2.2 release and unlikely RPC subsystem is popular.
1 parent 05ed7c8 commit d9b5fe3

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

evrpc-internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct evrpc_hook_meta {
118118
};
119119

120120
/* allows association of meta data with a request */
121-
static void evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
121+
static int evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
122122
struct evhttp_connection *evcon);
123123

124124
/* creates a new meta data store */

evrpc.c

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,12 @@ evrpc_add_hook(void *vbase,
134134
break;
135135
default:
136136
EVUTIL_ASSERT(hook_type == EVRPC_INPUT || hook_type == EVRPC_OUTPUT);
137+
return NULL;
137138
}
138139

139140
hook = mm_calloc(1, sizeof(struct evrpc_hook));
140-
EVUTIL_ASSERT(hook != NULL);
141+
if (!hook)
142+
return NULL;
141143

142144
hook->process = cb;
143145
hook->process_arg = cb_arg;
@@ -302,7 +304,9 @@ evrpc_request_cb(struct evhttp_request *req, void *arg)
302304
if (TAILQ_FIRST(&rpc->base->input_hooks) != NULL) {
303305
int hook_res;
304306

305-
evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
307+
int err = evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
308+
if (err)
309+
goto error;
306310

307311
/*
308312
* allow hooks to modify the outgoing request
@@ -427,7 +431,9 @@ evrpc_request_done(struct evrpc_req_generic *rpc_state)
427431
if (TAILQ_FIRST(&rpc->base->output_hooks) != NULL) {
428432
int hook_res;
429433

430-
evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
434+
int err = evrpc_hook_associate_meta_(&rpc_state->hook_meta, req->evcon);
435+
if (err)
436+
goto error;
431437

432438
/* do hook based tweaks to the request */
433439
hook_res = evrpc_process_hooks(&rpc->base->output_hooks,
@@ -678,7 +684,9 @@ evrpc_schedule_request(struct evhttp_connection *connection,
678684
if (TAILQ_FIRST(&pool->output_hooks) != NULL) {
679685
int hook_res;
680686

681-
evrpc_hook_associate_meta_(&ctx->hook_meta, connection);
687+
int err = evrpc_hook_associate_meta_(&ctx->hook_meta, connection);
688+
if (err)
689+
goto error;
682690

683691
/* apply hooks to the outgoing request */
684692
hook_res = evrpc_process_hooks(&pool->output_hooks,
@@ -875,7 +883,9 @@ evrpc_reply_done(struct evhttp_request *req, void *arg)
875883
}
876884

877885
if (TAILQ_FIRST(&pool->input_hooks) != NULL) {
878-
evrpc_hook_associate_meta_(&ctx->hook_meta, ctx->evcon);
886+
int err = evrpc_hook_associate_meta_(&ctx->hook_meta, ctx->evcon);
887+
if (err)
888+
goto error;
879889

880890
/* apply hooks to the incoming request */
881891
hook_res = evrpc_process_hooks(&pool->input_hooks,
@@ -905,8 +915,11 @@ evrpc_reply_done(struct evhttp_request *req, void *arg)
905915
}
906916

907917
evrpc_reply_done_closure(ctx, hook_res);
908-
918+
return;
909919
/* http request is being freed by underlying layer */
920+
921+
error:
922+
evrpc_request_wrapper_free(ctx);
910923
}
911924

912925
static void
@@ -984,7 +997,9 @@ static void
984997
evrpc_meta_data_free(struct evrpc_meta_list *meta_data)
985998
{
986999
struct evrpc_meta *entry;
987-
EVUTIL_ASSERT(meta_data != NULL);
1000+
1001+
if (!meta_data)
1002+
return;
9881003

9891004
while ((entry = TAILQ_FIRST(meta_data)) != NULL) {
9901005
TAILQ_REMOVE(meta_data, entry, next);
@@ -999,22 +1014,26 @@ evrpc_hook_meta_new_(void)
9991014
{
10001015
struct evrpc_hook_meta *ctx;
10011016
ctx = mm_malloc(sizeof(struct evrpc_hook_meta));
1002-
EVUTIL_ASSERT(ctx != NULL);
1017+
if (!ctx)
1018+
return NULL;
10031019

10041020
TAILQ_INIT(&ctx->meta_data);
10051021
ctx->evcon = NULL;
10061022

10071023
return (ctx);
10081024
}
10091025

1010-
static void
1026+
static int
10111027
evrpc_hook_associate_meta_(struct evrpc_hook_meta **pctx,
10121028
struct evhttp_connection *evcon)
10131029
{
10141030
struct evrpc_hook_meta *ctx = *pctx;
10151031
if (ctx == NULL)
10161032
*pctx = ctx = evrpc_hook_meta_new_();
1033+
if (!ctx)
1034+
return 1;
10171035
ctx->evcon = evcon;
1036+
return 0;
10181037
}
10191038

10201039
static void
@@ -1025,7 +1044,7 @@ evrpc_hook_context_free_(struct evrpc_hook_meta *ctx)
10251044
}
10261045

10271046
/* Adds meta data */
1028-
void
1047+
int
10291048
evrpc_hook_add_meta(void *ctx, const char *key,
10301049
const void *data, size_t data_size)
10311050
{
@@ -1036,16 +1055,32 @@ evrpc_hook_add_meta(void *ctx, const char *key,
10361055
if ((store = req->hook_meta) == NULL)
10371056
store = req->hook_meta = evrpc_hook_meta_new_();
10381057

1058+
if (!store)
1059+
goto err;
1060+
10391061
meta = mm_malloc(sizeof(struct evrpc_meta));
1040-
EVUTIL_ASSERT(meta != NULL);
1062+
if (!meta)
1063+
goto err;
10411064
meta->key = mm_strdup(key);
1042-
EVUTIL_ASSERT(meta->key != NULL);
1065+
if (!meta->key)
1066+
goto err;
10431067
meta->data_size = data_size;
10441068
meta->data = mm_malloc(data_size);
1045-
EVUTIL_ASSERT(meta->data != NULL);
1069+
if (!meta->data)
1070+
goto err;
10461071
memcpy(meta->data, data, data_size);
10471072

10481073
TAILQ_INSERT_TAIL(&store->meta_data, meta, next);
1074+
return 0;
1075+
1076+
err:
1077+
evrpc_hook_context_free_(store);
1078+
if (meta) {
1079+
mm_free(meta->data);
1080+
mm_free(meta->key);
1081+
mm_free(meta);
1082+
}
1083+
return 1;
10491084
}
10501085

10511086
int

include/event2/rpc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,10 @@ int evrpc_resume_request(void *vbase, void *ctx, enum EVRPC_HOOK_RESULT res);
546546
* @param key a NUL-terminated c-string
547547
* @param data the data to be associated with the key
548548
* @param data_size the size of the data
549+
* @return 0 on success or -1 on failure (in case of memory allocation failures)
549550
*/
550551
EVENT2_EXPORT_SYMBOL
551-
void evrpc_hook_add_meta(void *ctx, const char *key,
552+
int evrpc_hook_add_meta(void *ctx, const char *key,
552553
const void *data, size_t data_size);
553554

554555
/** retrieves meta data previously associated

0 commit comments

Comments
 (0)