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

Commit 0d4fac0

Browse files
committed
fix memory leak in parse.c
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 5b33d7a commit 0d4fac0

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
@@ -115,12 +115,26 @@ char *json_token_str(char *js, jsmntok_t *t)
115115

116116
int json_token_int(char *js, jsmntok_t *t)
117117
{
118-
return strtol(json_token_str(js, t), 0, 10);
118+
int ret = 0;
119+
char *data = json_token_str(js, t);
120+
if (data != NULL) {
121+
ret = strtol(data, 0, 10);
122+
free(data);
123+
}
124+
125+
return ret;
119126
}
120127

121128
uint64_t json_token_ll(char *js, jsmntok_t *t)
122129
{
123-
return strtoll(json_token_str(js, t), 0, 10);
130+
uint64_t ret = 0;
131+
char *data = json_token_str(js, t);
132+
if (data != NULL) {
133+
ret = strtoll(data, 0, 10);
134+
free(data);
135+
}
136+
137+
return ret;
124138
}
125139

126140
int json_token_streq(char *js, jsmntok_t *t, char *s)
@@ -129,6 +143,13 @@ int json_token_streq(char *js, jsmntok_t *t, char *s)
129143
strlen(s) == (size_t)(t->end - t->start));
130144
}
131145

146+
void hyper_print_unknown_section(char *json, jsmntok_t *t)
147+
{
148+
char *data = json_token_str(json, t);
149+
dprintf(stderr, "get unknown section %s\n", data);
150+
free(data);
151+
}
152+
132153
static int container_parse_additional_groups(struct hyper_exec *exec, char *json, jsmntok_t *toks)
133154
{
134155
int i = 0, j;
@@ -271,12 +292,10 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
271292
c->vols[j].scsiaddr = (json_token_str(json, &toks[++i]));
272293
dprintf(stdout, "volume %d scsi id %s\n", j, c->vols[j].scsiaddr);
273294
} else if (json_token_streq(json, &toks[i], "mount")) {
274-
c->vols[j].mountpoint =
275-
(json_token_str(json, &toks[++i]));
295+
c->vols[j].mountpoint = (json_token_str(json, &toks[++i]));
276296
dprintf(stdout, "volume %d mp %s\n", j, c->vols[j].mountpoint);
277297
} else if (json_token_streq(json, &toks[i], "fstype")) {
278-
c->vols[j].fstype =
279-
(json_token_str(json, &toks[++i]));
298+
c->vols[j].fstype = (json_token_str(json, &toks[++i]));
280299
dprintf(stdout, "volume %d fstype %s\n", j, c->vols[j].fstype);
281300
} else if (json_token_streq(json, &toks[i], "readOnly")) {
282301
if (!json_token_streq(json, &toks[++i], "false"))
@@ -287,8 +306,7 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
287306
c->vols[j].docker = 1;
288307
dprintf(stdout, "volume %d docker volume %d\n", j, c->vols[j].docker);
289308
} else {
290-
dprintf(stdout, "get unknown section %s in voulmes\n",
291-
json_token_str(json, &toks[i]));
309+
hyper_print_unknown_section(json, &toks[i]);
292310
return -1;
293311
}
294312
}
@@ -340,12 +358,10 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
340358
i++;
341359
for (i_map = 0; i_map < next_map; i_map++, i++) {
342360
if (json_token_streq(json, &toks[i], "source")) {
343-
c->maps[j].source =
344-
(json_token_str(json, &toks[++i]));
361+
c->maps[j].source = (json_token_str(json, &toks[++i]));
345362
dprintf(stdout, "maps %d source %s\n", j, c->maps[j].source);
346363
} else if (json_token_streq(json, &toks[i], "path")) {
347-
c->maps[j].path =
348-
(json_token_str(json, &toks[++i]));
364+
c->maps[j].path = (json_token_str(json, &toks[++i]));
349365
dprintf(stdout, "maps %d path %s\n", j, c->maps[j].path);
350366
} else if (json_token_streq(json, &toks[i], "readOnly")) {
351367
if (!json_token_streq(json, &toks[++i], "false"))
@@ -356,8 +372,7 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
356372
c->maps[j].docker = 1;
357373
dprintf(stdout, "maps %d docker volume %d\n", j, c->maps[j].docker);
358374
} else {
359-
dprintf(stdout, "in maps incorrect %s\n",
360-
json_token_str(json, &toks[i]));
375+
hyper_print_unknown_section(json, &toks[i]);
361376
return -1;
362377
}
363378
}
@@ -396,16 +411,13 @@ static int container_parse_envs(struct hyper_exec *exec, char *json, jsmntok_t *
396411
i++;
397412
for (i_env = 0; i_env < next_env; i_env++, i++) {
398413
if (json_token_streq(json, &toks[i], "env")) {
399-
exec->envs[j].env =
400-
(json_token_str(json, &toks[++i]));
414+
exec->envs[j].env = (json_token_str(json, &toks[++i]));
401415
dprintf(stdout, "envs %d env %s\n", j, exec->envs[j].env);
402416
} else if (json_token_streq(json, &toks[i], "value")) {
403-
exec->envs[j].value =
404-
(json_token_str(json, &toks[++i]));
417+
exec->envs[j].value = (json_token_str(json, &toks[++i]));
405418
dprintf(stdout, "envs %d value %s\n", j, exec->envs[j].value);
406419
} else {
407-
dprintf(stdout, "get unknown section %s in envs\n",
408-
json_token_str(json, &toks[i]));
420+
hyper_print_unknown_section(json, &toks[i]);
409421
return -1;
410422
}
411423
}
@@ -473,7 +485,7 @@ static int hyper_parse_process(struct hyper_exec *exec, char *json, jsmntok_t *t
473485
i++;
474486
for (j = 0; j < toks_size; j++) {
475487
t = &toks[i];
476-
dprintf(stdout, "%d name %s\n", i, json_token_str(json, t));
488+
477489
if (json_token_streq(json, t, "user") && t->size == 1) {
478490
exec->user = (json_token_str(json, &toks[++i]));
479491
dprintf(stdout, "container process user %s\n", exec->user);
@@ -569,8 +581,7 @@ static int container_parse_ports(struct hyper_container *c, char *json, jsmntok_
569581
i++;
570582
for (i_port = 0; i_port < next_port; i_port++, i++) {
571583
if (json_token_streq(json, &toks[i], "protocol")) {
572-
c->ports[j].protocol =
573-
(json_token_str(json, &toks[++i]));
584+
c->ports[j].protocol = (json_token_str(json, &toks[++i]));
574585
dprintf(stdout, "port %d protocol %s\n", j, c->ports[j].protocol);
575586
} else if (json_token_streq(json, &toks[i], "hostPort")) {
576587
c->ports[j].host_port = json_token_int(json, &toks[++i]);
@@ -579,8 +590,7 @@ static int container_parse_ports(struct hyper_container *c, char *json, jsmntok_
579590
c->ports[j].container_port = json_token_int(json, &toks[++i]);
580591
dprintf(stdout, "port %d container_port %d\n", j, c->ports[j].container_port);
581592
} else {
582-
dprintf(stdout, "get unknown section %s in ports\n",
583-
json_token_str(json, &toks[i]));
593+
hyper_print_unknown_section(json, &toks[i]);
584594
return -1;
585595
}
586596
}
@@ -648,7 +658,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
648658
i++;
649659
for (j = 0; j < next_container; j++) {
650660
t = &toks[i];
651-
dprintf(stdout, "%d name %s\n", i, json_token_str(json, t));
661+
652662
if (json_token_streq(json, t, "id") && t->size == 1) {
653663
c->id = (json_token_str(json, &toks[++i]));
654664
c->exec.container_id = strdup(c->id);
@@ -705,8 +715,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
705715
goto fail;
706716
i += next;
707717
} else {
708-
dprintf(stdout, "get unknown section %s in container\n",
709-
json_token_str(json, t));
718+
hyper_print_unknown_section(json, t);
710719
goto fail;
711720
}
712721
}
@@ -817,8 +826,7 @@ static int hyper_parse_interface(struct hyper_interface *iface,
817826
dprintf(stdout, "net mask for device %s is %s\n",
818827
iface->device, ipaddr->mask);
819828
} else {
820-
dprintf(stderr, "get unknown section %s in interfaces\n",
821-
json_token_str(json, &toks[i]));
829+
hyper_print_unknown_section(json, &toks[i]);
822830
free(ipaddr);
823831
goto fail;
824832
}
@@ -842,8 +850,7 @@ static int hyper_parse_interface(struct hyper_interface *iface,
842850
ipaddr_oldf->mask = (json_token_str(json, &toks[++i]));
843851
dprintf(stdout, "net mask is %s\n", ipaddr_oldf->mask);
844852
} else {
845-
dprintf(stderr, "get unknown section %s in interfaces\n",
846-
json_token_str(json, &toks[i]));
853+
hyper_print_unknown_section(json, &toks[i]);
847854
goto fail;
848855
}
849856
}
@@ -978,8 +985,7 @@ static int hyper_parse_routes(struct hyper_route **routes, uint32_t *r_num, char
978985
rt->device = (json_token_str(json, &toks[++i]));
979986
dprintf(stdout, "route %d device is %s\n", j, rt->device);
980987
} else {
981-
dprintf(stderr, "get unknown section %s in routes\n",
982-
json_token_str(json, &toks[i]));
988+
hyper_print_unknown_section(json, &toks[i]);
983989
goto out;
984990
}
985991
}
@@ -1173,7 +1179,7 @@ static int hyper_parse_portmapping_whitelist(struct hyper_pod *pod, char *json,
11731179
}
11741180
i += next;
11751181
} else {
1176-
dprintf(stdout, "get unknown section %s in portmap_white_lists\n", json_token_str(json, t));
1182+
hyper_print_unknown_section(json, t);
11771183
goto out;
11781184
}
11791185
}
@@ -1269,8 +1275,7 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
12691275

12701276
i += next;
12711277
} else {
1272-
dprintf(stdout, "get unknown section %s in pod\n",
1273-
json_token_str(json, &toks[i]));
1278+
hyper_print_unknown_section(json, &toks[i]);
12741279
next = -1;
12751280
break;
12761281
}
@@ -1433,8 +1438,7 @@ int hyper_parse_file_command(struct file_command *cmd, char *json, int length)
14331438
cmd->file = (json_token_str(json, &toks[i]));
14341439
dprintf(stdout, "file cmd get file %s\n", cmd->file);
14351440
} else {
1436-
dprintf(stdout, "get unknown section %s in file cmd\n",
1437-
json_token_str(json, t));
1441+
hyper_print_unknown_section(json, t);
14381442
goto fail;
14391443
}
14401444
}

0 commit comments

Comments
 (0)