Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

wireless.c: fix memory leak

  • Loading branch information...
commit 2b64bca186a3d465c4fe5622f8e556103dd1c6c3 1 parent 260e2d0
Xiangfu Liu authored July 08, 2012

Showing 1 changed file with 61 additions and 27 deletions. Show diff stats Hide diff stats

  1. 88  src/gui/wireless.c
88  src/gui/wireless.c
@@ -225,7 +225,7 @@ static char *jget_key(const json_t *obj, const char *key, int required)
225 225
 		return NULL;
226 226
 	}
227 227
 
228  
-	str = json_string_value(tmp);
  228
+	str = (char *)json_string_value(tmp);
229 229
 	if(!str) {
230 230
 		printf("JSON key '%s' is not a string", key);
231 231
 		return NULL;
@@ -330,32 +330,50 @@ static void scan_callback(mtk_event *e, void *arg)
330 330
 	curl = curl_easy_init();
331 331
 
332 332
 	val = json_rpc_call(curl, url, userpass, rpc_req_login, 5);
333  
-	if(!val)
  333
+	if(!val) {
  334
+		curl_easy_cleanup(curl);
334 335
 		return;
335  
-
  336
+	}
336 337
 
337 338
 	key = jget_key(val, "result", 0);
338  
-	if(!key)
  339
+	if(!key) {
  340
+		json_decref(val);
  341
+		curl_easy_cleanup(curl);
339 342
 		return;
  343
+	}
340 344
 		
341 345
 	url_token = malloc(100);
  346
+	if(!url_token) {
  347
+		json_decref(val);
  348
+		curl_easy_cleanup(curl);
  349
+		messagebox("Error", "Out of memory");
  350
+		return;
  351
+	}
  352
+
342 353
 	strcpy(url_token, URL_703N"/sys?auth=\0");
343 354
 	url_token = strcat(url_token, key);
344 355
 
345 356
 	json_decref(val);
346 357
 
347 358
 	val = json_rpc_call(curl, (char *)url_token, userpass, rpc_req_scan, 10);
348  
-	if(!val)
349  
-		goto free_url;
  359
+	if(!val) {
  360
+		free(url_token);
  361
+		curl_easy_cleanup(curl);
  362
+		return;
  363
+	}
  364
+
350 365
 	key = jget_key(val, "result", 0);
351  
-	if(!key)
352  
-		goto free_url;
  366
+	if(!key) {
  367
+		free(url_token);
  368
+		json_decref(val);
  369
+		curl_easy_cleanup(curl);
  370
+		return;
  371
+	}
  372
+
353 373
 	update_list(key);
354  
-	json_decref(val);
355 374
 
356  
-free_url:
357 375
 	free(url_token);
358  
-
  376
+	json_decref(val);
359 377
 	curl_easy_cleanup(curl);
360 378
 }
361 379
 
@@ -370,31 +388,47 @@ static int status(void)
370 388
 	char *essid, *ip, *tmp;
371 389
 	int ret = 1;
372 390
 
  391
+	mtk_cmd(appid, "l_essid.set(-text \"Disconnect\")");
  392
+	mtk_cmd(appid, "l_ip.set(-text \"0.0.0.0\")");
  393
+
373 394
 	curl = curl_easy_init();
374 395
 
375 396
 	val = json_rpc_call(curl, (char *)url, userpass, rpc_req_login, 5);
376  
-	if(!val)
  397
+	if(!val) {
  398
+		curl_easy_cleanup(curl);
377 399
 		return ret;
  400
+	}
378 401
 
379 402
 	url_token = malloc(100);
  403
+	if(!url_token) {
  404
+		json_decref(val);
  405
+		curl_easy_cleanup(curl);
  406
+		messagebox("Error", "Out of memory");
  407
+		return ret;
  408
+	}
  409
+	strcpy(url_token, URL_703N"/sys?auth=");
380 410
 
381 411
 	key = jget_key(val, "result", 0);
382  
-	strcpy(url_token, URL_703N"/sys?auth=");
383 412
 	url_token = strcat(url_token, key);
384 413
 
385 414
 	json_decref(val);
386 415
 
387 416
 	val = json_rpc_call(curl, (char *)url_token, userpass, rpc_req_status, 30);
388  
-	if(!val)
389  
-		goto free_url;
  417
+	if(!val) {
  418
+		curl_easy_cleanup(curl);
  419
+		free(url_token);
  420
+		return ret;
  421
+	}
390 422
 
391 423
 	key = jget_key(val, "result", 0);
392 424
 
393 425
 	essid = strdup(key);
394 426
 	if(!essid) {
395  
-		mtk_cmd(appid, "l_essid.set(-text \"Disconnect\")");
396  
-		mtk_cmd(appid, "l_ip.set(-text \"0.0.0.0\")");
397  
-		goto free_url;
  427
+		free(url_token);
  428
+		json_decref(val);
  429
+		curl_easy_cleanup(curl);
  430
+		messagebox("Error", "Out of memory");
  431
+		return ret;
398 432
 	}
399 433
 
400 434
 	ip = strchr(essid, '\n');
@@ -406,16 +440,13 @@ static int status(void)
406 440
 
407 441
 	mtk_cmdf(appid, "l_essid.set(-text \"%s\")", essid);
408 442
 	mtk_cmdf(appid, "l_ip.set(-text \"%s\")", ip);
409  
-	free(essid);
410  
-
411  
-	json_decref(val);
412 443
 
413  
-	ret = 0;
414  
-free_url:
  444
+	free(essid);
415 445
 	free(url_token);
  446
+	json_decref(val);
416 447
 	curl_easy_cleanup(curl);
417 448
 
418  
-	return ret;
  449
+	return 0;
419 450
 }
420 451
 
421 452
 static void status_callback(mtk_event *e, void *arg)
@@ -454,12 +485,15 @@ static void connect_callback(mtk_event *e, void *arg)
454 485
 
455 486
 	val = json_rpc_call(curl, (char *)url, userpass, rpc_req_login, 5);
456 487
 	if(!val) {
  488
+		curl_easy_cleanup(curl);
457 489
 		messagebox("Error", "Cannot reach wireless gadget");
458 490
 		return;
459 491
 	}
460 492
 
461 493
 	url_token = malloc(100);
462 494
 	if(!url_token) {
  495
+		json_decref(val);
  496
+		curl_easy_cleanup(curl);
463 497
 		messagebox("Error", "Out of memory");
464 498
 		return;
465 499
 	}
@@ -481,16 +515,16 @@ static void connect_callback(mtk_event *e, void *arg)
481 515
 
482 516
 	val = json_rpc_call(curl, (char *)url_token, userpass, rpc_req_set, 30);
483 517
 	if(!val) {
  518
+		free(url_token);
  519
+		curl_easy_cleanup(curl);
484 520
 		messagebox("Error", "Cannot reach wireless gadget");
485  
-		goto free_url;
  521
+		return;
486 522
 	}
487 523
 	json_decref(val);
488 524
 
489 525
 	status();
490 526
 
491  
-free_url:
492 527
 	free(url_token);
493  
-
494 528
 	curl_easy_cleanup(curl);
495 529
 }
496 530
 

0 notes on commit 2b64bca

Please sign in to comment.
Something went wrong with that request. Please try again.