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

Commit 1558b44

Browse files
committed
add the check of memory allocation
Signed-off-by: Gao feng <omarapazanadi@gmail.com>
1 parent 0c42ea9 commit 1558b44

File tree

2 files changed

+97
-25
lines changed

2 files changed

+97
-25
lines changed

src/init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,6 @@ static int hyper_cmd_write_file(char *json, int length)
724724
int len = 0, size, ret = -1;
725725

726726
fprintf(stdout, "%s\n", __func__);
727-
memset(&writter, 0, sizeof(writter));
728727

729728
if (hyper_parse_write_file(&writter, json, length) < 0) {
730729
goto out;
@@ -892,7 +891,6 @@ static int hyper_cmd_read_file(char *json, int length, uint32_t *datalen, uint8_
892891
uint32_t type;
893892

894893
fprintf(stdout, "%s\n", __func__);
895-
memset(&reader, 0, sizeof(reader));
896894

897895
if (hyper_parse_read_file(&reader, json, length) < 0) {
898896
goto out;

src/parse.c

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ static int container_parse_cmd(struct hyper_container *c, char *json, jsmntok_t
4040
c->exec.argc = toks[i].size;
4141

4242
c->exec.argv = calloc(c->exec.argc + 1, sizeof(*c->exec.argv));
43+
if (c->exec.argv == NULL) {
44+
fprintf(stderr, "allocate memory for exec argv failed\n");
45+
return -1;
46+
}
47+
4348
c->exec.argv[c->exec.argc] = NULL;
4449

4550
for (j = 0; j < c->exec.argc; j++) {
@@ -61,7 +66,12 @@ static int container_parse_volumes(struct hyper_container *c, char *json, jsmnto
6166
}
6267
c->vols_num = toks[i].size;
6368
fprintf(stdout, "volumes num %d\n", c->vols_num);
69+
6470
c->vols = calloc(c->vols_num, sizeof(*c->vols));
71+
if (c->vols == NULL) {
72+
fprintf(stderr, "allocate memory for volume failed\n");
73+
return -1;
74+
}
6575

6676
for (j = 0; j < c->vols_num; j++) {
6777
int i_volume, next_volume;
@@ -110,7 +120,12 @@ static int container_parse_fsmap(struct hyper_container *c, char *json, jsmntok_
110120

111121
c->maps_num = toks[i].size;
112122
fprintf(stdout, "fsmap num %d\n", c->maps_num);
123+
113124
c->maps = calloc(c->maps_num, sizeof(*c->maps));
125+
if (c->maps == NULL) {
126+
fprintf(stderr, "allocate memory for fsmap failed\n");
127+
return -1;
128+
}
114129

115130
for (j = 0; j < c->maps_num; j++) {
116131
int i_map, next_map;
@@ -156,7 +171,12 @@ static int container_parse_envs(struct hyper_container *c, char *json, jsmntok_t
156171

157172
c->envs_num = toks[i].size;
158173
fprintf(stdout, "envs num %d\n", c->envs_num);
174+
159175
c->envs = calloc(c->envs_num, sizeof(*c->envs));
176+
if (c->envs == NULL) {
177+
fprintf(stderr, "allocate memory for env failed\n");
178+
return -1;
179+
}
160180

161181
for (j = 0; j < c->envs_num; j++) {
162182
int i_env, next_env;
@@ -198,7 +218,12 @@ static int container_parse_sysctl(struct hyper_container *c, char *json, jsmntok
198218

199219
c->sys_num = toks[i].size;
200220
fprintf(stdout, "sysctl size %d\n", c->sys_num);
221+
201222
c->sys = calloc(c->sys_num, sizeof(*c->sys));
223+
if (c->sys == NULL) {
224+
fprintf(stderr, "allocate memory for sysctl failed\n");
225+
return -1;
226+
}
202227

203228
for (j = 0; j < c->sys_num; j++) {
204229
c->sys[j].path = strdup(json_token_str(json, &toks[++i]));
@@ -320,8 +345,8 @@ static int hyper_parse_containers(struct hyper_pod *pod, char *json, jsmntok_t *
320345

321346
pod->remains = pod->c_num = toks[i].size;
322347
fprintf(stdout, "container count %d\n", pod->c_num);
323-
pod->c = calloc(pod->c_num, sizeof(*pod->c));
324348

349+
pod->c = calloc(pod->c_num, sizeof(*pod->c));
325350
if (pod->c == NULL) {
326351
fprintf(stdout, "alloc memory for container failed\n");
327352
return -1;
@@ -350,7 +375,12 @@ static int hyper_parse_interfaces(struct hyper_pod *pod, char *json, jsmntok_t *
350375

351376
pod->i_num = toks[i].size;
352377
fprintf(stdout, "network interfaces num %d\n", pod->i_num);
378+
353379
pod->iface = calloc(pod->i_num, sizeof(*iface));
380+
if (pod->iface == NULL) {
381+
fprintf(stdout, "alloc memory for interface failed\n");
382+
return -1;
383+
}
354384

355385
for (j = 0; j < pod->i_num; j++) {
356386
int i_if;
@@ -393,7 +423,12 @@ static int hyper_parse_routes(struct hyper_pod *pod, char *json, jsmntok_t *toks
393423

394424
pod->r_num = toks[i].size;
395425
fprintf(stdout, "network routes num %d\n", pod->r_num);
426+
396427
pod->rt = calloc(pod->r_num, sizeof(*rt));
428+
if (pod->rt == NULL) {
429+
fprintf(stdout, "alloc memory for router failed\n");
430+
return -1;
431+
}
397432

398433
for (j = 0; j < pod->r_num; j++) {
399434
int i_rt;
@@ -435,10 +470,10 @@ static int hyper_parse_dns(struct hyper_pod *pod, char *json, jsmntok_t *toks)
435470

436471
pod->d_num = toks[i].size;
437472
fprintf(stdout, "dns count %d\n", pod->d_num);
438-
pod->dns = calloc(pod->d_num, sizeof(*pod->dns));
439473

474+
pod->dns = calloc(pod->d_num, sizeof(*pod->dns));
440475
if (pod->dns == NULL) {
441-
fprintf(stdout, "alloc memory for container failed\n");
476+
fprintf(stdout, "alloc memory for dns failed\n");
442477
return -1;
443478
}
444479

@@ -459,6 +494,10 @@ int hyper_parse_pod(struct hyper_pod *pod, char *json, int length)
459494

460495
realloc:
461496
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
497+
if (toks == NULL) {
498+
fprintf(stderr, "allocate tokens for pod failed\n");
499+
goto out;
500+
}
462501

463502
fprintf(stdout, "call hyper_start_pod, json %s, len %d\n", json, length);
464503
jsmn_init(&p);
@@ -540,6 +579,10 @@ int hyper_parse_new_container(struct hyper_pod *pod, char *json, int length)
540579

541580
realloc:
542581
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
582+
if (toks == NULL) {
583+
fprintf(stderr, "allocate tokens for new container failed\n");
584+
goto fail;
585+
}
543586

544587
jsmn_init(&p);
545588
n = jsmn_parse(&p, json, length, toks, toks_num);
@@ -584,6 +627,10 @@ int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
584627

585628
realloc:
586629
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
630+
if (toks == NULL) {
631+
fprintf(stderr, "allocate tokens for winsize failed\n");
632+
goto fail;
633+
}
587634

588635
jsmn_init(&p);
589636

@@ -636,14 +683,17 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
636683
{
637684
int i, j, n, has_seq = 0;
638685
struct hyper_exec *exec = NULL;
639-
char **argv = NULL;
640686

641687
jsmn_parser p;
642688
int toks_num = 10;
643689
jsmntok_t *toks = NULL;
644690

645691
realloc:
646692
toks = realloc(toks, toks_num * sizeof(jsmntok_t));
693+
if (toks == NULL) {
694+
fprintf(stderr, "allocate tokens for execcmd failed\n");
695+
goto fail;
696+
}
647697

648698
jsmn_init(&p);
649699
n = jsmn_parse(&p, json, length, toks, toks_num);
@@ -657,8 +707,10 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
657707
}
658708

659709
exec = calloc(1, sizeof(*exec));
660-
if (exec == NULL)
710+
if (exec == NULL) {
711+
fprintf(stderr, "allocate memory for exec cmd failed\n");
661712
goto out;
713+
}
662714

663715
exec->ptyfd = -1;
664716
exec->errfd = -1;
@@ -693,27 +745,31 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
693745
goto fail;
694746
}
695747

748+
exec->argv = calloc(toks[i].size + 1, sizeof(*exec->argv));
749+
if (exec->argv == NULL) {
750+
fprintf(stdout, "allocate memory for exec cmd argv failed\n");
751+
goto fail;
752+
}
696753
exec->argc = toks[i].size;
697-
argv = calloc(exec->argc + 1, sizeof(*argv));
698-
argv[exec->argc] = NULL;
754+
exec->argv[exec->argc] = NULL;
699755
} else if (j < exec->argc) {
700-
argv[j++] = strdup(json_token_str(json, &toks[i]));
701-
fprintf(stdout, "argv %d, %s\n", j - 1, argv[j - 1]);
756+
exec->argv[j++] = strdup(json_token_str(json, &toks[i]));
757+
fprintf(stdout, "argv %d, %s\n", j - 1, exec->argv[j - 1]);
702758
}
703759
}
704760

705761
if (!has_seq) {
706762
fprintf(stderr, "execcmd format error, has no seq\n");
707763
goto fail;
708764
}
709-
exec->argv = argv;
765+
710766
out:
711767
free(toks);
712768
return exec;
713769
fail:
714770
free(exec->id);
715771
for (i = 0; i < exec->argc; i++)
716-
free(argv[i]);
772+
free(exec->argv[i]);
717773

718774
free(exec->argv);
719775
free(exec);
@@ -724,28 +780,35 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
724780

725781
int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length)
726782
{
727-
int i, n, ret = 0;
783+
int i, n, ret = -1;
728784

729785
jsmn_parser p;
730786
int toks_num = 10;
731787
jsmntok_t *toks = NULL;
732788

789+
memset(writter, 0, sizeof(*writter));
790+
733791
toks = calloc(toks_num, sizeof(jsmntok_t));
792+
if (toks == NULL) {
793+
fprintf(stderr, "fail to allocate tokens for write file cmd\n");
794+
goto fail;
795+
}
734796

735797
jsmn_init(&p);
736798
n = jsmn_parse(&p, json, length, toks, toks_num);
737799
/* Must be json first */
738800
if (n <= 0) {
739801
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
740-
ret = -1;
741-
goto out;
802+
goto fail;
742803
}
743804

744805
writter->len = length - toks[0].end;
745806
writter->data = malloc(writter->len);
746807

747-
if (writter->data == NULL)
808+
if (writter->data == NULL) {
809+
fprintf(stderr, "fail to allocate memory for writter data\n");
748810
goto fail;
811+
}
749812

750813
memcpy(writter->data, json + toks[0].end, writter->len);
751814
fprintf(stdout, "writefile get data len %d %s\n", writter->len, writter->data);
@@ -769,42 +832,49 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length
769832
writter->file = strdup(json_token_str(json, &toks[i]));
770833
fprintf(stdout, "writefile get file %s\n", writter->file);
771834
} else {
772-
fprintf(stdout, "in writefile incorrect %s\n", json_token_str(json, &toks[i]));
835+
fprintf(stderr, "in writefile incorrect %s\n", json_token_str(json, &toks[i]));
773836
}
774837
}
838+
839+
if (writter->id == NULL || writter->file == NULL) {
840+
fprintf(stderr, "writefile format incorrect\n");
841+
goto fail;
842+
}
843+
844+
ret = 0;
775845
out:
776846
free(toks);
777847
return ret;
778848
fail:
779849
free(writter->id);
780850
free(writter->file);
781851
free(writter->data);
782-
783-
ret = -1;
784852
goto out;
785853
}
786854

787855
int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length)
788856
{
789-
int i, n, ret = 0;
857+
int i, n, ret = -1;
790858

791859
jsmn_parser p;
792860
int toks_num = 10;
793861
jsmntok_t *toks = NULL;
794862

863+
memset(reader, 0, sizeof(*reader));
864+
795865
toks = calloc(toks_num, sizeof(jsmntok_t));
796866
if (toks == NULL) {
797867
fprintf(stderr, "fail to allocate tokens for read file cmd\n");
798868
ret = -1;
799-
goto out;
869+
goto fail;
800870
}
801871

802872
jsmn_init(&p);
803873
n = jsmn_parse(&p, json, length, toks, toks_num);
804874
if (n < 0) {
805875
fprintf(stdout, "jsmn parse failed, n is %d\n", n);
806876
ret = -1;
807-
goto out;
877+
goto fail;
808878
}
809879

810880
for (i = 0; i < n; i++) {
@@ -830,13 +900,17 @@ int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length)
830900
}
831901
}
832902

903+
if (reader->id == NULL || reader->file == NULL) {
904+
fprintf(stderr, "readfile format incorrect\n");
905+
goto fail;
906+
}
907+
908+
ret = 0;
833909
out:
834910
free(toks);
835911
return ret;
836912
fail:
837913
free(reader->id);
838914
free(reader->file);
839-
840-
ret = -1;
841915
goto out;
842916
}

0 commit comments

Comments
 (0)