Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit f6e080e

Browse files
authored
Merge pull request #239 from gao-feng/memory
fix memory leak in parse.c
2 parents fe5e0fc + e449081 commit f6e080e

File tree

1 file changed

+43
-39
lines changed

1 file changed

+43
-39
lines changed

src/parse.c

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,34 @@ static char* process_string(const char *input, size_t len) {
108108
return NULL;
109109
}
110110

111+
// the return value need to be freed
111112
char *json_token_str(char *js, jsmntok_t *t)
112113
{
113114
return process_string(js+t->start, t->end - t->start);
114115
}
115116

116117
int json_token_int(char *js, jsmntok_t *t)
117118
{
118-
return strtol(json_token_str(js, t), 0, 10);
119+
int ret = 0;
120+
char *data = json_token_str(js, t);
121+
if (data != NULL) {
122+
ret = strtol(data, 0, 10);
123+
free(data);
124+
}
125+
126+
return ret;
119127
}
120128

121129
uint64_t json_token_ll(char *js, jsmntok_t *t)
122130
{
123-
return strtoll(json_token_str(js, t), 0, 10);
131+
uint64_t ret = 0;
132+
char *data = json_token_str(js, t);
133+
if (data != NULL) {
134+
ret = strtoll(data, 0, 10);
135+
free(data);
136+
}
137+
138+
return ret;
124139
}
125140

126141
int json_token_streq(char *js, jsmntok_t *t, char *s)
@@ -129,6 +144,13 @@ int json_token_streq(char *js, jsmntok_t *t, char *s)
129144
strlen(s) == (size_t)(t->end - t->start));
130145
}
131146

147+
void hyper_print_unknown_key(char *json, jsmntok_t *t)
148+
{
149+
char *data = json_token_str(json, t);
150+
dprintf(stderr, "get unknown key %s\n", data);
151+
free(data);
152+
}
153+
132154
static int container_parse_additional_groups(struct hyper_exec *exec, char *json, jsmntok_t *toks)
133155
{
134156
int i = 0, j;
@@ -274,12 +296,10 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
274296
c->vols[j].scsiaddr = (json_token_str(json, &toks[++i]));
275297
dprintf(stdout, "volume %d scsi id %s\n", j, c->vols[j].scsiaddr);
276298
} else if (json_token_streq(json, &toks[i], "mount")) {
277-
c->vols[j].mountpoint =
278-
(json_token_str(json, &toks[++i]));
299+
c->vols[j].mountpoint = (json_token_str(json, &toks[++i]));
279300
dprintf(stdout, "volume %d mp %s\n", j, c->vols[j].mountpoint);
280301
} else if (json_token_streq(json, &toks[i], "fstype")) {
281-
c->vols[j].fstype =
282-
(json_token_str(json, &toks[++i]));
302+
c->vols[j].fstype = (json_token_str(json, &toks[++i]));
283303
dprintf(stdout, "volume %d fstype %s\n", j, c->vols[j].fstype);
284304
} else if (json_token_streq(json, &toks[i], "readOnly")) {
285305
if (!json_token_streq(json, &toks[++i], "false"))
@@ -290,8 +310,7 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
290310
c->vols[j].docker = 1;
291311
dprintf(stdout, "volume %d docker volume %d\n", j, c->vols[j].docker);
292312
} else {
293-
dprintf(stdout, "get unknown section %s in voulmes\n",
294-
json_token_str(json, &toks[i]));
313+
hyper_print_unknown_key(json, &toks[i]);
295314
return -1;
296315
}
297316
}
@@ -343,12 +362,10 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
343362
i++;
344363
for (i_map = 0; i_map < next_map; i_map++, i++) {
345364
if (json_token_streq(json, &toks[i], "source")) {
346-
c->maps[j].source =
347-
(json_token_str(json, &toks[++i]));
365+
c->maps[j].source = (json_token_str(json, &toks[++i]));
348366
dprintf(stdout, "maps %d source %s\n", j, c->maps[j].source);
349367
} else if (json_token_streq(json, &toks[i], "path")) {
350-
c->maps[j].path =
351-
(json_token_str(json, &toks[++i]));
368+
c->maps[j].path = (json_token_str(json, &toks[++i]));
352369
dprintf(stdout, "maps %d path %s\n", j, c->maps[j].path);
353370
} else if (json_token_streq(json, &toks[i], "readOnly")) {
354371
if (!json_token_streq(json, &toks[++i], "false"))
@@ -359,8 +376,7 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
359376
c->maps[j].docker = 1;
360377
dprintf(stdout, "maps %d docker volume %d\n", j, c->maps[j].docker);
361378
} else {
362-
dprintf(stdout, "in maps incorrect %s\n",
363-
json_token_str(json, &toks[i]));
379+
hyper_print_unknown_key(json, &toks[i]);
364380
return -1;
365381
}
366382
}
@@ -399,16 +415,13 @@ static int container_parse_envs(struct hyper_exec *exec, char *json, jsmntok_t *
399415
i++;
400416
for (i_env = 0; i_env < next_env; i_env++, i++) {
401417
if (json_token_streq(json, &toks[i], "env")) {
402-
exec->envs[j].env =
403-
(json_token_str(json, &toks[++i]));
418+
exec->envs[j].env = (json_token_str(json, &toks[++i]));
404419
dprintf(stdout, "envs %d env %s\n", j, exec->envs[j].env);
405420
} else if (json_token_streq(json, &toks[i], "value")) {
406-
exec->envs[j].value =
407-
(json_token_str(json, &toks[++i]));
421+
exec->envs[j].value = (json_token_str(json, &toks[++i]));
408422
dprintf(stdout, "envs %d value %s\n", j, exec->envs[j].value);
409423
} else {
410-
dprintf(stdout, "get unknown section %s in envs\n",
411-
json_token_str(json, &toks[i]));
424+
hyper_print_unknown_key(json, &toks[i]);
412425
return -1;
413426
}
414427
}
@@ -476,7 +489,6 @@ static int hyper_parse_process(struct hyper_exec *exec, char *json, jsmntok_t *t
476489
i++;
477490
for (j = 0; j < toks_size; j++) {
478491
t = &toks[i];
479-
dprintf(stdout, "%d name %s\n", i, json_token_str(json, t));
480492
if (json_token_streq(json, t, "id") && t->size == 1) {
481493
exec->id = (json_token_str(json, &toks[++i]));
482494
dprintf(stdout, "container process id %s\n", exec->id);
@@ -576,8 +588,7 @@ static int container_parse_ports(struct hyper_container *c, char *json, jsmntok_
576588
i++;
577589
for (i_port = 0; i_port < next_port; i_port++, i++) {
578590
if (json_token_streq(json, &toks[i], "protocol")) {
579-
c->ports[j].protocol =
580-
(json_token_str(json, &toks[++i]));
591+
c->ports[j].protocol = (json_token_str(json, &toks[++i]));
581592
dprintf(stdout, "port %d protocol %s\n", j, c->ports[j].protocol);
582593
} else if (json_token_streq(json, &toks[i], "hostPort")) {
583594
c->ports[j].host_port = json_token_int(json, &toks[++i]);
@@ -586,8 +597,7 @@ static int container_parse_ports(struct hyper_container *c, char *json, jsmntok_
586597
c->ports[j].container_port = json_token_int(json, &toks[++i]);
587598
dprintf(stdout, "port %d container_port %d\n", j, c->ports[j].container_port);
588599
} else {
589-
dprintf(stdout, "get unknown section %s in ports\n",
590-
json_token_str(json, &toks[i]));
600+
hyper_print_unknown_key(json, &toks[i]);
591601
return -1;
592602
}
593603
}
@@ -655,7 +665,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
655665
i++;
656666
for (j = 0; j < next_container; j++) {
657667
t = &toks[i];
658-
dprintf(stdout, "%d name %s\n", i, json_token_str(json, t));
668+
659669
if (json_token_streq(json, t, "id") && t->size == 1) {
660670
c->id = (json_token_str(json, &toks[++i]));
661671
c->exec.container_id = strdup(c->id);
@@ -712,8 +722,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
712722
goto fail;
713723
i += next;
714724
} else {
715-
dprintf(stdout, "get unknown section %s in container\n",
716-
json_token_str(json, t));
725+
hyper_print_unknown_key(json, t);
717726
goto fail;
718727
}
719728
}
@@ -824,8 +833,7 @@ static int hyper_parse_interface(struct hyper_interface *iface,
824833
dprintf(stdout, "net mask for device %s is %s\n",
825834
iface->device, ipaddr->mask);
826835
} else {
827-
dprintf(stderr, "get unknown section %s in interfaces\n",
828-
json_token_str(json, &toks[i]));
836+
hyper_print_unknown_key(json, &toks[i]);
829837
free(ipaddr);
830838
goto fail;
831839
}
@@ -849,8 +857,7 @@ static int hyper_parse_interface(struct hyper_interface *iface,
849857
ipaddr_oldf->mask = (json_token_str(json, &toks[++i]));
850858
dprintf(stdout, "net mask is %s\n", ipaddr_oldf->mask);
851859
} else {
852-
dprintf(stderr, "get unknown section %s in interfaces\n",
853-
json_token_str(json, &toks[i]));
860+
hyper_print_unknown_key(json, &toks[i]);
854861
goto fail;
855862
}
856863
}
@@ -985,8 +992,7 @@ static int hyper_parse_routes(struct hyper_route **routes, uint32_t *r_num, char
985992
rt->device = (json_token_str(json, &toks[++i]));
986993
dprintf(stdout, "route %d device is %s\n", j, rt->device);
987994
} else {
988-
dprintf(stderr, "get unknown section %s in routes\n",
989-
json_token_str(json, &toks[i]));
995+
hyper_print_unknown_key(json, &toks[i]);
990996
goto out;
991997
}
992998
}
@@ -1180,7 +1186,7 @@ static int hyper_parse_portmapping_whitelist(struct hyper_pod *pod, char *json,
11801186
}
11811187
i += next;
11821188
} else {
1183-
dprintf(stdout, "get unknown section %s in portmap_white_lists\n", json_token_str(json, t));
1189+
hyper_print_unknown_key(json, t);
11841190
goto out;
11851191
}
11861192
}
@@ -1276,8 +1282,7 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
12761282

12771283
i += next;
12781284
} else {
1279-
dprintf(stdout, "get unknown section %s in pod\n",
1280-
json_token_str(json, &toks[i]));
1285+
hyper_print_unknown_key(json, &toks[i]);
12811286
next = -1;
12821287
break;
12831288
}
@@ -1440,8 +1445,7 @@ int hyper_parse_file_command(struct file_command *cmd, char *json, int length)
14401445
cmd->file = (json_token_str(json, &toks[i]));
14411446
dprintf(stdout, "file cmd get file %s\n", cmd->file);
14421447
} else {
1443-
dprintf(stdout, "get unknown section %s in file cmd\n",
1444-
json_token_str(json, t));
1448+
hyper_print_unknown_key(json, t);
14451449
goto fail;
14461450
}
14471451
}

0 commit comments

Comments
 (0)