[json] Avoid leaks on error code paths

Bug 1476 - JSON Parser memory leak

Static analysis of the code showed that the in-tree copy of
the JsonParser object leaks objects and arrays on parse errors.

Thanks to Gordon Williams <gordon.williams@collabora.co.uk>
This commit is contained in:
Emmanuele Bassi 2009-04-15 12:18:55 +01:00
parent 269598a2cf
commit 5af99660cb

View File

@ -378,6 +378,7 @@ json_parse_array (JsonParser *parser,
{ {
json_node_free (node); json_node_free (node);
json_array_unref (array); json_array_unref (array);
return token; return token;
} }
@ -411,6 +412,7 @@ json_parse_array (JsonParser *parser,
{ {
json_node_free (node); json_node_free (node);
json_array_unref (array); json_array_unref (array);
return token; return token;
} }
@ -440,6 +442,8 @@ json_parse_array (JsonParser *parser,
} }
else else
{ {
json_array_unref (array);
return G_TOKEN_INT; return G_TOKEN_INT;
} }
} }
@ -475,6 +479,7 @@ json_parse_array (JsonParser *parser,
break; break;
default: default:
json_array_unref (array);
return G_TOKEN_RIGHT_BRACE; return G_TOKEN_RIGHT_BRACE;
} }
@ -542,6 +547,7 @@ json_parse_object (JsonParser *parser,
{ {
g_free (name); g_free (name);
json_object_unref (object); json_object_unref (object);
return ':'; return ':';
} }
else else
@ -554,6 +560,7 @@ json_parse_object (JsonParser *parser,
if (!name) if (!name)
{ {
json_object_unref (object); json_object_unref (object);
return G_TOKEN_STRING; return G_TOKEN_STRING;
} }
@ -576,6 +583,7 @@ json_parse_object (JsonParser *parser,
json_node_free (node); json_node_free (node);
json_object_unref (object); json_object_unref (object);
return token; return token;
} }
@ -611,6 +619,7 @@ json_parse_object (JsonParser *parser,
g_free (name); g_free (name);
json_node_free (node); json_node_free (node);
json_object_unref (object); json_object_unref (object);
return token; return token;
} }
@ -641,6 +650,8 @@ json_parse_object (JsonParser *parser,
} }
else else
{ {
json_object_unref (object);
return G_TOKEN_INT; return G_TOKEN_INT;
} }
} }
@ -676,6 +687,7 @@ json_parse_object (JsonParser *parser,
break; break;
default: default:
json_object_unref (object);
return G_TOKEN_SYMBOL; return G_TOKEN_SYMBOL;
} }
@ -948,7 +960,7 @@ json_parser_load_from_data (JsonParser *parser,
if (symbols[i].token == expected_token) if (symbols[i].token == expected_token)
symbol_name = symbol_names + symbols[i].name_offset; symbol_name = symbol_names + symbols[i].name_offset;
if (msg) if (!msg)
msg = g_strconcat ("e.g. '", symbol_name, "'", NULL); msg = g_strconcat ("e.g. '", symbol_name, "'", NULL);
} }