@matheusbsbs / matheus-bagliano / commits / dafc88b136
Fork 0

cleanups: prevent leak of two strduped strings in config.c

Config_filename and lockfile are strduped and then leaked in git_config_set_multivar. Signed-off-by: Serge E. Hallyn <serue@us.ibm.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Serge E. Hallyn committed Apr 17, 2006 at 10:14 UTC dafc88b136b508c1c08129b756b6ba23b7c9d341
1 file changed +28 -11
config.c
+28 -11
@@ -420,6 +420,7 @@ int git_config_set_multivar(const char* key, const char* value,
420 {
421 int i;
422 int fd, in_fd;
423 + int ret;
424 char* config_filename = strdup(git_path("config"));
425 char* lock_file = strdup(git_path("config.lock"));
426 const char* last_dot = strrchr(key, '.');
@@ -429,9 +430,10 @@ int git_config_set_multivar(const char* key, const char* value,
430 * key name separated by a dot, we have to know where the dot is.
431 */
432
432 - if (last_dot == NULL) {
433 + if (last_dot == NULL) {
434 fprintf(stderr, "key does not contain a section: %s\n", key);
434 - return 2;
435 + ret = 2;
436 + goto out_free;
437 }
438 store.baselen = last_dot - key;
439
@@ -447,7 +449,8 @@ int git_config_set_multivar(const char* key, const char* value,
449 (i == store.baselen+1 && !isalpha(key[i])))) {
450 fprintf(stderr, "invalid key: %s\n", key);
451 free(store.key);
450 - return 1;
452 + ret = 1;
453 + goto out_free;
454 } else
455 store.key[i] = tolower(key[i]);
456 store.key[i] = 0;
@@ -460,7 +463,8 @@ int git_config_set_multivar(const char* key, const char* value,
463 if (fd < 0) {
464 fprintf(stderr, "could not lock config file\n");
465 free(store.key);
463 - return -1;
466 + ret = -1;
467 + goto out_free;
468 }
469
470 /*
@@ -475,13 +479,15 @@ int git_config_set_multivar(const char* key, const char* value,
479 strerror(errno));
480 close(fd);
481 unlink(lock_file);
478 - return 3; /* same as "invalid config file" */
482 + ret = 3; /* same as "invalid config file" */
483 + goto out_free;
484 }
485 /* if nothing to unset, error out */
486 if (value == NULL) {
487 close(fd);
488 unlink(lock_file);
484 - return 5;
489 + ret = 5;
490 + goto out_free;
491 }
492
493 store.key = (char*)key;
@@ -507,7 +513,8 @@ int git_config_set_multivar(const char* key, const char* value,
513 fprintf(stderr, "Invalid pattern: %s\n",
514 value_regex);
515 free(store.value_regex);
510 - return 6;
516 + ret = 6;
517 + goto out_free;
518 }
519 }
520
@@ -528,7 +535,8 @@ int git_config_set_multivar(const char* key, const char* value,
535 regfree(store.value_regex);
536 free(store.value_regex);
537 }
531 - return 3;
538 + ret = 3;
539 + goto out_free;
540 }
541
542 free(store.key);
@@ -542,7 +550,8 @@ int git_config_set_multivar(const char* key, const char* value,
550 (store.seen > 1 && multi_replace == 0)) {
551 close(fd);
552 unlink(lock_file);
545 - return 5;
553 + ret = 5;
554 + goto out_free;
555 }
556
557 fstat(in_fd, &st);
@@ -593,10 +602,18 @@ int git_config_set_multivar(const char* key, const char* value,
602
603 if (rename(lock_file, config_filename) < 0) {
604 fprintf(stderr, "Could not rename the lock file?\n");
596 - return 4;
605 + ret = 4;
606 + goto out_free;
607 }
608
599 - return 0;
609 + ret = 0;
610 +
611 +out_free:
612 + if (config_filename)
613 + free(config_filename);
614 + if (lock_file)
615 + free(lock_file);
616 + return ret;
617 }
618
619