@@ -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+
5985static 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+
114154static 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+
166220static 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+
214282static 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
246340static 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
338436static 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
368473static int hyper_parse_interfaces (struct hyper_pod * pod , char * json , jsmntok_t * toks )
0 commit comments