Skip to content

Commit

Permalink
* FIX [rest_api] when realloc failed, need to free itself
Browse files Browse the repository at this point in the history
Signed-off-by: Moi Ran <maoyi.ran@emqx.io>
  • Loading branch information
RanMaoyi committed May 16, 2024
1 parent b9cfec0 commit 62bbfcb
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions nanomq_cli/rest_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ uri_parse_tree(const char *path, size_t *count)
str += strlen(REST_URI_ROOT);
while (NULL != (ret = strchr(str, '/'))) {
num++;
root = realloc(root, sizeof(tree *) * num);
tree **new_root = NULL;
new_root = realloc(root, sizeof(tree *) * num);
if (new_root == NULL) {
if (root != NULL) {
free(root);
}
return NULL;
}
root = new_root;
len = ret - str + 1;
tree *sub = nng_zalloc(sizeof(tree));
sub->node = nng_zalloc(len);
Expand All @@ -76,7 +84,15 @@ uri_parse_tree(const char *path, size_t *count)
if (num > 0) {
if (strlen(str) > 0) {
num++;
root = realloc(root, sizeof(char *) * num);
tree **new_root = NULL;
new_root = realloc(root, sizeof(tree *) * num);
if (new_root == NULL) {
if (root != NULL) {
free(root);
}
return NULL;
}
root = new_root;
tree *sub = nng_zalloc(sizeof(tree));
sub->node = nng_strdup(str);
sub->end = true;
Expand Down Expand Up @@ -119,15 +135,31 @@ uri_param_parse(const char *path, size_t *count)

while (NULL != (ret = strchr(str, '&'))) {
num++;
kv_str = realloc(kv_str, sizeof(char *) * num);
char **new_kv_str = NULL;
new_kv_str = realloc(kv_str, sizeof(char *) * num);
if (new_kv_str == NULL) {
if (kv_str != NULL) {
free(kv_str);
}
return NULL;
}
kv_str = new_kv_str;
len = ret - str + 1;
kv_str[num - 1] = nng_zalloc(len);
memcpy(kv_str[num - 1], str, len - 1);
str = ret + 1;
}
if (num > 0) {
num++;
kv_str = realloc(kv_str, sizeof(char *) * num);
char **new_kv_str = NULL;
new_kv_str = realloc(kv_str, sizeof(char *) * num);
if (new_kv_str == NULL) {
if (kv_str != NULL) {
free(kv_str);
}
return NULL;
}
kv_str = new_kv_str;
kv_str[num - 1] = nng_strdup(str);
} else {
return NULL;
Expand Down

0 comments on commit 62bbfcb

Please sign in to comment.