You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
To reproduce, open a file in a libeditorconfig client from a directory whose ancestry contains no .editorconfig files. A leaks report (output of leaks <pid> on macOS will show something like this:
if (eh->name_value_count==0) { /* no value is set, just return 0. */free(hfp.full_filename);
free(config_files);
return0;
The free(config_files) frees only the array block itself, and not the strings inside of it. This antipattern also occurs in the error case for reallocing the value array:
if (eh->name_values==NULL) {
free(hfp.full_filename);
free(config_files);
returnEDITORCONFIG_PARSE_MEMORY_ERROR;
}
I refactored the code at the cleanup: label into a separate function, and called that to ensure that the string array is properly freed. Patch follows:
diff --git a/src/lib/editorconfig.c b/src/lib/editorconfig.c
index e5262ca..6af834a 100644
--- a/src/lib/editorconfig.c
+++ b/src/lib/editorconfig.c
@@ -383,6 +383,20 @@ failure_cleanup:
return NULL;
}
+/*
+ * Free the memory used by an array of strings that was created by
+ * get_filenames().
+ */
+static void free_filenames(char **filenames)
+{
+ if (filenames != NULL) {
+ for (char** filename = filenames; *filename != NULL; filename++) {
+ free(*filename);
+ }
+ free(filenames);
+ }
+}
+
/*
* version number comparison
*/
@@ -564,7 +578,7 @@ int editorconfig_parse(const char* full_filename, editorconfig_handle h)
if (eh->name_value_count == 0) { /* no value is set, just return 0. */
free(hfp.full_filename);
- free(config_files);
+ free_filenames(config_files);
return 0;
}
eh->name_values = hfp.array_name_value.name_values;
@@ -573,17 +587,12 @@ int editorconfig_parse(const char* full_filename, editorconfig_handle h)
sizeof(editorconfig_name_value) * eh->name_value_count);
if (eh->name_values == NULL) {
free(hfp.full_filename);
+ free_filenames(config_files);
return EDITORCONFIG_PARSE_MEMORY_ERROR;
}
cleanup:
-
- if (config_files != NULL) {
- for (config_file = config_files; *config_file != NULL; config_file++) {
- free(*config_file);
- }
- free(config_files);
- }
+ free_filenames(config_files);
free(hfp.full_filename);
free(hfp.editorconfig_file_dir);
The text was updated successfully, but these errors were encountered:
To reproduce, open a file in a
libeditorconfig
client from a directory whose ancestry contains no.editorconfig
files. A leaks report (output ofleaks <pid>
on macOS will show something like this:The origin of the leak is the "no values" case:
The
free(config_files)
frees only the array block itself, and not the strings inside of it. This antipattern also occurs in the error case for reallocing the value array:I refactored the code at the
cleanup:
label into a separate function, and called that to ensure that the string array is properly freed. Patch follows:The text was updated successfully, but these errors were encountered: