Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit fee54bd

Browse files
committed
reader: Fix reading compressed data
Also make sure that functions that don't return error codes set the global error.
1 parent ae29937 commit fee54bd

2 files changed

Lines changed: 139 additions & 62 deletions

File tree

xmllint.c

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,51 +1712,35 @@ static void streamFile(xmllintState *lint, const char *filename) {
17121712
if (lint->memory) {
17131713
reader = xmlReaderForMemory(lint->memoryData, lint->memorySize,
17141714
filename, NULL, lint->options);
1715+
if (reader == NULL) {
1716+
lint->progresult = XMLLINT_ERR_MEM;
1717+
return;
1718+
}
17151719
} else
17161720
#endif
17171721
{
1722+
xmlResetLastError();
1723+
17181724
if (strcmp(filename, "-") == 0) {
1719-
reader = xmlReaderForFd(STDIN_FILENO, "-", NULL, lint->options);
1725+
reader = xmlReaderForFd(STDIN_FILENO, "-", NULL,
1726+
lint->options | XML_PARSE_UNZIP);
17201727
}
17211728
else {
1722-
/*
1723-
* There's still no easy way to get a reader for a file with
1724-
* adequate error repoting.
1725-
*/
1726-
1727-
xmlResetLastError();
1728-
input = xmlParserInputBufferCreateFilename(filename,
1729-
XML_CHAR_ENCODING_NONE);
1730-
if (input == NULL) {
1731-
const xmlError *error = xmlGetLastError();
1732-
1733-
if ((error != NULL) && (error->code == XML_ERR_NO_MEMORY)) {
1734-
lint->progresult = XMLLINT_ERR_MEM;
1735-
} else {
1736-
fprintf(errStream, "Unable to open %s\n", filename);
1737-
lint->progresult = XMLLINT_ERR_RDFILE;
1738-
}
1739-
return;
1740-
}
1729+
reader = xmlReaderForFile(filename, NULL,
1730+
lint->options | XML_PARSE_UNZIP);
1731+
}
1732+
if (reader == NULL) {
1733+
const xmlError *error = xmlGetLastError();
17411734

1742-
reader = xmlNewTextReader(input, filename);
1743-
if (reader == NULL) {
1735+
if ((error != NULL) && (error->code == XML_ERR_NO_MEMORY)) {
17441736
lint->progresult = XMLLINT_ERR_MEM;
1745-
xmlFreeParserInputBuffer(input);
1746-
return;
1747-
}
1748-
if (xmlTextReaderSetup(reader, NULL, NULL, NULL,
1749-
lint->options) < 0) {
1750-
lint->progresult = XMLLINT_ERR_MEM;
1751-
xmlFreeParserInputBuffer(input);
1752-
return;
1737+
} else {
1738+
fprintf(errStream, "Unable to open %s\n", filename);
1739+
lint->progresult = XMLLINT_ERR_RDFILE;
17531740
}
1741+
return;
17541742
}
17551743
}
1756-
if (reader == NULL) {
1757-
lint->progresult = XMLLINT_ERR_MEM;
1758-
return;
1759-
}
17601744

17611745
#ifdef LIBXML_PATTERN_ENABLED
17621746
if (lint->patternc != NULL) {

xmlreader.c

Lines changed: 121 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include "private/buf.h"
4545
#include "private/error.h"
46+
#include "private/io.h"
4647
#include "private/memory.h"
4748
#include "private/parser.h"
4849
#include "private/tree.h"
@@ -188,6 +189,21 @@ static int xmlTextReaderNextTree(xmlTextReaderPtr reader);
188189
static void xmlTextReaderFreeNode(xmlTextReaderPtr reader, xmlNodePtr cur);
189190
static void xmlTextReaderFreeNodeList(xmlTextReaderPtr reader, xmlNodePtr cur);
190191

192+
static void
193+
xmlTextReaderErr(xmlParserErrors code, const char *msg, ...) {
194+
va_list ap;
195+
int res;
196+
197+
va_start(ap, msg);
198+
res = xmlVRaiseError(NULL, NULL, NULL, NULL, NULL,
199+
XML_FROM_PARSER, code, XML_ERR_FATAL,
200+
NULL, 0, NULL, NULL, NULL, 0, 0,
201+
msg, ap);
202+
va_end(ap);
203+
if (res < 0)
204+
xmlRaiseMemoryError(NULL, NULL, NULL, XML_FROM_PARSER, NULL);
205+
}
206+
191207
static void
192208
xmlTextReaderErrMemory(xmlTextReaderPtr reader) {
193209
if (reader->ctxt != NULL)
@@ -2114,11 +2130,30 @@ xmlNewTextReaderFilename(const char *URI) {
21142130
xmlParserInputBufferPtr input;
21152131
xmlTextReaderPtr ret;
21162132

2117-
input = xmlParserInputBufferCreateFilename(URI, XML_CHAR_ENCODING_NONE);
2118-
if (input == NULL)
2119-
return(NULL);
2133+
if (xmlParserInputBufferCreateFilenameValue != NULL) {
2134+
input = xmlParserInputBufferCreateFilenameValue(URI,
2135+
XML_CHAR_ENCODING_NONE);
2136+
if (input == NULL) {
2137+
xmlTextReaderErr(XML_IO_ENOENT, "filaed to open %s", URI);
2138+
return(NULL);
2139+
}
2140+
} else {
2141+
xmlParserErrors code;
2142+
2143+
/*
2144+
* TODO: Remove XML_INPUT_UNZIP
2145+
*/
2146+
code = xmlParserInputBufferCreateUrl(URI, XML_CHAR_ENCODING_NONE,
2147+
XML_INPUT_UNZIP, &input);
2148+
if (code != XML_ERR_OK) {
2149+
xmlTextReaderErr(code, "failed to open %s", URI);
2150+
return(NULL);
2151+
}
2152+
}
2153+
21202154
ret = xmlNewTextReader(input, URI);
21212155
if (ret == NULL) {
2156+
xmlTextReaderErrMemory(NULL);
21222157
xmlFreeParserInputBuffer(input);
21232158
return(NULL);
21242159
}
@@ -5226,23 +5261,39 @@ xmlReaderForFd(int fd, const char *URL, const char *encoding, int options)
52265261
{
52275262
xmlTextReaderPtr reader;
52285263
xmlParserInputBufferPtr input;
5264+
xmlParserErrors code;
52295265

5230-
if (fd < 0)
5231-
return (NULL);
5266+
if (fd < 0) {
5267+
xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5268+
return(NULL);
5269+
}
52325270

5233-
input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5234-
if (input == NULL)
5235-
return (NULL);
5271+
input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
5272+
if (input == NULL) {
5273+
xmlTextReaderErrMemory(NULL);
5274+
return(NULL);
5275+
}
5276+
/*
5277+
* TODO: Remove XML_INPUT_UNZIP
5278+
*/
5279+
code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP);
5280+
if (code != XML_ERR_OK) {
5281+
xmlTextReaderErr(code, "failed to open fd");
5282+
return(NULL);
5283+
}
52365284
input->closecallback = NULL;
5285+
52375286
reader = xmlNewTextReader(input, URL);
52385287
if (reader == NULL) {
5288+
xmlTextReaderErrMemory(NULL);
52395289
xmlFreeParserInputBuffer(input);
5240-
return (NULL);
5290+
return(NULL);
52415291
}
52425292
reader->allocs |= XML_TEXTREADER_INPUT;
52435293
if (xmlTextReaderSetup(reader, NULL, URL, encoding, options) < 0) {
5294+
xmlTextReaderErrMemory(NULL);
52445295
xmlFreeTextReader(reader);
5245-
return (NULL);
5296+
return(NULL);
52465297
}
52475298
return (reader);
52485299
}
@@ -5386,17 +5437,42 @@ xmlReaderNewFile(xmlTextReaderPtr reader, const char *filename,
53865437
{
53875438
xmlParserInputBufferPtr input;
53885439

5389-
if (filename == NULL)
5390-
return (-1);
5391-
if (reader == NULL)
5392-
return (-1);
5440+
if ((filename == NULL) || (reader == NULL)) {
5441+
xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5442+
return(-1);
5443+
}
53935444

5394-
input =
5395-
xmlParserInputBufferCreateFilename(filename,
5396-
XML_CHAR_ENCODING_NONE);
5397-
if (input == NULL)
5398-
return (-1);
5399-
return (xmlTextReaderSetup(reader, input, filename, encoding, options));
5445+
if (xmlParserInputBufferCreateFilenameValue != NULL) {
5446+
input = xmlParserInputBufferCreateFilenameValue(filename,
5447+
XML_CHAR_ENCODING_NONE);
5448+
if (input == NULL) {
5449+
xmlTextReaderErr(XML_IO_ENOENT, "failed to open %s", filename);
5450+
return(-1);
5451+
}
5452+
} else {
5453+
/*
5454+
* TODO: Remove XML_INPUT_UNZIP
5455+
*/
5456+
xmlParserInputFlags flags = XML_INPUT_UNZIP;
5457+
xmlParserErrors code;
5458+
5459+
if ((options & XML_PARSE_NONET) == 0)
5460+
flags |= XML_INPUT_NETWORK;
5461+
5462+
code = xmlParserInputBufferCreateUrl(filename, XML_CHAR_ENCODING_NONE,
5463+
flags, &input);
5464+
if (code != XML_ERR_OK) {
5465+
xmlTextReaderErr(code, "failed to open %s", filename);
5466+
return(-1);
5467+
}
5468+
}
5469+
5470+
if (xmlTextReaderSetup(reader, input, filename, encoding, options) < 0) {
5471+
xmlTextReaderErrMemory(NULL);
5472+
return(-1);
5473+
}
5474+
5475+
return(0);
54005476
}
54015477

54025478
/**
@@ -5454,17 +5530,34 @@ xmlReaderNewFd(xmlTextReaderPtr reader, int fd,
54545530
const char *URL, const char *encoding, int options)
54555531
{
54565532
xmlParserInputBufferPtr input;
5533+
xmlParserErrors code;
54575534

5458-
if (fd < 0)
5459-
return (-1);
5460-
if (reader == NULL)
5461-
return (-1);
5535+
if ((fd < 0) || (reader == NULL)) {
5536+
xmlTextReaderErr(XML_ERR_ARGUMENT, "invalid argument");
5537+
return(-1);
5538+
}
54625539

5463-
input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
5464-
if (input == NULL)
5465-
return (-1);
5540+
input = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
5541+
if (input == NULL) {
5542+
xmlTextReaderErrMemory(NULL);
5543+
return(-1);
5544+
}
5545+
/*
5546+
* TODO: Remove XML_INPUT_UNZIP
5547+
*/
5548+
code = xmlInputFromFd(input, fd, XML_INPUT_UNZIP);
5549+
if (code != XML_ERR_OK) {
5550+
xmlTextReaderErr(code, "failed to open fd");
5551+
return(-1);
5552+
}
54665553
input->closecallback = NULL;
5467-
return (xmlTextReaderSetup(reader, input, URL, encoding, options));
5554+
5555+
if (xmlTextReaderSetup(reader, input, URL, encoding, options) < 0) {
5556+
xmlTextReaderErrMemory(NULL);
5557+
return(-1);
5558+
}
5559+
5560+
return(0);
54685561
}
54695562

54705563
/**

0 commit comments

Comments
 (0)