Kaydet (Commit) cb72e1d8 authored tarafından Colomban Wendling's avatar Colomban Wendling

Fix possible crashers in filetypes_detect_from_file_internal()

* Never try to do a regex match on a NULL string;
* Don't try to unref a possibly NULL regex.
üst 70f41f64
...@@ -962,7 +962,8 @@ static GeanyFiletype *find_shebang(const gchar *utf8_filename, const gchar *line ...@@ -962,7 +962,8 @@ static GeanyFiletype *find_shebang(const gchar *utf8_filename, const gchar *line
} }
/* Detect the filetype checking for a shebang, then filename extension. */ /* Detect the filetype checking for a shebang, then filename extension.
* @lines: an strv of the lines to scan (must containing at least one line) */
static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_filename, static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_filename,
gchar **lines) gchar **lines)
{ {
...@@ -970,7 +971,6 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file ...@@ -970,7 +971,6 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file
gint i; gint i;
GRegex *ft_regex; GRegex *ft_regex;
GMatchInfo *match; GMatchInfo *match;
GError *regerr = NULL;
/* try to find a shebang and if found use it prior to the filename extension /* try to find a shebang and if found use it prior to the filename extension
* also checks for <?xml */ * also checks for <?xml */
...@@ -979,13 +979,11 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file ...@@ -979,13 +979,11 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file
return ft; return ft;
/* try to extract the filetype using a regex capture */ /* try to extract the filetype using a regex capture */
ft_regex = g_regex_new( file_prefs.extract_filetype_regex, ft_regex = g_regex_new(file_prefs.extract_filetype_regex,
G_REGEX_RAW | G_REGEX_MULTILINE, G_REGEX_RAW | G_REGEX_MULTILINE, 0, NULL);
0, if (ft_regex != NULL)
&regerr);
if (regerr == NULL)
{ {
for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; i++) for (i = 0; ft == NULL && lines[i] != NULL; i++)
{ {
if (g_regex_match(ft_regex, lines[i], 0, &match)) if (g_regex_match(ft_regex, lines[i], 0, &match))
{ {
...@@ -997,15 +995,9 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file ...@@ -997,15 +995,9 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file
} }
} }
g_match_info_free(match); g_match_info_free(match);
if (ft != NULL)
break;
} }
g_regex_unref(ft_regex);
} }
else
{
g_error_free(regerr);
}
g_regex_unref(ft_regex);
if (ft != NULL) if (ft != NULL)
return ft; return ft;
...@@ -1020,7 +1012,7 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file ...@@ -1020,7 +1012,7 @@ static GeanyFiletype *filetypes_detect_from_file_internal(const gchar *utf8_file
GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc) GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
{ {
GeanyFiletype *ft; GeanyFiletype *ft;
gchar *lines[GEANY_FILETYPE_SEARCH_LINES]; gchar *lines[GEANY_FILETYPE_SEARCH_LINES + 1];
gint i; gint i;
if (doc == NULL) if (doc == NULL)
...@@ -1030,6 +1022,7 @@ GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc) ...@@ -1030,6 +1022,7 @@ GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
{ {
lines[i] = sci_get_line(doc->editor->sci, i); lines[i] = sci_get_line(doc->editor->sci, i);
} }
lines[i] = NULL;
ft = filetypes_detect_from_file_internal(doc->file_name, lines); ft = filetypes_detect_from_file_internal(doc->file_name, lines);
for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i) for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
{ {
...@@ -1052,10 +1045,9 @@ GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc) ...@@ -1052,10 +1045,9 @@ GeanyFiletype *filetypes_detect_from_document(GeanyDocument *doc)
GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename) GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename)
{ {
gchar line[1024]; gchar line[1024];
gchar *lines[GEANY_FILETYPE_SEARCH_LINES]; gchar *lines[2];
FILE *f; FILE *f;
gchar *locale_name = utils_get_locale_from_utf8(utf8_filename); gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
gint i;
f = g_fopen(locale_name, "r"); f = g_fopen(locale_name, "r");
g_free(locale_name); g_free(locale_name);
...@@ -1064,11 +1056,8 @@ GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename) ...@@ -1064,11 +1056,8 @@ GeanyFiletype *filetypes_detect_from_file(const gchar *utf8_filename)
if (fgets(line, sizeof(line), f) != NULL) if (fgets(line, sizeof(line), f) != NULL)
{ {
fclose(f); fclose(f);
for (i = 0; i < GEANY_FILETYPE_SEARCH_LINES; ++i)
{
lines[i] = NULL;
}
lines[0] = line; lines[0] = line;
lines[1] = NULL;
return filetypes_detect_from_file_internal(utf8_filename, lines); return filetypes_detect_from_file_internal(utf8_filename, lines);
} }
fclose(f); fclose(f);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment