gcontenttype: Fix strchr failure leading to a NULL dereference

This commit is contained in:
Egor Bychin 2021-10-11 13:52:26 +03:00
parent cbb2a51a5b
commit 36531f7015

View File

@ -997,6 +997,8 @@ parse_header (gchar *line)
line[len - 1] = 0; line[len - 1] = 0;
s = strchr (line, ':'); s = strchr (line, ':');
if (s == NULL)
return NULL;
match = g_slice_new0 (TreeMatch); match = g_slice_new0 (TreeMatch);
match->priority = atoi (line + 1); match->priority = atoi (line + 1);
@ -1025,9 +1027,13 @@ parse_match_line (gchar *line,
{ {
*depth = atoi (line); *depth = atoi (line);
s = strchr (line, '>'); s = strchr (line, '>');
if (s == NULL)
goto handle_error;
} }
s += 2; s += 2;
p = strchr (s, '"'); p = strchr (s, '"');
if (p == NULL)
goto handle_error;
*p = 0; *p = 0;
matchlet->path = g_strdup (s); matchlet->path = g_strdup (s);
@ -1058,6 +1064,10 @@ parse_match_line (gchar *line,
g_strfreev (parts); g_strfreev (parts);
return matchlet; return matchlet;
handle_error:
g_slice_free (TreeMatchlet, matchlet);
return NULL;
} }
static gint static gint
@ -1119,7 +1129,7 @@ read_tree_magic_from_directory (const gchar *prefix)
gchar *text; gchar *text;
gsize len; gsize len;
gchar **lines; gchar **lines;
gint i; gsize i;
TreeMatch *match; TreeMatch *match;
TreeMatchlet *matchlet; TreeMatchlet *matchlet;
gint depth; gint depth;
@ -1134,14 +1144,18 @@ read_tree_magic_from_directory (const gchar *prefix)
match = NULL; match = NULL;
for (i = 0; lines[i] && lines[i][0]; i++) for (i = 0; lines[i] && lines[i][0]; i++)
{ {
if (lines[i][0] == '[') if (lines[i][0] == '[' && (match = parse_header (lines[i])) != NULL)
{ {
match = parse_header (lines[i]);
insert_match (match); insert_match (match);
} }
else if (match != NULL) else if (match != NULL)
{ {
matchlet = parse_match_line (lines[i], &depth); matchlet = parse_match_line (lines[i], &depth);
if (matchlet == NULL)
{
g_warning ("%s: body corrupt; skipping", filename);
break;
}
insert_matchlet (match, matchlet, depth); insert_matchlet (match, matchlet, depth);
} }
else else