فهرست منبع

modules/carrierroute: Prevent resource leak in case of error

the 2 FILE* opened in file mode where not properly closed on all code paths
(cherry picked from commit 821a4322a0902b827add7913705f72d5e50176b5)
Marius Zbihlei 15 سال پیش
والد
کامیت
17e5609c9e
1فایلهای تغییر یافته به همراه7 افزوده شده و 3 حذف شده
  1. 7 3
      modules/carrierroute/cr_config.c

+ 7 - 3
modules/carrierroute/cr_config.c

@@ -157,18 +157,18 @@ static int backup_config(void) {
 		ch = fgetc(from);
 		if (ferror(from)) {
 			LM_ERR("Error reading source file.\n");
-			goto errout;
+			goto errclose;
 		}
 		if (!feof(from)) fputc(ch, to);
 		if (ferror(to)) {
 			LM_ERR("Error writing destination file.\n");
-			goto errout;
+			goto errclose;
 		}
 	}
 
 	if (fclose(from)==EOF) {
 		LM_ERR("Error closing source file.\n");
-		goto errout;
+		goto errclose;
 	}
 
 	if (fclose(to)==EOF) {
@@ -178,6 +178,10 @@ static int backup_config(void) {
 	LM_NOTICE("backup written to %s\n", backup_file);
 	pkg_free(backup_file);
 	return 0;
+errclose:
+	/* close the files so that resource leak is prevented ; ignore errors*/
+	fclose(from);
+	fclose(to);
 errout:
 	pkg_free(backup_file);
 	return -1;