5 Star 6 Fork 47

OpenHarmony/third_party_libxml2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
backport-malloc-fail-Fix-memory-leak-in-xmlXIncludeLoadTxt.patch 4.52 KB
一键复制 编辑 原始数据 按行查看 历史
冉召宇 提交于 2024-04-25 19:13 . libxml2切openEuler7.0
From ec05f04d8b5a0a60515235f65ed1256644a77741 Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer <[email protected]>
Date: Thu, 16 Feb 2023 12:40:02 +0100
Subject: [PATCH] malloc-fail: Fix memory leak in xmlXIncludeLoadTxt
Found with libFuzzer, see #344.
Reference:https://github.com/GNOME/libxml2/commit/ec05f04d8b5a0a60515235f65ed1256644a77741
Conflict:xinclude.c
---
xinclude.c | 67 +++++++++++++++++++++++-------------------------------
1 file changed, 28 insertions(+), 39 deletions(-)
diff --git a/xinclude.c b/xinclude.c
index cc22848..c0b4439 100644
--- a/xinclude.c
+++ b/xinclude.c
@@ -1791,14 +1791,15 @@ error:
static int
xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
xmlParserInputBufferPtr buf;
- xmlNodePtr node;
- xmlURIPtr uri;
- xmlChar *URL;
+ xmlNodePtr node = NULL;
+ xmlURIPtr uri = NULL;
+ xmlChar *URL = NULL;
int i;
+ int ret = -1;
xmlChar *encoding = NULL;
xmlCharEncoding enc = (xmlCharEncoding) 0;
- xmlParserCtxtPtr pctxt;
- xmlParserInputPtr inputStream;
+ xmlParserCtxtPtr pctxt = NULL;
+ xmlParserInputPtr inputStream = NULL;
int len;
const xmlChar *content;
@@ -1814,21 +1815,19 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
if (uri == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
"invalid value URI %s\n", url);
- return(-1);
+ goto error;
}
if (uri->fragment != NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_TEXT_FRAGMENT,
"fragment identifier forbidden for text: %s\n",
(const xmlChar *) uri->fragment);
- xmlFreeURI(uri);
- return(-1);
+ goto error;
}
URL = xmlSaveUri(uri);
- xmlFreeURI(uri);
if (URL == NULL) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_HREF_URI,
"invalid value URI %s\n", url);
- return(-1);
+ goto error;
}
/*
@@ -1839,8 +1838,7 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_TEXT_DOCUMENT,
"text serialization of document not available\n", NULL);
- xmlFree(URL);
- return(-1);
+ goto error;
}
/*
@@ -1870,11 +1868,8 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref,
XML_XINCLUDE_UNKNOWN_ENCODING,
"encoding %s not supported\n", encoding);
- xmlFree(encoding);
- xmlFree(URL);
- return(-1);
+ goto error;
}
- xmlFree(encoding);
}
/*
@@ -1882,27 +1877,18 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
*/
pctxt = xmlNewParserCtxt();
inputStream = xmlLoadExternalEntity((const char*)URL, NULL, pctxt);
- if(inputStream == NULL) {
- xmlFreeParserCtxt(pctxt);
- xmlFree(URL);
- return(-1);
- }
+ if(inputStream == NULL)
+ goto error;
buf = inputStream->buf;
- if (buf == NULL) {
- xmlFreeInputStream (inputStream);
- xmlFreeParserCtxt(pctxt);
- xmlFree(URL);
- return(-1);
- }
+ if (buf == NULL)
+ goto error;
if (buf->encoder)
xmlCharEncCloseFunc(buf->encoder);
buf->encoder = xmlGetCharEncodingHandler(enc);
node = xmlNewText(NULL);
if (node == NULL) {
- xmlFreeInputStream(inputStream);
- xmlFreeParserCtxt(pctxt);
- xmlFree(URL);
- return(-1);
+ xmlXIncludeErrMemory(ctxt, ctxt->incTab[nr]->ref, NULL);
+ goto error;
}
/*
@@ -1921,28 +1907,31 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) {
if (!IS_CHAR(cur)) {
xmlXIncludeErr(ctxt, ctxt->incTab[nr]->ref, XML_XINCLUDE_INVALID_CHAR,
"%s contains invalid char\n", URL);
- xmlFreeNode(node);
- xmlFreeInputStream(inputStream);
- xmlFreeParserCtxt(pctxt);
- xmlFree(URL);
- return(-1);
+ goto error;
}
i += l;
}
xmlNodeAddContentLen(node, content, len);
- xmlFreeParserCtxt(pctxt);
xmlXIncludeAddTxt(ctxt, node->content, URL);
- xmlFreeInputStream(inputStream);
loaded:
/*
* Add the element as the replacement copy.
*/
ctxt->incTab[nr]->inc = node;
+ node = NULL;
+ ret = 0;
+
+error:
+ xmlFreeNode(node);
+ xmlFreeInputStream(inputStream);
+ xmlFreeParserCtxt(pctxt);
+ xmlFree(encoding);
+ xmlFreeURI(uri);
xmlFree(URL);
- return(0);
+ return(ret);
}
/**
--
2.27.0
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openharmony/third_party_libxml2.git
[email protected]:openharmony/third_party_libxml2.git
openharmony
third_party_libxml2
third_party_libxml2
master

搜索帮助