1From ee6c6084e58ab114bddd06453790d22b08e45d93 Mon Sep 17 00:00:00 2001
2From: Nick Wellnhofer <wellnhofer@aevum.de>
3Date: Sun, 13 Nov 2022 16:30:46 +0100
4Subject: [PATCH] io: Remove xmlInputReadCallbackNop
5
6In some cases, for example when using encoders, the read callback was
7set to NULL, in other cases it was set to xmlInputReadCallbackNop.
8xmlGROW only tested for xmlInputReadCallbackNop, resulting in errors
9when parsing large encoded content from memory.
10
11Always use a NULL callback for memory buffers to avoid ambiguities.
12
13Fixes #262.
14
15Reference:https://github.com/GNOME/libxml2/commit/46cd7d224ed5c4cdbd4f72ec899db24e18d21fe7
16Conflict:include/private/io.h
17---
18 parser.c          |  2 +-
19 parserInternals.c |  3 ++-
20 xmlIO.c           | 30 ++++--------------------------
21 3 files changed, 7 insertions(+), 28 deletions(-)
22
23diff --git a/parser.c b/parser.c
24index adc449c..f13287a 100644
25--- a/parser.c
26+++ b/parser.c
27@@ -2134,7 +2134,7 @@ static void xmlGROW (xmlParserCtxtPtr ctxt) {
28     if (((curEnd > XML_MAX_LOOKUP_LIMIT) ||
29          (curBase > XML_MAX_LOOKUP_LIMIT)) &&
30          ((ctxt->input->buf) &&
31-          (ctxt->input->buf->readcallback != xmlInputReadCallbackNop)) &&
32+          (ctxt->input->buf->readcallback != NULL)) &&
33         ((ctxt->options & XML_PARSE_HUGE) == 0)) {
34         xmlFatalErr(ctxt, XML_ERR_INTERNAL_ERROR, "Huge input lookup");
35         xmlHaltParser(ctxt);
36diff --git a/parserInternals.c b/parserInternals.c
37index 0ef44fe..ef18ccf 100644
38--- a/parserInternals.c
39+++ b/parserInternals.c
40@@ -311,7 +311,8 @@ xmlParserInputGrow(xmlParserInputPtr in, int len) {
41     if (in->buf->buffer == NULL) return(-1);
42 
43     /* Don't grow memory buffers. */
44-    if (in->buf->readcallback == NULL) return(0);
45+    if ((in->buf->encoder == NULL) && (in->buf->readcallback == NULL))
46+        return(0);
47 
48     CHECK_BUFFER(in);
49 
50diff --git a/xmlIO.c b/xmlIO.c
51index 0762034..71c9fbf 100644
52--- a/xmlIO.c
53+++ b/xmlIO.c
54@@ -729,20 +729,6 @@ xmlCheckFilename (const char *path)
55     return 1;
56 }
57 
58-/**
59- * xmlInputReadCallbackNop:
60- *
61- * No Operation xmlInputReadCallback function, does nothing.
62- *
63- * Returns zero
64- */
65-int
66-xmlInputReadCallbackNop(void *context ATTRIBUTE_UNUSED,
67-                        char *buffer ATTRIBUTE_UNUSED,
68-                        int len ATTRIBUTE_UNUSED) {
69-    return(0);
70-}
71-
72 /**
73  * xmlFdRead:
74  * @context:  the I/O context
75@@ -2963,7 +2949,7 @@ xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) {
76     ret = xmlAllocParserInputBuffer(enc);
77     if (ret != NULL) {
78         ret->context = (void *) mem;
79-	ret->readcallback = xmlInputReadCallbackNop;
80+	ret->readcallback = NULL;
81 	ret->closecallback = NULL;
82 	errcode = xmlBufAdd(ret->buffer, (const xmlChar *) mem, size);
83 	if (errcode != 0) {
84@@ -3261,10 +3247,8 @@ xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
85 	res = in->readcallback(in->context, &buffer[0], len);
86 	if (res <= 0)
87 	    in->readcallback = endOfInput;
88-    } else {
89-	xmlIOErr(XML_IO_NO_INPUT, NULL);
90-	in->error = XML_IO_NO_INPUT;
91-	return(-1);
92+    } else if (in->encoder == NULL) {
93+	return(0);
94     }
95     if (res < 0) {
96 	return(-1);
97@@ -3331,13 +3315,7 @@ xmlParserInputBufferGrow(xmlParserInputBufferPtr in, int len) {
98  */
99 int
100 xmlParserInputBufferRead(xmlParserInputBufferPtr in, int len) {
101-    if ((in == NULL) || (in->error)) return(-1);
102-    if (in->readcallback != NULL)
103-	return(xmlParserInputBufferGrow(in, len));
104-    else if (xmlBufGetAllocationScheme(in->buffer) == XML_BUFFER_ALLOC_IMMUTABLE)
105-	return(0);
106-    else
107-        return(-1);
108+    return(xmlParserInputBufferGrow(in, len));
109 }
110 
111 #ifdef LIBXML_OUTPUT_ENABLED
112-- 
1132.27.0
114
115