Commit 35fdf8e7 authored by Nikias Bassen's avatar Nikias Bassen

xplist: Prevent memory leaks when parsing fails

parent c98451f9
...@@ -944,9 +944,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -944,9 +944,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
if (!tp) { if (!tp) {
PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
free(tag); ctx->err++;
free(keyname); goto err_out;
break;
} }
if (tp->begin) { if (tp->begin) {
int requires_free = 0; int requires_free = 0;
...@@ -955,9 +954,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -955,9 +954,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); PLIST_XML_ERR("Could not get text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
break;
} }
char *str = str_content; char *str = str_content;
int is_negative = 0; int is_negative = 0;
...@@ -998,9 +995,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -998,9 +995,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
if (!tp) { if (!tp) {
PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
free(tag); ctx->err++;
free(keyname); goto err_out;
break;
} }
if (tp->begin) { if (tp->begin) {
int requires_free = 0; int requires_free = 0;
...@@ -1009,9 +1005,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1009,9 +1005,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); PLIST_XML_ERR("Could not get text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
break;
} }
data->realval = atof(str_content); data->realval = atof(str_content);
if (requires_free) { if (requires_free) {
...@@ -1045,18 +1039,15 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1045,18 +1039,15 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
if (!tp) { if (!tp) {
PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
free(tag); ctx->err++;
free(keyname); goto err_out;
break;
} }
str = text_parts_get_content(tp, 1, &length, NULL); str = text_parts_get_content(tp, 1, &length, NULL);
text_parts_free(first_part.next); text_parts_free(first_part.next);
if (!str) { if (!str) {
PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); PLIST_XML_ERR("Could not get text content for '%s' node\n", tag);
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
return;
} }
if (!strcmp(tag, "key") && !keyname && *plist && (plist_get_node_type(*plist) == PLIST_DICT)) { if (!strcmp(tag, "key") && !keyname && *plist && (plist_get_node_type(*plist) == PLIST_DICT)) {
keyname = str; keyname = str;
...@@ -1080,9 +1071,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1080,9 +1071,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
if (!tp) { if (!tp) {
PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
free(tag); ctx->err++;
free(keyname); goto err_out;
break;
} }
if (tp->begin) { if (tp->begin) {
int requires_free = 0; int requires_free = 0;
...@@ -1091,9 +1081,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1091,9 +1081,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); PLIST_XML_ERR("Could not get text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
break;
} }
size_t size = tp->length; size_t size = tp->length;
data->buff = base64decode(str_content, &size); data->buff = base64decode(str_content, &size);
...@@ -1113,9 +1101,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1113,9 +1101,8 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
if (!tp) { if (!tp) {
PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag); PLIST_XML_ERR("Could not parse text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
free(tag); ctx->err++;
free(keyname); goto err_out;
break;
} }
Time64_T timev = 0; Time64_T timev = 0;
if (tp->begin) { if (tp->begin) {
...@@ -1126,9 +1113,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1126,9 +1113,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
PLIST_XML_ERR("Could not get text content for '%s' node\n", tag); PLIST_XML_ERR("Could not get text content for '%s' node\n", tag);
text_parts_free(first_part.next); text_parts_free(first_part.next);
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
break;
} }
if ((length >= 11) && (length < 32)) { if ((length >= 11) && (length < 32)) {
...@@ -1157,9 +1142,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1157,9 +1142,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
PLIST_XML_ERR("Unexpected tag <%s%s> encountered\n", tag, (is_empty) ? "/" : ""); PLIST_XML_ERR("Unexpected tag <%s%s> encountered\n", tag, (is_empty) ? "/" : "");
ctx->pos = ctx->end; ctx->pos = ctx->end;
ctx->err++; ctx->err++;
free(tag); goto err_out;
free(keyname);
return;
} }
if (subnode && !closing_tag) { if (subnode && !closing_tag) {
/* parse sub nodes for structured types */ /* parse sub nodes for structured types */
...@@ -1169,9 +1152,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1169,9 +1152,7 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
node_from_xml(ctx, &subnode, depth+1); node_from_xml(ctx, &subnode, depth+1);
if (ctx->err) { if (ctx->err) {
/* make sure to bail out if parsing failed */ /* make sure to bail out if parsing failed */
free(tag); goto err_out;
free(keyname);
return;
} }
if ((data->type == PLIST_DICT) && (plist_dict_get_size(subnode) == 1)) { if ((data->type == PLIST_DICT) && (plist_dict_get_size(subnode) == 1)) {
/* convert XML CF$UID dictionaries to PLIST_UID nodes */ /* convert XML CF$UID dictionaries to PLIST_UID nodes */
...@@ -1239,11 +1220,13 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth) ...@@ -1239,11 +1220,13 @@ static void node_from_xml(parse_ctx ctx, plist_t *plist, uint32_t depth)
break; break;
} }
} }
err_out:
free(tag); free(tag);
free(keyname); free(keyname);
keyname = NULL; keyname = NULL;
plist_free(subnode); plist_free(subnode);
if (closing_tag) { subnode = NULL;
if (closing_tag || ctx->err) {
break; break;
} }
} }
......
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