@@ -203,92 +203,102 @@ static int __parse_mac(uint8_t mac[6], const char* mac_str)
203
203
}
204
204
205
205
206
- static void __parse_vif (json_t * vif , config * conf )
206
+ static void __parse_vifs (json_t * vifs , config * conf )
207
207
{
208
208
int ret ;
209
209
210
- int vid ;
210
+ size_t idx ;
211
+ json_t * vif ;
211
212
json_t * value ;
212
213
const char * key ;
213
214
214
- if (!json_is_object ( vif )) {
215
- fprintf (stderr , "Parameter 'vifs' contains invalid element. Must be array of objects .\n" );
215
+ if (!json_is_array ( vifs )) {
216
+ fprintf (stderr , "Parameter 'vifs' has invalid type, must be array.\n" );
216
217
conf -> error = true;
217
218
return ;
218
219
}
219
220
220
- if ( conf -> vifs_count >= DEV_MAX_COUNT ) {
221
- fprintf ( stderr , "Too many vifs defined. Maximum %d supported.\n" , DEV_MAX_COUNT );
222
- conf -> error = true;
223
- return ;
224
- }
221
+ json_array_foreach ( vifs , idx , vif ) {
222
+ /* In case of error the loop will `continue` without incrementing the
223
+ * counter, so do it at the beginning and access `vifs_count - 1`.
224
+ */
225
+ conf -> vifs_count ++ ;
225
226
226
- vid = conf -> vifs_count ;
227
+ if (conf -> vifs_count >= DEV_MAX_COUNT ) {
228
+ fprintf (stderr , "Too many vifs defined. Maximum %d supported.\n" , DEV_MAX_COUNT );
229
+ conf -> error = true;
230
+ break ;
231
+ }
227
232
228
- json_object_foreach (vif , key , value ) {
229
- if (strcmp (key , "ip" ) == 0 ) {
230
- if (conf -> vifs [vid ].ip_set ) {
231
- fprintf (stderr , "Parameter 'ip' defined multiple times.\n" );
232
- conf -> error = true;
233
- continue ;
234
- }
233
+ if (!json_is_object (vif )) {
234
+ fprintf (stderr , "Parameter 'vifs' contains invalid element. Must be array of objects.\n" );
235
+ conf -> error = true;
236
+ continue ;
237
+ }
235
238
236
- conf -> vifs [vid ].ip_set = true;
239
+ json_object_foreach (vif , key , value ) {
240
+ if (strcmp (key , "ip" ) == 0 ) {
241
+ if (conf -> vifs [conf -> vifs_count - 1 ].ip_set ) {
242
+ fprintf (stderr , "Parameter 'ip' defined multiple times.\n" );
243
+ conf -> error = true;
244
+ continue ;
245
+ }
237
246
238
- const char * ip_str = json_string_value (value );
239
- if (ip_str == NULL ) {
240
- fprintf (stderr , "Parameter 'ip' has invalid type, must be string.\n" );
241
- conf -> error = true;
242
- continue ;
243
- }
247
+ conf -> vifs [conf -> vifs_count - 1 ].ip_set = true;
244
248
245
- ret = __parse_ip (& (conf -> vifs [vid ].ip ), ip_str );
246
- if (ret ) {
247
- fprintf (stderr , "Parameter 'ip' is an invalid IP.\n" );
248
- conf -> error = true;
249
- }
250
- } else if (strcmp (key , "mac" ) == 0 ) {
251
- if (conf -> vifs [vid ].mac_set ) {
252
- fprintf (stderr , "Parameter 'mac' defined multiple times.\n" );
253
- conf -> error = true;
254
- continue ;
255
- }
249
+ const char * ip_str = json_string_value (value );
250
+ if (ip_str == NULL ) {
251
+ fprintf (stderr , "Parameter 'ip' has invalid type, must be string.\n" );
252
+ conf -> error = true;
253
+ continue ;
254
+ }
255
+
256
+ ret = __parse_ip (& (conf -> vifs [conf -> vifs_count - 1 ].ip ), ip_str );
257
+ if (ret ) {
258
+ fprintf (stderr , "Parameter 'ip' is an invalid IP.\n" );
259
+ conf -> error = true;
260
+ }
261
+ } else if (strcmp (key , "mac" ) == 0 ) {
262
+ if (conf -> vifs [conf -> vifs_count - 1 ].mac_set ) {
263
+ fprintf (stderr , "Parameter 'mac' defined multiple times.\n" );
264
+ conf -> error = true;
265
+ continue ;
266
+ }
256
267
257
- conf -> vifs [vid ].mac_set = true;
268
+ conf -> vifs [conf -> vifs_count - 1 ].mac_set = true;
258
269
259
- const char * mac_str = json_string_value (value );
260
- if (mac_str == NULL ) {
261
- fprintf (stderr , "Parameter 'mac' has invalid type, must be string.\n" );
262
- conf -> error = true;
263
- continue ;
264
- }
270
+ const char * mac_str = json_string_value (value );
271
+ if (mac_str == NULL ) {
272
+ fprintf (stderr , "Parameter 'mac' has invalid type, must be string.\n" );
273
+ conf -> error = true;
274
+ continue ;
275
+ }
265
276
266
- ret = __parse_mac (conf -> vifs [vid ].mac , mac_str );
267
- if (ret ) {
268
- fprintf (stderr , "Parameter 'mac' is an invalid MAC.\n" );
269
- conf -> error = true;
270
- }
271
- } else if (strcmp (key , "bridge" ) == 0 ) {
272
- if (conf -> vifs [vid ].bridge_set ) {
273
- fprintf (stderr , "Parameter 'bridge' defined multiple times.\n" );
274
- conf -> error = true;
275
- continue ;
276
- }
277
+ ret = __parse_mac (conf -> vifs [conf -> vifs_count - 1 ].mac , mac_str );
278
+ if (ret ) {
279
+ fprintf (stderr , "Parameter 'mac' is an invalid MAC.\n" );
280
+ conf -> error = true;
281
+ }
282
+ } else if (strcmp (key , "bridge" ) == 0 ) {
283
+ if (conf -> vifs [conf -> vifs_count - 1 ].bridge_set ) {
284
+ fprintf (stderr , "Parameter 'bridge' defined multiple times.\n" );
285
+ conf -> error = true;
286
+ continue ;
287
+ }
277
288
278
- conf -> vifs [vid ].bridge_set = true;
289
+ conf -> vifs [conf -> vifs_count - 1 ].bridge_set = true;
279
290
280
- conf -> vifs [vid ].bridge = json_string_value (value );
281
- if (conf -> vifs [vid ].bridge == NULL ) {
282
- fprintf (stderr , "Parameter 'bridge' has invalid type, must be string.\n" );
291
+ conf -> vifs [conf -> vifs_count - 1 ].bridge = json_string_value (value );
292
+ if (conf -> vifs [conf -> vifs_count - 1 ].bridge == NULL ) {
293
+ fprintf (stderr , "Parameter 'bridge' has invalid type, must be string.\n" );
294
+ conf -> error = true;
295
+ }
296
+ } else {
297
+ fprintf (stderr , "Invalid parameter '%s' on vif definition.\n" , key );
283
298
conf -> error = true;
284
299
}
285
- } else {
286
- fprintf (stderr , "Invalid parameter '%s' on vif definition.\n" , key );
287
- conf -> error = true;
288
300
}
289
301
}
290
-
291
- conf -> vifs_count ++ ;
292
302
}
293
303
294
304
static void __parse_vcpus (json_t * vcpus , config * conf )
@@ -448,9 +458,7 @@ static void __parse_xen(json_t* xen, config* conf)
448
458
449
459
static void __parse_root (json_t * root , config * conf )
450
460
{
451
- size_t idx ;
452
461
json_t * value ;
453
- json_t * vif ;
454
462
const char * key ;
455
463
456
464
if (!json_is_object (root )) {
@@ -551,15 +559,7 @@ static void __parse_root(json_t* root, config* conf)
551
559
552
560
conf -> vifs_set = true;
553
561
554
- if (!json_is_array (value )) {
555
- fprintf (stderr , "Parameter 'vifs' has invalid type, must be array.\n" );
556
- conf -> error = true;
557
- continue ;
558
- }
559
-
560
- json_array_foreach (value , idx , vif ) {
561
- __parse_vif (vif , conf );
562
- }
562
+ __parse_vifs (value , conf );
563
563
} else if (strcmp (key , "paused" ) == 0 ) {
564
564
if (conf -> paused_set ) {
565
565
fprintf (stderr , "Parameter 'paused' defined multiple times.\n" );
0 commit comments