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

Commit a46092a

Browse files
committed
free allocated memory in parse_container
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 25da342 commit a46092a

File tree

3 files changed

+139
-83
lines changed

3 files changed

+139
-83
lines changed

src/container.c

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "util.h"
1818
#include "hyper.h"
19+
#include "parse.h"
1920

2021
static int container_setup_volume(struct hyper_container *container)
2122
{
@@ -599,57 +600,6 @@ struct hyper_container *hyper_find_container(struct hyper_pod *pod, char *id)
599600
return NULL;
600601
}
601602

602-
void hyper_free_container(struct hyper_container *c)
603-
{
604-
int i;
605-
struct volume *vol;
606-
struct env *env;
607-
struct fsmap *map;
608-
struct sysctl *sys;
609-
610-
free(c->id);
611-
free(c->rootfs);
612-
free(c->image);
613-
free(c->workdir);
614-
free(c->fstype);
615-
616-
for (i = 0; i < c->vols_num; i++) {
617-
vol = &(c->vols[i]);
618-
free(vol->device);
619-
free(vol->mountpoint);
620-
free(vol->fstype);
621-
}
622-
free(c->vols);
623-
624-
for (i = 0; i < c->envs_num; i++) {
625-
env = &(c->envs[i]);
626-
free(env->env);
627-
free(env->value);
628-
}
629-
free(c->envs);
630-
631-
for (i = 0; i < c->sys_num; i++) {
632-
sys = &(c->sys[i]);
633-
free(sys->path);
634-
free(sys->value);
635-
}
636-
free(c->sys);
637-
638-
for (i = 0; i < c->maps_num; i++) {
639-
map = &(c->maps[i]);
640-
free(map->source);
641-
free(map->path);
642-
}
643-
free(c->maps);
644-
645-
free(c->exec.id);
646-
for (i = 0; i < c->exec.argc; i++) {
647-
//fprintf(stdout, "argv %d %s\n", i, exec->argv[i]);
648-
free(c->exec.argv[i]);
649-
}
650-
free(c->exec.argv);
651-
}
652-
653603
void hyper_cleanup_container(struct hyper_container *c)
654604
{
655605
char root[512];

src/parse.c

Lines changed: 137 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t
3737
return -1;
3838
}
3939

40-
c->exec.argc = toks[i].size;
41-
42-
c->exec.argv = calloc(c->exec.argc + 1, sizeof(*c->exec.argv));
40+
c->exec.argv = calloc(toks[i].size + 1, sizeof(*c->exec.argv));
4341
if (c->exec.argv == NULL) {
4442
fprintf(stderr, "allocate memory for exec argv failed\n");
4543
return -1;
4644
}
4745

4846
c->exec.argv[c->exec.argc] = NULL;
47+
c->exec.argc = toks[i].size;
4948

5049
i++;
5150
for (j = 0; j < c->exec.argc; j++, i++) {
@@ -56,6 +55,33 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t
5655
return i;
5756
}
5857

58+
static void container_free_cmd(struct hyper_container *c)
59+
{
60+
int i;
61+
62+
for (i = 0; i < c->exec.argc; i++) {
63+
free(c->exec.argv[i]);
64+
}
65+
66+
free(c->exec.argv);
67+
c->exec.argv = NULL;
68+
c->exec.argc = 0;
69+
}
70+
71+
static void container_free_volumes(struct hyper_container *c)
72+
{
73+
int i;
74+
75+
for (i = 0; i < c->vols_num; i++) {
76+
free(c->vols[i].device);
77+
free(c->vols[i].mountpoint);
78+
free(c->vols[i].fstype);
79+
}
80+
free(c->vols);
81+
c->vols = NULL;
82+
c->vols_num = 0;
83+
}
84+
5985
static int container_parse_volumes(struct hyper_container *c, char *json, jsmntok_t *toks)
6086
{
6187
int i = 0, j;
@@ -64,15 +90,16 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
6490
fprintf(stdout, "volume need array\n");
6591
return -1;
6692
}
67-
c->vols_num = toks[i].size;
68-
fprintf(stdout, "volumes num %d\n", c->vols_num);
6993

70-
c->vols = calloc(c->vols_num, sizeof(*c->vols));
94+
c->vols = calloc(toks[i].size, sizeof(*c->vols));
7195
if (c->vols == NULL) {
7296
fprintf(stderr, "allocate memory for volume failed\n");
7397
return -1;
7498
}
7599

100+
c->vols_num = toks[i].size;
101+
fprintf(stdout, "volumes num %d\n", c->vols_num);
102+
76103
i++;
77104
for (j = 0; j < c->vols_num; j++) {
78105
int i_volume, next_volume;
@@ -111,6 +138,19 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
111138
return i;
112139
}
113140

141+
void container_free_fsmap(struct hyper_container *c)
142+
{
143+
int i;
144+
145+
for (i = 0; i < c->maps_num; i++) {
146+
free(c->maps[i].source);
147+
free(c->maps[i].path);
148+
}
149+
free(c->maps);
150+
c->maps = NULL;
151+
c->maps_num = 0;
152+
}
153+
114154
static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_t *toks)
115155
{
116156
int i = 0, j;
@@ -120,15 +160,15 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
120160
return -1;
121161
}
122162

123-
c->maps_num = toks[i].size;
124-
fprintf(stdout, "fsmap num %d\n", c->maps_num);
125-
126-
c->maps = calloc(c->maps_num, sizeof(*c->maps));
163+
c->maps = calloc(toks[i].size, sizeof(*c->maps));
127164
if (c->maps == NULL) {
128165
fprintf(stderr, "allocate memory for fsmap failed\n");
129166
return -1;
130167
}
131168

169+
c->maps_num = toks[i].size;
170+
fprintf(stdout, "fsmap num %d\n", c->maps_num);
171+
132172
i++;
133173
for (j = 0; j < c->maps_num; j++) {
134174
int i_map, next_map;
@@ -163,6 +203,20 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
163203
return i;
164204
}
165205

206+
static void container_free_envs(struct hyper_container *c)
207+
{
208+
int i;
209+
210+
for (i = 0; i < c->envs_num; i++) {
211+
free(c->envs[i].env);
212+
free(c->envs[i].value);
213+
}
214+
215+
free(c->envs);
216+
c->envs = NULL;
217+
c->envs_num = 0;
218+
}
219+
166220
static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t *toks)
167221
{
168222
int i = 0, j;
@@ -172,15 +226,15 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
172226
return -1;
173227
}
174228

175-
c->envs_num = toks[i].size;
176-
fprintf(stdout, "envs num %d\n", c->envs_num);
177-
178-
c->envs = calloc(c->envs_num, sizeof(*c->envs));
229+
c->envs = calloc(toks[i].size, sizeof(*c->envs));
179230
if (c->envs == NULL) {
180231
fprintf(stderr, "allocate memory for env failed\n");
181232
return -1;
182233
}
183234

235+
c->envs_num = toks[i].size;
236+
fprintf(stdout, "envs num %d\n", c->envs_num);
237+
184238
i++;
185239
for (j = 0; j < c->envs_num; j++) {
186240
int i_env, next_env;
@@ -211,6 +265,20 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
211265
return i;
212266
}
213267

268+
static void container_free_sysctl(struct hyper_container *c)
269+
{
270+
int i;
271+
272+
for (i = 0; i < c->sys_num; i++) {
273+
free(c->sys[i].path);
274+
free(c->sys[i].value);
275+
}
276+
277+
free(c->sys);
278+
c->sys = NULL;
279+
c->sys_num = 0;
280+
}
281+
214282
static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok_t *toks)
215283
{
216284
int i = 0, j;
@@ -221,15 +289,15 @@ static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok
221289
return -1;
222290
}
223291

224-
c->sys_num = toks[i].size;
225-
fprintf(stdout, "sysctl size %d\n", c->sys_num);
226-
227-
c->sys = calloc(c->sys_num, sizeof(*c->sys));
292+
c->sys = calloc(toks[i].size, sizeof(*c->sys));
228293
if (c->sys == NULL) {
229294
fprintf(stderr, "allocate memory for sysctl failed\n");
230295
return -1;
231296
}
232297

298+
c->sys_num = toks[i].size;
299+
fprintf(stdout, "sysctl size %d\n", c->sys_num);
300+
233301
i++;
234302
for (j = 0; j < c->sys_num; j++) {
235303
c->sys[j].path = strdup(json_token_str(json, &toks[++i]));
@@ -242,9 +310,35 @@ static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok
242310
return i;
243311
}
244312

313+
void hyper_free_container(struct hyper_container *c)
314+
{
315+
free(c->id);
316+
c->id = NULL;
317+
318+
free(c->rootfs);
319+
c->rootfs = NULL;
320+
321+
free(c->image);
322+
c->image = NULL;
323+
324+
free(c->workdir);
325+
c->workdir = NULL;
326+
327+
free(c->fstype);
328+
c->fstype = NULL;
329+
330+
free(c->exec.id);
331+
c->exec.id = NULL;
332+
333+
container_free_volumes(c);
334+
container_free_envs(c);
335+
container_free_sysctl(c);
336+
container_free_fsmap(c);
337+
container_free_cmd(c);
338+
}
245339

246340
static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *c,
247-
char *json, jsmntok_t *toks)
341+
char *json, jsmntok_t *toks)
248342
{
249343
int i = 0, j, next, next_container;
250344
jsmntok_t *t;
@@ -276,7 +370,7 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
276370
} else if (json_token_streq(json, t, "cmd") && t->size == 1) {
277371
next = container_parse_cmd(c, json, &toks[++i]);
278372
if (next < 0)
279-
return -1;
373+
goto fail;
280374
i += next;
281375
} else if (json_token_streq(json, t, "rootfs") && t->size == 1) {
282376
c->rootfs = strdup(json_token_str(json, &toks[++i]));
@@ -305,64 +399,75 @@ static int hyper_parse_container(struct hyper_pod *pod, struct hyper_container *
305399
} else if (json_token_streq(json, t, "volumes") && t->size == 1) {
306400
next = container_parse_volumes(c, json, &toks[++i]);
307401
if (next < 0)
308-
return -1;
402+
goto fail;
309403
i += next;
310404
} else if (json_token_streq(json, t, "fsmap") && t->size == 1) {
311405
next = container_parse_fsmap(c, json, &toks[++i]);
312406
if (next < 0)
313-
return -1;
407+
goto fail;
314408
i += next;
315409
} else if (json_token_streq(json, t, "envs") && t->size == 1) {
316410
next = container_parse_envs(c, json, &toks[++i]);
317411
if (next < 0)
318-
return -1;
412+
goto fail;
319413
i += next;
320414
} else if (json_token_streq(json, t, "sysctl") && t->size == 1) {
321415
next = container_parse_sysctl(c, json, &toks[++i]);
322416
if (next < 0)
323-
return -1;
417+
goto fail;
324418
i += next;
325419
} else if (json_token_streq(json, t, "restartPolicy") && t->size == 1) {
326420
fprintf(stdout, "restart policy %s\n", json_token_str(json, &toks[++i]));
327421
i++;
328422
} else {
329423
fprintf(stdout, "get unknown section %s in container\n",
330424
json_token_str(json, t));
331-
return -1;
425+
goto fail;
332426
}
333427
}
334428

335429
return i;
430+
431+
fail:
432+
hyper_free_container(c);
433+
return -1;
336434
}
337435

338436
static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *toks)
339437
{
340-
int i = 0, j, next;
438+
int i = 0, j = 0, next;
341439

342440
if (toks[i].type != JSMN_ARRAY) {
343441
fprintf(stdout, "format incorrect\n");
344442
return -1;
345443
}
346444

347-
pod->remains = pod->c_num = toks[i].size;
348-
fprintf(stdout, "container count %d\n", pod->c_num);
349-
350-
pod->c = calloc(pod->c_num, sizeof(*pod->c));
445+
pod->c = calloc(toks[i].size, sizeof(*pod->c));
351446
if (pod->c == NULL) {
352447
fprintf(stdout, "alloc memory for container failed\n");
353-
return -1;
448+
goto fail;
354449
}
355450

451+
pod->remains = pod->c_num = toks[i].size;
452+
fprintf(stdout, "container count %d\n", pod->c_num);
453+
356454
i++;
357455
for (j = 0; j < pod->c_num; j++) {
358456
next = hyper_parse_container(pod, &pod->c[j], json, toks + i);
359457
if (next < 0)
360-
return -1;
458+
goto fail;
361459

362460
i += next;
363461
}
364462

365463
return i;
464+
fail:
465+
for (; j > 0; j--)
466+
hyper_free_container(&pod->c[j]);
467+
468+
free(pod->c);
469+
pod->c = NULL;
470+
return -1;
366471
}
367472

368473
static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *toks)

0 commit comments

Comments
 (0)