Skip to content

Commit 410310e

Browse files
committed
* MDF [core/sp/supplemental] Handle the case when nni_zalloc fails
1 parent 4d9d374 commit 410310e

File tree

9 files changed

+155
-3
lines changed

9 files changed

+155
-3
lines changed

src/core/aio.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,10 @@ nni_aio_sys_init(void)
815815

816816
nni_aio_expire_q_list =
817817
nni_zalloc(sizeof(nni_aio_expire_q *) * num_thr);
818+
if (nni_aio_expire_q_list == NULL) {
819+
return NNG_ENOMEM;
820+
}
821+
818822
nni_aio_expire_q_cnt = num_thr;
819823
for (int i = 0; i < num_thr; i++) {
820824
nni_aio_expire_q *eq;
@@ -828,4 +832,4 @@ nni_aio_sys_init(void)
828832
return (0);
829833
}
830834

831-
// NANOMQ APIs
835+
// NANOMQ APIs

src/sp/protocol/mqtt/mqtt_parser.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1850,6 +1850,11 @@ bool
18501850
topic_filtern(const char *origin, const char *input, size_t n)
18511851
{
18521852
char *buff = nni_zalloc(n + 1);
1853+
if (buff == NULL) {
1854+
log_error("Cannot allocate memory");
1855+
return false;
1856+
}
1857+
18531858
strncpy(buff, input, n);
18541859
bool res = false;
18551860
size_t len = strlen(origin);

src/supplemental/mqtt/mqtt_codec.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3465,6 +3465,10 @@ property *
34653465
property_alloc(void)
34663466
{
34673467
property *p = nni_zalloc(sizeof(property));
3468+
if (p == NULL) {
3469+
log_error("Cannot allocate memory");
3470+
return NULL;
3471+
}
34683472
p->next = NULL;
34693473
return p;
34703474
}
@@ -4230,6 +4234,10 @@ nni_mqtt_msgack_encode(nng_msg *msg, uint16_t packet_id, uint8_t reason_code,
42304234
property *prop, uint8_t proto_ver)
42314235
{
42324236
uint8_t *rbuf = nni_zalloc(2);
4237+
if (rbuf == NULL) {
4238+
log_error("Cannot allocate memory");
4239+
return MQTT_ERR_NOMEM;
4240+
}
42334241
NNI_PUT16(rbuf, packet_id);
42344242
nni_msg_clear(msg);
42354243
nni_msg_append(msg, rbuf, 2);

src/supplemental/mqtt/mqtt_qos_db.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,10 @@ nni_mqtt_qos_db_find_retain(sqlite3 *db, const char *topic_pattern)
794794

795795
size_t full_sql_sz = strlen(sql) + strlen(topic_str) + 10;
796796
char * full_sql = nni_zalloc(full_sql_sz);
797+
if (full_sql == NULL) {
798+
log_error("Cannot allocate memory");
799+
return NULL;
800+
}
797801
snprintf(full_sql, full_sql_sz, "%s '%s'", sql, topic_str);
798802

799803
sqlite3_stmt *stmt;
@@ -1265,6 +1269,10 @@ nni_mqtt_msg_serialize(nni_msg *msg, size_t *out_len, uint8_t proto_ver)
12651269
// time: nni_time(uint64)
12661270
// aio: address value
12671271
uint8_t *bytes = nng_zalloc(len);
1272+
if (bytes == NULL) {
1273+
log_error("Cannot allocate memory");
1274+
return NULL;
1275+
}
12681276

12691277
struct pos_buf buf = { .curpos = &bytes[0], .endpos = &bytes[len] };
12701278

@@ -1372,6 +1380,10 @@ nni_msg_serialize(nni_msg *msg, size_t *out_len)
13721380
// body: body_len(uint32) + body(body_len)
13731381
// time: nni_time(uint64)
13741382
uint8_t *bytes = nng_zalloc(len);
1383+
if (bytes == NULL) {
1384+
log_error("Cannot allocate memory");
1385+
return NULL;
1386+
}
13751387

13761388
struct pos_buf buf = { .curpos = &bytes[0], .endpos = &bytes[len] };
13771389

src/supplemental/nanolib/acl_conf.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ acl_parse_str_array_item(cJSON *obj, const char *key, acl_rule_ct *content)
3232
content->type = ACL_RULE_STRING_ARRAY;
3333
content->count = size;
3434
content->value.str_array = nni_zalloc(size * sizeof(char *));
35+
if (content->value.str_array == NULL) {
36+
log_error("Cannot allocate memory");
37+
return false;
38+
}
39+
3540
for (int i = 0; i < size; i++) {
3641
cJSON *item = cJSON_GetArrayItem(array, i);
3742
if (cJSON_IsString(item)) {
@@ -48,6 +53,10 @@ bool
4853
acl_parse_json_rule(cJSON *obj, size_t id, acl_rule **rule)
4954
{
5055
acl_rule *r = nni_zalloc(sizeof(acl_rule));
56+
if (r == NULL) {
57+
log_error("Cannot allocate memory");
58+
return false;
59+
}
5160

5261
r->id = id;
5362
r->action = ACL_ALL;
@@ -90,6 +99,11 @@ acl_parse_json_rule(cJSON *obj, size_t id, acl_rule **rule)
9099
int size = cJSON_GetArraySize(topics);
91100
r->topic_count = size;
92101
r->topics = nni_zalloc(size * sizeof(char *));
102+
if (r->topics == NULL) {
103+
log_error("Cannot allocate memory");
104+
goto err;
105+
}
106+
93107
for (int i = 0; i < size; i++) {
94108
cJSON *item = cJSON_GetArrayItem(topics, i);
95109
if (cJSON_IsString(item)) {
@@ -133,11 +147,21 @@ acl_parse_json_rule(cJSON *obj, size_t id, acl_rule **rule)
133147
int size = cJSON_GetArraySize(op);
134148
acl_sub_rules_array *rule_list = &r->rule_ct.array;
135149
rule_list->rules = nni_zalloc(sizeof(acl_sub_rule *) * size);
150+
if (rule_list->rules == NULL) {
151+
log_error("Cannot allocate memory");
152+
goto err;
153+
}
154+
136155
rule_list->count = 0;
137156
for (int i = 0; i < size; i++) {
138157
cJSON * sub_item = cJSON_GetArrayItem(op, i);
139158
acl_sub_rule *sub_rule =
140159
nni_zalloc(sizeof(acl_sub_rule));
160+
if (sub_rule == NULL) {
161+
log_error("Cannot allocate memory");
162+
nni_free(rule_list->rules, sizeof(acl_sub_rule *) * size);
163+
goto err;
164+
}
141165

142166
if (acl_parse_str_item(
143167
sub_item, "clientid", &sub_rule->rule_ct)) {
@@ -166,6 +190,10 @@ acl_parse_json_rule(cJSON *obj, size_t id, acl_rule **rule)
166190
return true;
167191

168192
err:
193+
if (r->topics != NULL) {
194+
nni_free(r->topics, cJSON_GetArraySize(topics) * sizeof(char *));
195+
}
196+
169197
nni_free(r, sizeof(acl_rule));
170198
return false;
171199
}
@@ -402,4 +430,4 @@ print_acl_conf(conf_acl *acl)
402430
log_info("");
403431
}
404432
}
405-
}
433+
}

src/supplemental/nanolib/conf.c

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ conf_update_var2(const char *fpath, const char *key1, const char *key2,
154154
{
155155
size_t sz = strlen(key1) + strlen(key2) + strlen(key3) + 2;
156156
char * key = nni_zalloc(sz);
157+
if (key == NULL) {
158+
log_error("Cannot allocate memory");
159+
return;
160+
}
157161
snprintf(key, sz, "%s%s%s", key1, key2, key3);
158162
conf_update_var(fpath, key, type, var);
159163
nng_free(key, sz);
@@ -232,6 +236,10 @@ conf_update2(const char *fpath, const char *key1, const char *key2,
232236
{
233237
size_t sz = strlen(key1) + strlen(key2) + strlen(key3) + 2;
234238
char * key = nni_zalloc(sz);
239+
if (key == NULL) {
240+
log_error("Cannot allocate memory");
241+
return;
242+
}
235243
snprintf(key, sz, "%s%s%s", key1, key2, key3);
236244
conf_update(fpath, key, value);
237245
nng_free(key, sz);
@@ -258,8 +266,19 @@ get_conf_value(char *line, size_t len, const char *key)
258266
}
259267

260268
char *prefix = nni_zalloc(len);
261-
char *trim = strtrim_head_tail(line, len);
269+
if (prefix == NULL) {
270+
log_error("Cannot allocate memory");
271+
return NULL;
272+
}
273+
262274
char *value = nni_zalloc(len);
275+
if (value == NULL) {
276+
nni_free(prefix, len);
277+
log_error("Cannot allocate memory");
278+
return NULL;
279+
}
280+
281+
char *trim = strtrim_head_tail(line, len);
263282
int match = sscanf(trim, "%[^=]=%[^\n]s", prefix, value);
264283
char *res = NULL;
265284
nni_strfree(trim);
@@ -281,6 +300,10 @@ get_conf_value_with_prefix(
281300
{
282301
size_t sz = strlen(prefix) + strlen(key) + 2;
283302
char * str = nni_zalloc(sz);
303+
if (str == NULL) {
304+
log_error("Cannot allocate memory");
305+
return NULL;
306+
}
284307
snprintf(str, sz, "%s%s", prefix, key);
285308
char *value = get_conf_value(line, len, str);
286309
free(str);
@@ -296,6 +319,10 @@ get_conf_value_with_prefix2(char *line, size_t len, const char *prefix,
296319
size_t sz = prefix_sz + name_sz + strlen(key) + 2;
297320

298321
char *str = nni_zalloc(sz);
322+
if (str == NULL) {
323+
log_error("Cannot allocate memory");
324+
return NULL;
325+
}
299326
snprintf(str, sz, "%s%s%s", prefix, name ? name : "", key ? key : "");
300327
char *value = get_conf_value(line, len, str);
301328
free(str);
@@ -1750,6 +1777,10 @@ conf_rule_fdb_parse(conf_rule *cr, char *path)
17501777
size_t sz = 0;
17511778
FILE * fp;
17521779
rule_key *rk = (rule_key *) nni_zalloc(sizeof(rule_key));
1780+
if (rk == NULL) {
1781+
log_error("Cannot allocate memory");
1782+
return;
1783+
}
17531784
memset(rk, 0, sizeof(rule_key));
17541785

17551786
if (NULL == (fp = fopen(path, "r"))) {
@@ -2464,6 +2495,10 @@ get_bridge_group_names(const char *path, const char *prefix, size_t *count)
24642495

24652496
size_t len = strlen(prefix) + 34;
24662497
char * pattern = nni_zalloc(len);
2498+
if (pattern == NULL) {
2499+
log_error("Cannot allocate memory");
2500+
return NULL;
2501+
}
24672502
snprintf(
24682503
pattern, len, "%sbridge.mqtt.%%[^.].%%*[^=]=%%*[^\n]", prefix);
24692504

@@ -2699,6 +2734,10 @@ conf_bridge_content_parse(conf *nanomq_conf, conf_bridge *bridge,
26992734
// 1. parse sqlite config from nanomq_bridge.conf
27002735
size_t sz = strlen(prefix) + 15;
27012736
char * key = nni_zalloc(sz);
2737+
if (key == NULL) {
2738+
log_error("Cannot allocate memory");
2739+
return;
2740+
}
27022741
snprintf(key, sz, "%sbridge.sqlite", prefix);
27032742
conf_sqlite_parse(&bridge->sqlite, path, "bridge.sqlite");
27042743
nni_strfree(key);
@@ -3260,6 +3299,10 @@ conf_parse_http_headers(
32603299

32613300
size_t len = strlen(key_prefix) + 23;
32623301
char * pattern = nni_zalloc(len);
3302+
if (pattern == NULL) {
3303+
log_error("Cannot allocate memory");
3304+
return NULL;
3305+
}
32633306
snprintf(pattern, len, "%s.headers.%%[^=]=%%[^\n]", key_prefix);
32643307

32653308
size_t header_count = 0;

src/supplemental/nanolib/log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ file_rotation(FILE *fp, conf_log *config)
229229

230230
size_t log_name_len = strlen(config->abs_path) + 20;
231231
char * log_name = nni_zalloc(log_name_len);
232+
if (log_name == NULL) {
233+
return;
234+
}
232235
snprintf(
233236
log_name, log_name_len, "%s.%lu", config->file, index);
234237
char *backup_log_path =

src/supplemental/nanolib/mqtt_db.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,27 @@ topic_parse(char *topic)
135135

136136
// Here we will get (cnt + 1) memory, one for NULL end
137137
char **topic_queue = (char **) nni_zalloc(sizeof(char *) * (cnt + 1));
138+
if (topic_queue == NULL) {
139+
log_error("Cannot allocate memory");
140+
return NULL;
141+
}
138142

139143
while ((pos = strchr(b_pos, '/')) != NULL) {
140144

141145
len = pos - b_pos + 1;
142146
topic_queue[row] = (char *) nni_zalloc(sizeof(char) * len);
147+
if (topic_queue[row] == NULL) {
148+
for (int i = 0; i < row; i++) {
149+
/*
150+
* Use free() to release memory, because of the len
151+
* is too difficult to obtain.
152+
*/
153+
free(topic_queue[i]);
154+
}
155+
nni_free(topic_queue, sizeof(char *) * (cnt + 1));
156+
log_error("Cannot allocate memory");
157+
return NULL;
158+
}
143159
memcpy(topic_queue[row], b_pos, (len - 1));
144160
topic_queue[row][len - 1] = '\0';
145161
b_pos = pos + 1;
@@ -149,6 +165,19 @@ topic_parse(char *topic)
149165
len = strlen(b_pos);
150166

151167
topic_queue[row] = (char *) nni_zalloc(sizeof(char) * (len + 1));
168+
if (topic_queue[row] == NULL) {
169+
for (int i = 0; i < row; i++) {
170+
/*
171+
* Use free() to release memory, because of the len
172+
* is too difficult to obtain.
173+
*/
174+
free(topic_queue[i]);
175+
}
176+
nni_free(topic_queue, sizeof(char *) * (cnt + 1));
177+
log_error("Cannot allocate memory");
178+
return NULL;
179+
}
180+
152181
memcpy(topic_queue[row], b_pos, (len));
153182
topic_queue[row][len] = '\0';
154183
topic_queue[++row] = NULL;
@@ -199,6 +228,12 @@ dbtree_get_tree(dbtree *db, void *(*cb)(uint32_t pipe_id))
199228
dbtree_info **ret_line_ping = NULL;
200229
for (size_t i = 0; i < cvector_size(nodes); i++) {
201230
dbtree_info *vn = nni_zalloc(sizeof(dbtree_info));
231+
if (vn == NULL) {
232+
/* All vn are pushed in ret_line_ping vector */
233+
cvector_free(ret_line_ping);
234+
log_error("Cannot allocate memory");
235+
return NULL;
236+
}
202237
vn->clients = NULL;
203238
if (cb) {
204239
for (size_t j = 0;
@@ -228,6 +263,12 @@ dbtree_get_tree(dbtree *db, void *(*cb)(uint32_t pipe_id))
228263
dbtree_info **ret_line_pang = NULL;
229264
for (size_t i = 0; i < cvector_size(nodes_t); i++) {
230265
dbtree_info *vn = nni_zalloc(sizeof(dbtree_info));
266+
if (vn == NULL) {
267+
/* All vn are pushed in ret_line_pang vector */
268+
cvector_free(ret_line_pang);
269+
log_error("Cannot allocate memory");
270+
return NULL;
271+
}
231272
vn->clients = NULL;
232273
if (cb) {
233274
for (size_t j = 0;
@@ -424,6 +465,10 @@ void
424465
dbtree_create(dbtree **db)
425466
{
426467
*db = (dbtree *) nni_zalloc(sizeof(dbtree));
468+
if (*db == NULL) {
469+
log_error("Cannot allocate memory");
470+
return;
471+
}
427472
memset(*db, 0, sizeof(dbtree));
428473

429474
dbtree_node *node = dbtree_node_new("\0");

src/supplemental/nanolib/rule.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,10 @@ parse_where(char *where, rule *info)
442442
char *p_b = where;
443443

444444
info->filter = (char **) nni_zalloc(sizeof(char *) * 8);
445+
if (info->filter == NULL) {
446+
log_error("Cannot allocate memory");
447+
return NNG_ENOMEM;
448+
}
445449
memset(info->filter, 0, 8 * sizeof(char *));
446450

447451
while ((p = nng_strcasestr(p, "and"))) {

0 commit comments

Comments
 (0)