@@ -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
460495realloc :
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 @@ struct hyper_container *hyper_parse_new_container(struct hyper_pod *pod, char *j
540579
541580realloc :
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 );
@@ -582,6 +625,10 @@ int hyper_parse_winsize(struct hyper_win_size *ws, char *json, int length)
582625
583626realloc :
584627 toks = realloc (toks , toks_num * sizeof (jsmntok_t ));
628+ if (toks == NULL ) {
629+ fprintf (stderr , "allocate tokens for winsize failed\n" );
630+ goto fail ;
631+ }
585632
586633 jsmn_init (& p );
587634
@@ -634,14 +681,17 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
634681{
635682 int i , j , n , has_seq = 0 ;
636683 struct hyper_exec * exec = NULL ;
637- char * * argv = NULL ;
638684
639685 jsmn_parser p ;
640686 int toks_num = 10 ;
641687 jsmntok_t * toks = NULL ;
642688
643689realloc :
644690 toks = realloc (toks , toks_num * sizeof (jsmntok_t ));
691+ if (toks == NULL ) {
692+ fprintf (stderr , "allocate tokens for execcmd failed\n" );
693+ goto fail ;
694+ }
645695
646696 jsmn_init (& p );
647697 n = jsmn_parse (& p , json , length , toks , toks_num );
@@ -655,8 +705,10 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
655705 }
656706
657707 exec = calloc (1 , sizeof (* exec ));
658- if (exec == NULL )
708+ if (exec == NULL ) {
709+ fprintf (stderr , "allocate memory for exec cmd failed\n" );
659710 goto out ;
711+ }
660712
661713 exec -> ptyfd = -1 ;
662714 exec -> errfd = -1 ;
@@ -691,27 +743,31 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
691743 goto fail ;
692744 }
693745
746+ exec -> argv = calloc (toks [i ].size + 1 , sizeof (* exec -> argv ));
747+ if (exec -> argv == NULL ) {
748+ fprintf (stdout , "allocate memory for exec cmd argv failed\n" );
749+ goto fail ;
750+ }
694751 exec -> argc = toks [i ].size ;
695- argv = calloc (exec -> argc + 1 , sizeof (* argv ));
696- argv [exec -> argc ] = NULL ;
752+ exec -> argv [exec -> argc ] = NULL ;
697753 } else if (j < exec -> argc ) {
698- argv [j ++ ] = strdup (json_token_str (json , & toks [i ]));
699- fprintf (stdout , "argv %d, %s\n" , j - 1 , argv [j - 1 ]);
754+ exec -> argv [j ++ ] = strdup (json_token_str (json , & toks [i ]));
755+ fprintf (stdout , "argv %d, %s\n" , j - 1 , exec -> argv [j - 1 ]);
700756 }
701757 }
702758
703759 if (!has_seq ) {
704760 fprintf (stderr , "execcmd format error, has no seq\n" );
705761 goto fail ;
706762 }
707- exec -> argv = argv ;
763+
708764out :
709765 free (toks );
710766 return exec ;
711767fail :
712768 free (exec -> id );
713769 for (i = 0 ; i < exec -> argc ; i ++ )
714- free (argv [i ]);
770+ free (exec -> argv [i ]);
715771
716772 free (exec -> argv );
717773 free (exec );
@@ -722,28 +778,35 @@ struct hyper_exec *hyper_parse_execcmd(char *json, int length)
722778
723779int hyper_parse_write_file (struct hyper_writter * writter , char * json , int length )
724780{
725- int i , n , ret = 0 ;
781+ int i , n , ret = -1 ;
726782
727783 jsmn_parser p ;
728784 int toks_num = 10 ;
729785 jsmntok_t * toks = NULL ;
730786
787+ memset (writter , 0 , sizeof (* writter ));
788+
731789 toks = calloc (toks_num , sizeof (jsmntok_t ));
790+ if (toks == NULL ) {
791+ fprintf (stderr , "fail to allocate tokens for write file cmd\n" );
792+ goto fail ;
793+ }
732794
733795 jsmn_init (& p );
734796 n = jsmn_parse (& p , json , length , toks , toks_num );
735797 /* Must be json first */
736798 if (n <= 0 ) {
737799 fprintf (stdout , "jsmn parse failed, n is %d\n" , n );
738- ret = -1 ;
739- goto out ;
800+ goto fail ;
740801 }
741802
742803 writter -> len = length - toks [0 ].end ;
743804 writter -> data = malloc (writter -> len );
744805
745- if (writter -> data == NULL )
806+ if (writter -> data == NULL ) {
807+ fprintf (stderr , "fail to allocate memory for writter data\n" );
746808 goto fail ;
809+ }
747810
748811 memcpy (writter -> data , json + toks [0 ].end , writter -> len );
749812 fprintf (stdout , "writefile get data len %d %s\n" , writter -> len , writter -> data );
@@ -767,42 +830,49 @@ int hyper_parse_write_file(struct hyper_writter *writter, char *json, int length
767830 writter -> file = strdup (json_token_str (json , & toks [i ]));
768831 fprintf (stdout , "writefile get file %s\n" , writter -> file );
769832 } else {
770- fprintf (stdout , "in writefile incorrect %s\n" , json_token_str (json , & toks [i ]));
833+ fprintf (stderr , "in writefile incorrect %s\n" , json_token_str (json , & toks [i ]));
771834 }
772835 }
836+
837+ if (writter -> id == NULL || writter -> file == NULL ) {
838+ fprintf (stderr , "writefile format incorrect\n" );
839+ goto fail ;
840+ }
841+
842+ ret = 0 ;
773843out :
774844 free (toks );
775845 return ret ;
776846fail :
777847 free (writter -> id );
778848 free (writter -> file );
779849 free (writter -> data );
780-
781- ret = -1 ;
782850 goto out ;
783851}
784852
785853int hyper_parse_read_file (struct hyper_reader * reader , char * json , int length )
786854{
787- int i , n , ret = 0 ;
855+ int i , n , ret = -1 ;
788856
789857 jsmn_parser p ;
790858 int toks_num = 10 ;
791859 jsmntok_t * toks = NULL ;
792860
861+ memset (reader , 0 , sizeof (* reader ));
862+
793863 toks = calloc (toks_num , sizeof (jsmntok_t ));
794864 if (toks == NULL ) {
795865 fprintf (stderr , "fail to allocate tokens for read file cmd\n" );
796866 ret = -1 ;
797- goto out ;
867+ goto fail ;
798868 }
799869
800870 jsmn_init (& p );
801871 n = jsmn_parse (& p , json , length , toks , toks_num );
802872 if (n < 0 ) {
803873 fprintf (stdout , "jsmn parse failed, n is %d\n" , n );
804874 ret = -1 ;
805- goto out ;
875+ goto fail ;
806876 }
807877
808878 for (i = 0 ; i < n ; i ++ ) {
@@ -828,13 +898,17 @@ int hyper_parse_read_file(struct hyper_reader *reader, char *json, int length)
828898 }
829899 }
830900
901+ if (reader -> id == NULL || reader -> file == NULL ) {
902+ fprintf (stderr , "readfile format incorrect\n" );
903+ goto fail ;
904+ }
905+
906+ ret = 0 ;
831907out :
832908 free (toks );
833909 return ret ;
834910fail :
835911 free (reader -> id );
836912 free (reader -> file );
837-
838- ret = -1 ;
839913 goto out ;
840914}
0 commit comments