From 2059df5358d978c0b891fc75d77c7e771a7cfcdc Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 14 Nov 2022 22:27:58 +0100 Subject: [PATCH] buf: Deprecate static/immutable buffers --- buf.c | 56 +++--------------------------------------- encoding.c | 6 ++--- entities.c | 1 - gentest.py | 1 - include/libxml/tree.h | 2 +- include/libxml/xmlIO.h | 1 + include/private/buf.h | 1 - parser.c | 1 - testchar.c | 24 ++++++++++-------- tree.c | 53 +++++++-------------------------------- xmlIO.c | 36 +++------------------------ xmlreader.c | 10 +------- 12 files changed, 35 insertions(+), 157 deletions(-) diff --git a/buf.c b/buf.c index 9b2a7d19..7fb75c3b 100644 --- a/buf.c +++ b/buf.c @@ -200,8 +200,6 @@ xmlBufDetach(xmlBufPtr buf) { if (buf == NULL) return(NULL); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) - return(NULL); if (buf->buffer != NULL) return(NULL); if (buf->error) @@ -216,40 +214,6 @@ xmlBufDetach(xmlBufPtr buf) { return ret; } - -/** - * xmlBufCreateStatic: - * @mem: the memory area - * @size: the size in byte - * - * routine to create an XML buffer from an immutable memory area. - * The area won't be modified nor copied, and is expected to be - * present until the end of the buffer lifetime. - * - * returns the new structure. - */ -xmlBufPtr -xmlBufCreateStatic(void *mem, size_t size) { - xmlBufPtr ret; - - if (mem == NULL) - return(NULL); - - ret = (xmlBufPtr) xmlMalloc(sizeof(xmlBuf)); - if (ret == NULL) { - xmlBufMemoryError(NULL, "creating buffer"); - return(NULL); - } - ret->use = size; - ret->size = size; - UPDATE_COMPAT(ret); - ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; - ret->content = (xmlChar *) mem; - ret->error = 0; - ret->buffer = NULL; - return(ret); -} - /** * xmlBufGetAllocationScheme: * @buf: the buffer @@ -289,13 +253,11 @@ xmlBufSetAllocationScheme(xmlBufPtr buf, #endif return(-1); } - if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || - (buf->alloc == XML_BUFFER_ALLOC_IO)) + if (buf->alloc == XML_BUFFER_ALLOC_IO) return(-1); if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || (scheme == XML_BUFFER_ALLOC_EXACT) || (scheme == XML_BUFFER_ALLOC_HYBRID) || - (scheme == XML_BUFFER_ALLOC_IMMUTABLE) || (scheme == XML_BUFFER_ALLOC_BOUNDED)) { buf->alloc = scheme; if (buf->buffer) @@ -333,8 +295,7 @@ xmlBufFree(xmlBufPtr buf) { if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { xmlFree(buf->contentIO); - } else if ((buf->content != NULL) && - (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { + } else if (buf->content != NULL) { xmlFree(buf->content); } xmlFree(buf); @@ -352,9 +313,7 @@ xmlBufEmpty(xmlBufPtr buf) { if (buf->content == NULL) return; CHECK_COMPAT(buf) buf->use = 0; - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { - buf->content = BAD_CAST ""; - } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { size_t start_buf = buf->content - buf->contentIO; @@ -387,8 +346,7 @@ xmlBufShrink(xmlBufPtr buf, size_t len) { if (len > buf->use) return(0); buf->use -= len; - if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || - ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { /* * we just move the content pointer, but also make sure * the perceived buffer size has shrunk accordingly @@ -436,7 +394,6 @@ xmlBufGrowInternal(xmlBufPtr buf, size_t len) { if ((buf == NULL) || (buf->error != 0)) return(0); CHECK_COMPAT(buf) - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); if (len < buf->size - buf->use) return(buf->size - buf->use - 1); if (len >= SIZE_MAX - buf->use) { @@ -701,7 +658,6 @@ xmlBufResize(xmlBufPtr buf, size_t size) return(0); CHECK_COMPAT(buf) - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); if (buf->alloc == XML_BUFFER_ALLOC_BOUNDED) { /* * Used to provide parsing limits @@ -827,7 +783,6 @@ xmlBufAdd(xmlBufPtr buf, const xmlChar *str, int len) { return -1; CHECK_COMPAT(buf) - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; if (len < -1) { #ifdef DEBUG_BUFFER xmlGenericError(xmlGenericErrorContext, @@ -887,7 +842,6 @@ xmlBufCat(xmlBufPtr buf, const xmlChar *str) { if ((buf == NULL) || (buf->error)) return(-1); CHECK_COMPAT(buf) - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; if (str == NULL) return -1; return xmlBufAdd(buf, str, -1); } @@ -925,8 +879,6 @@ xmlBufWriteQuotedString(xmlBufPtr buf, const xmlChar *string) { if ((buf == NULL) || (buf->error)) return(-1); CHECK_COMPAT(buf) - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) - return(-1); if (xmlStrchr(string, '\"')) { if (xmlStrchr(string, '\'')) { #ifdef DEBUG_BUFFER diff --git a/encoding.c b/encoding.c index c990bcf1..35ac3835 100644 --- a/encoding.c +++ b/encoding.c @@ -2584,8 +2584,7 @@ retry: xmlEncodingErr(XML_I18N_CONV_FAILED, "output conversion failed due to conv error, bytes %s\n", buf); - if (xmlBufGetAllocationScheme(in) != XML_BUFFER_ALLOC_IMMUTABLE) - content[0] = ' '; + content[0] = ' '; break; } @@ -2745,8 +2744,7 @@ retry: xmlEncodingErr(XML_I18N_CONV_FAILED, "output conversion failed due to conv error, bytes %s\n", buf); - if (in->alloc != XML_BUFFER_ALLOC_IMMUTABLE) - in->content[0] = ' '; + in->content[0] = ' '; break; } diff --git a/entities.c b/entities.c index 063a02fa..600d53de 100644 --- a/entities.c +++ b/entities.c @@ -1011,7 +1011,6 @@ xmlCopyEntitiesTable(xmlEntitiesTablePtr table) { */ static void xmlDumpEntityContent(xmlBufferPtr buf, const xmlChar *content) { - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; if (xmlStrchr(content, '%')) { const xmlChar * base, *cur; diff --git a/gentest.py b/gentest.py index 172ed7e9..803a5d9a 100755 --- a/gentest.py +++ b/gentest.py @@ -261,7 +261,6 @@ extra_post_call = { "xmlParseChunk": "if (ctxt != NULL) {xmlFreeDoc(ctxt->myDoc); ctxt->myDoc = NULL;}", "xmlParseExtParsedEnt": "if (ctxt != NULL) {xmlFreeDoc(ctxt->myDoc); ctxt->myDoc = NULL;}", "xmlDOMWrapAdoptNode": "if ((node != NULL) && (node->parent == NULL)) {xmlUnlinkNode(node);xmlFreeNode(node);node = NULL;}", - "xmlBufferSetAllocationScheme": "if ((buf != NULL) && (scheme == XML_BUFFER_ALLOC_IMMUTABLE) && (buf->content != NULL) && (buf->content != static_buf_content)) { xmlFree(buf->content); buf->content = NULL;}" } modules = [] diff --git a/include/libxml/tree.h b/include/libxml/tree.h index 4147522c..fffcd8df 100644 --- a/include/libxml/tree.h +++ b/include/libxml/tree.h @@ -74,7 +74,7 @@ typedef xmlEntity *xmlEntityPtr; typedef enum { XML_BUFFER_ALLOC_DOUBLEIT, /* double each time one need to grow */ XML_BUFFER_ALLOC_EXACT, /* grow only to the minimal size */ - XML_BUFFER_ALLOC_IMMUTABLE, /* immutable buffer */ + XML_BUFFER_ALLOC_IMMUTABLE, /* immutable buffer, deprecated */ XML_BUFFER_ALLOC_IO, /* special allocation scheme used for I/O */ XML_BUFFER_ALLOC_HYBRID, /* exact up to a threshold, and doubleit thereafter */ XML_BUFFER_ALLOC_BOUNDED /* limit the upper size of the buffer */ diff --git a/include/libxml/xmlIO.h b/include/libxml/xmlIO.h index 095b2f56..7b41f2cc 100644 --- a/include/libxml/xmlIO.h +++ b/include/libxml/xmlIO.h @@ -178,6 +178,7 @@ XMLPUBFUN xmlParserInputBufferPtr XMLCALL XMLPUBFUN xmlParserInputBufferPtr XMLCALL xmlParserInputBufferCreateMem (const char *mem, int size, xmlCharEncoding enc); +XML_DEPRECATED XMLPUBFUN xmlParserInputBufferPtr XMLCALL xmlParserInputBufferCreateStatic (const char *mem, int size, xmlCharEncoding enc); diff --git a/include/private/buf.h b/include/private/buf.h index 12f725de..bff213df 100644 --- a/include/private/buf.h +++ b/include/private/buf.h @@ -5,7 +5,6 @@ xmlBufPtr xmlBufCreate(void); xmlBufPtr xmlBufCreateSize(size_t size); -xmlBufPtr xmlBufCreateStatic(void *mem, size_t size); int xmlBufSetAllocationScheme(xmlBufPtr buf, xmlBufferAllocationScheme scheme); diff --git a/parser.c b/parser.c index df79a7ca..3aeb8ad9 100644 --- a/parser.c +++ b/parser.c @@ -14377,7 +14377,6 @@ xmlCreateMemoryParserCtxt(const char *buffer, int size) { if (ctxt == NULL) return(NULL); - /* TODO: xmlParserInputBufferCreateStatic, requires some serious changes */ buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); if (buf == NULL) { xmlFreeParserCtxt(ctxt); diff --git a/testchar.c b/testchar.c index 2fc38697..4fd68c0d 100644 --- a/testchar.c +++ b/testchar.c @@ -259,9 +259,10 @@ static int testDocumentRanges(void) { return(test_ret); } -static int testCharRangeByte1(xmlParserCtxtPtr ctxt, char *data) { +static int testCharRangeByte1(xmlParserCtxtPtr ctxt) { int i = 0; int len, c; + char *data = (char *) ctxt->input->cur; data[1] = 0; data[2] = 0; @@ -292,9 +293,10 @@ static int testCharRangeByte1(xmlParserCtxtPtr ctxt, char *data) { return(0); } -static int testCharRangeByte2(xmlParserCtxtPtr ctxt, char *data) { +static int testCharRangeByte2(xmlParserCtxtPtr ctxt) { int i, j; int len, c; + char *data = (char *) ctxt->input->cur; data[2] = 0; data[3] = 0; @@ -379,10 +381,11 @@ static int testCharRangeByte2(xmlParserCtxtPtr ctxt, char *data) { return(0); } -static int testCharRangeByte3(xmlParserCtxtPtr ctxt, char *data) { +static int testCharRangeByte3(xmlParserCtxtPtr ctxt) { int i, j, k, K; int len, c; unsigned char lows[6] = {0, 0x80, 0x81, 0xC1, 0xFF, 0xBF}; + char *data = (char *) ctxt->input->cur; int value; data[3] = 0; @@ -476,10 +479,11 @@ static int testCharRangeByte3(xmlParserCtxtPtr ctxt, char *data) { return(0); } -static int testCharRangeByte4(xmlParserCtxtPtr ctxt, char *data) { +static int testCharRangeByte4(xmlParserCtxtPtr ctxt) { int i, j, k, K, l, L; int len, c; unsigned char lows[6] = {0, 0x80, 0x81, 0xC1, 0xFF, 0xBF}; + char *data = (char *) ctxt->input->cur; int value; data[4] = 0; @@ -606,8 +610,8 @@ static int testCharRanges(void) { fprintf(stderr, "Failed to allocate parser context\n"); return(1); } - buf = xmlParserInputBufferCreateStatic(data, sizeof(data), - XML_CHAR_ENCODING_NONE); + buf = xmlParserInputBufferCreateMem(data, sizeof(data), + XML_CHAR_ENCODING_NONE); if (buf == NULL) { fprintf(stderr, "Failed to allocate input buffer\n"); test_ret = 1; @@ -628,16 +632,16 @@ static int testCharRanges(void) { printf("testing char range: 1"); fflush(stdout); - test_ret += testCharRangeByte1(ctxt, data); + test_ret += testCharRangeByte1(ctxt); printf(" 2"); fflush(stdout); - test_ret += testCharRangeByte2(ctxt, data); + test_ret += testCharRangeByte2(ctxt); printf(" 3"); fflush(stdout); - test_ret += testCharRangeByte3(ctxt, data); + test_ret += testCharRangeByte3(ctxt); printf(" 4"); fflush(stdout); - test_ret += testCharRangeByte4(ctxt, data); + test_ret += testCharRangeByte4(ctxt); printf(" done\n"); fflush(stdout); diff --git a/tree.c b/tree.c index 502f51c0..a7368a32 100644 --- a/tree.c +++ b/tree.c @@ -7207,8 +7207,6 @@ xmlBufferDetach(xmlBufferPtr buf) { if (buf == NULL) return(NULL); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) - return(NULL); ret = buf->content; buf->content = NULL; @@ -7224,31 +7222,14 @@ xmlBufferDetach(xmlBufferPtr buf) { * @mem: the memory area * @size: the size in byte * - * routine to create an XML buffer from an immutable memory area. - * The area won't be modified nor copied, and is expected to be - * present until the end of the buffer lifetime. - * - * returns the new structure. + * Create an XML buffer initialized with bytes. */ xmlBufferPtr xmlBufferCreateStatic(void *mem, size_t size) { - xmlBufferPtr ret; + xmlBufferPtr buf = xmlBufferCreateSize(size); - if ((mem == NULL) || (size == 0)) - return(NULL); - if (size > UINT_MAX) - return(NULL); - - ret = (xmlBufferPtr) xmlMalloc(sizeof(xmlBuffer)); - if (ret == NULL) { - xmlTreeErrMemory("creating buffer"); - return(NULL); - } - ret->use = size; - ret->size = size; - ret->alloc = XML_BUFFER_ALLOC_IMMUTABLE; - ret->content = (xmlChar *) mem; - return(ret); + xmlBufferAdd(buf, mem, size); + return(buf); } /** @@ -7268,12 +7249,10 @@ xmlBufferSetAllocationScheme(xmlBufferPtr buf, #endif return; } - if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || - (buf->alloc == XML_BUFFER_ALLOC_IO)) return; + if (buf->alloc == XML_BUFFER_ALLOC_IO) return; if ((scheme == XML_BUFFER_ALLOC_DOUBLEIT) || (scheme == XML_BUFFER_ALLOC_EXACT) || - (scheme == XML_BUFFER_ALLOC_HYBRID) || - (scheme == XML_BUFFER_ALLOC_IMMUTABLE)) + (scheme == XML_BUFFER_ALLOC_HYBRID)) buf->alloc = scheme; } @@ -7297,8 +7276,7 @@ xmlBufferFree(xmlBufferPtr buf) { if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { xmlFree(buf->contentIO); - } else if ((buf->content != NULL) && - (buf->alloc != XML_BUFFER_ALLOC_IMMUTABLE)) { + } else if (buf->content != NULL) { xmlFree(buf->content); } xmlFree(buf); @@ -7315,10 +7293,7 @@ xmlBufferEmpty(xmlBufferPtr buf) { if (buf == NULL) return; if (buf->content == NULL) return; buf->use = 0; - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) { - buf->content = BAD_CAST ""; - } else if ((buf->alloc == XML_BUFFER_ALLOC_IO) && - (buf->contentIO != NULL)) { + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { size_t start_buf = buf->content - buf->contentIO; buf->size += start_buf; @@ -7345,8 +7320,7 @@ xmlBufferShrink(xmlBufferPtr buf, unsigned int len) { if (len > buf->use) return(-1); buf->use -= len; - if ((buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) || - ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL))) { + if ((buf->alloc == XML_BUFFER_ALLOC_IO) && (buf->contentIO != NULL)) { /* * we just move the content pointer, but also make sure * the perceived buffer size has shrunk accordingly @@ -7390,7 +7364,6 @@ xmlBufferGrow(xmlBufferPtr buf, unsigned int len) { if (buf == NULL) return(-1); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); if (len < buf->size - buf->use) return(0); if (len >= UINT_MAX - buf->use) { @@ -7514,8 +7487,6 @@ xmlBufferResize(xmlBufferPtr buf, unsigned int size) if (buf == NULL) return(0); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return(0); - /* Don't resize if we don't have to */ if (size < buf->size) return 1; @@ -7633,7 +7604,6 @@ xmlBufferAdd(xmlBufferPtr buf, const xmlChar *str, int len) { if ((str == NULL) || (buf == NULL)) { return -1; } - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; if (len < -1) { #ifdef DEBUG_BUFFER xmlGenericError(xmlGenericErrorContext, @@ -7686,7 +7656,6 @@ xmlBufferAddHead(xmlBufferPtr buf, const xmlChar *str, int len) { if (buf == NULL) return(-1); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; if (str == NULL) { #ifdef DEBUG_BUFFER xmlGenericError(xmlGenericErrorContext, @@ -7757,7 +7726,6 @@ int xmlBufferCat(xmlBufferPtr buf, const xmlChar *str) { if (buf == NULL) return(-1); - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return -1; if (str == NULL) return -1; return xmlBufferAdd(buf, str, -1); } @@ -7789,7 +7757,6 @@ void xmlBufferWriteCHAR(xmlBufferPtr buf, const xmlChar *string) { if (buf == NULL) return; - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; xmlBufferCat(buf, string); } @@ -7805,7 +7772,6 @@ void xmlBufferWriteChar(xmlBufferPtr buf, const char *string) { if (buf == NULL) return; - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; xmlBufferCCat(buf, string); } @@ -7824,7 +7790,6 @@ xmlBufferWriteQuotedString(xmlBufferPtr buf, const xmlChar *string) { const xmlChar *cur, *base; if (buf == NULL) return; - if (buf->alloc == XML_BUFFER_ALLOC_IMMUTABLE) return; if (xmlStrchr(string, '\"')) { if (xmlStrchr(string, '\'')) { #ifdef DEBUG_BUFFER diff --git a/xmlIO.c b/xmlIO.c index ee53d000..05800185 100644 --- a/xmlIO.c +++ b/xmlIO.c @@ -2950,43 +2950,14 @@ xmlParserInputBufferCreateMem(const char *mem, int size, xmlCharEncoding enc) { * @size: the length of the memory block * @enc: the charset encoding if known * - * Create a buffered parser input for the progressive parsing for the input - * from an immutable memory area. This will not copy the memory area to - * the buffer, but the memory is expected to be available until the end of - * the parsing, this is useful for example when using mmap'ed file. + * DEPRECATED: Use xmlParserInputBufferCreateMem. * * Returns the new parser input or NULL */ xmlParserInputBufferPtr xmlParserInputBufferCreateStatic(const char *mem, int size, xmlCharEncoding enc) { - xmlParserInputBufferPtr ret; - - if (size < 0) return(NULL); - if (mem == NULL) return(NULL); - - ret = (xmlParserInputBufferPtr) xmlMalloc(sizeof(xmlParserInputBuffer)); - if (ret == NULL) { - xmlIOErrMemory("creating input buffer"); - return(NULL); - } - memset(ret, 0, sizeof(xmlParserInputBuffer)); - ret->buffer = xmlBufCreateStatic((void *)mem, size); - if (ret->buffer == NULL) { - xmlFree(ret); - return(NULL); - } - ret->encoder = xmlGetCharEncodingHandler(enc); - if (ret->encoder != NULL) - ret->raw = xmlBufCreateSize(2 * xmlDefaultBufferSize); - else - ret->raw = NULL; - ret->compressed = -1; - ret->context = (void *) mem; - ret->readcallback = NULL; - ret->closecallback = NULL; - - return(ret); + return(xmlParserInputBufferCreateMem(mem, size, enc)); } #ifdef LIBXML_OUTPUT_ENABLED @@ -3504,8 +3475,7 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str, int cons; /* byte from str consumed */ if ((out == NULL) || (out->error) || (str == NULL) || - (out->buffer == NULL) || - (xmlBufGetAllocationScheme(out->buffer) == XML_BUFFER_ALLOC_IMMUTABLE)) + (out->buffer == NULL)) return(-1); len = strlen((const char *)str); if (len < 0) return(0); diff --git a/xmlreader.c b/xmlreader.c index 9f05a2c2..6ba578c6 100644 --- a/xmlreader.c +++ b/xmlreader.c @@ -773,7 +773,6 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) { xmlBufPtr inbuf; int val, s; xmlTextReaderState oldstate; - int alloc; if ((reader->input == NULL) || (reader->input->buffer == NULL)) return(-1); @@ -781,7 +780,6 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) { oldstate = reader->state; reader->state = XML_TEXTREADER_NONE; inbuf = reader->input->buffer; - alloc = xmlBufGetAllocationScheme(inbuf); while (reader->state == XML_TEXTREADER_NONE) { if (xmlBufUse(inbuf) < reader->cur + CHUNK_SIZE) { @@ -790,13 +788,7 @@ xmlTextReaderPushData(xmlTextReaderPtr reader) { */ if (reader->mode != XML_TEXTREADER_MODE_EOF) { val = xmlParserInputBufferRead(reader->input, 4096); - if ((val == 0) && - (alloc == XML_BUFFER_ALLOC_IMMUTABLE)) { - if (xmlBufUse(inbuf) == reader->cur) { - reader->mode = XML_TEXTREADER_MODE_EOF; - reader->state = oldstate; - } - } else if (val < 0) { + if (val < 0) { reader->mode = XML_TEXTREADER_MODE_EOF; reader->state = oldstate; if ((oldstate != XML_TEXTREADER_START) ||