1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +03:00

io: Don't use "-" to read from stdin

To implement this feature on such a low level is a disaster waiting to
happen. Remove these checks from the IO code and move them to xmllint.

Note that the serialization API will still treat "-" as stdout.
This commit is contained in:
Nick Wellnhofer 2023-12-20 23:53:19 +01:00
parent c9a46a91fe
commit 0a658c0f0a
3 changed files with 46 additions and 51 deletions

View File

@ -429,10 +429,6 @@ xmlXIncludeParseFile(xmlXIncludeCtxtPtr ctxt, const char *URL) {
xmlCtxtUseOptions(pctxt, ctxt->parseFlags | XML_PARSE_DTDLOAD);
/* Don't read from stdin. */
if ((URL != NULL) && (strcmp(URL, "-") == 0))
URL = "./-";
inputStream = xmlLoadExternalEntity(URL, NULL, pctxt);
if (inputStream == NULL)
goto error;
@ -1663,11 +1659,6 @@ xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url,
int res;
const xmlChar *content;
/* Don't read from stdin. */
if (xmlStrcmp(url, BAD_CAST "-") == 0)
url = BAD_CAST "./-";
/*
* Check the URL and remove any fragment identifier
*/

35
xmlIO.c
View File

@ -58,10 +58,6 @@
#define MINLEN 4000
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef S_ISDIR
# ifdef _S_ISDIR
# define S_ISDIR(x) _S_ISDIR(x)
@ -576,11 +572,6 @@ xmlFdOpen(const char *filename, void **out) {
if (filename == NULL)
return(XML_ERR_ARGUMENT);
if (!strcmp(filename, "-")) {
*out = (void *) (ptrdiff_t) STDIN_FILENO;
return(XML_ERR_OK);
}
/*
* TODO: This should be moved to uri.c. We also need support for
* UNC paths on Windows.
@ -730,7 +721,6 @@ xmlFileMatch (const char *filename ATTRIBUTE_UNUSED) {
* @out: pointer to resulting context
*
* input from FILE *, supports compressed input
* if @filename is "-" then the standard input is used
*
* Returns an I/O context or NULL in case of error
*/
@ -744,11 +734,6 @@ xmlFileOpenReal(const char *filename, void **out) {
if (filename == NULL)
return(XML_ERR_ARGUMENT);
if (!strcmp(filename, "-")) {
*out = stdin;
return(XML_ERR_OK);
}
if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) {
#if defined (_WIN32)
path = &filename[17];
@ -1027,7 +1012,7 @@ xmlBufferWrite (void * context, const char * buffer, int len) {
*/
static int
xmlGzfileMatch (const char *filename ATTRIBUTE_UNUSED) {
return(strcmp(filename, "-") != 0);
return(1);
}
/**
@ -1044,16 +1029,6 @@ xmlGzfileOpen_real (const char *filename) {
const char *path = NULL;
gzFile fd;
if (!strcmp(filename, "-")) {
int duped_fd = dup(fileno(stdin));
fd = gzdopen(duped_fd, "rb");
if (fd == Z_NULL && duped_fd >= 0) {
close(duped_fd); /* gzdOpen() does not close on failure */
}
return((void *) fd);
}
if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17))
#if defined (_WIN32)
path = &filename[17];
@ -1233,7 +1208,7 @@ xmlGzfileClose (void * context) {
*/
static int
xmlXzfileMatch (const char *filename ATTRIBUTE_UNUSED) {
return(strcmp(filename, "-") != 0);
return(1);
}
/**
@ -1250,11 +1225,6 @@ xmlXzfileOpen_real (const char *filename) {
const char *path = NULL;
xzFile fd;
if (!strcmp(filename, "-")) {
fd = __libxml2_xzdopen(dup(fileno(stdin)), "rb");
return((void *) fd);
}
if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file://localhost/", 17)) {
path = &filename[16];
} else if (!xmlStrncasecmp(BAD_CAST filename, BAD_CAST "file:///", 8)) {
@ -2485,7 +2455,6 @@ __xmlParserInputBufferCreateFilename(const char *URI, xmlCharEncoding enc) {
* @enc: the charset encoding if known
*
* Create a buffered parser input for the progressive parsing of a file
* If filename is "-" then we use stdin as the input.
* Automatic support for ZLIB/Compress compressed document is provided
* by default if found at compile-time.
* Do an encoding check if enc == XML_CHAR_ENCODING_NONE

View File

@ -85,6 +85,10 @@
#define XML_XML_DEFAULT_CATALOG "file://" SYSCONFDIR "/xml/catalog"
#endif
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
typedef enum {
XMLLINT_RETURN_OK = 0, /* No error */
XMLLINT_ERR_UNCLASS = 1, /* Unclassified */
@ -1604,8 +1608,12 @@ testSAX(const char *filename) {
xmlSchemaValidCtxtPtr vctxt;
xmlParserInputBufferPtr buf;
buf = xmlParserInputBufferCreateFilename(filename,
XML_CHAR_ENCODING_NONE);
if (strcmp(filename, "-") == 0)
buf = xmlParserInputBufferCreateFd(STDIN_FILENO,
XML_CHAR_ENCODING_NONE);
else
buf = xmlParserInputBufferCreateFilename(filename,
XML_CHAR_ENCODING_NONE);
if (buf == NULL)
return;
@ -1649,7 +1657,11 @@ testSAX(const char *filename) {
}
if (maxAmpl > 0)
xmlCtxtSetMaxAmplification(ctxt, maxAmpl);
xmlCtxtReadFile(ctxt, filename, NULL, options);
if (strcmp(filename, "-") == 0)
xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, options);
else
xmlCtxtReadFile(ctxt, filename, NULL, options);
if (ctxt->myDoc != NULL) {
fprintf(stderr, "SAX generated a doc !\n");
@ -1783,6 +1795,9 @@ static void streamFile(char *filename) {
NULL, options);
} else
#endif
if (strcmp(filename, "-") == 0)
reader = xmlReaderForFd(STDIN_FILENO, "-", NULL, options);
else
reader = xmlReaderForFile(filename, NULL, options);
#ifdef LIBXML_PATTERN_ENABLED
if (patternc != NULL) {
@ -2189,7 +2204,10 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
}
#endif
else if (html) {
doc = htmlReadFile(filename, NULL, options);
if (strcmp(filename, "-") == 0)
doc = htmlReadFd(STDIN_FILENO, "-", NULL, options);
else
doc = htmlReadFile(filename, NULL, options);
}
#endif /* LIBXML_HTML_ENABLED */
else {
@ -2245,7 +2263,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
#endif /* LIBXML_PUSH_ENABLED */
if (testIO) {
if ((filename[0] == '-') && (filename[1] == 0)) {
doc = xmlReadFd(0, NULL, NULL, options);
doc = xmlReadFd(STDIN_FILENO, "-", NULL, options);
} else {
FILE *f;
@ -2280,7 +2298,10 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
ctxt->vctxt.error = xmlHTMLValidityError;
ctxt->vctxt.warning = xmlHTMLValidityWarning;
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (strcmp(filename, "-") == 0)
doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, options);
else
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (rectxt == NULL)
xmlFreeParserCtxt(ctxt);
@ -2339,7 +2360,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
if (maxAmpl > 0)
xmlCtxtSetMaxAmplification(ctxt, maxAmpl);
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (strcmp(filename, "-") == 0)
doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL, options);
else
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (ctxt->valid == 0)
progresult = XMLLINT_ERR_RDFILE;
@ -2348,7 +2373,11 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
#endif /* LIBXML_VALID_ENABLED */
} else {
if (rectxt != NULL) {
doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
if (strcmp(filename, "-") == 0)
doc = xmlCtxtReadFd(rectxt, STDIN_FILENO, "-", NULL,
options);
else
doc = xmlCtxtReadFile(rectxt, filename, NULL, options);
} else {
xmlParserCtxtPtr ctxt;
@ -2360,7 +2389,13 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) {
}
if (maxAmpl > 0)
xmlCtxtSetMaxAmplification(ctxt, maxAmpl);
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
if (strcmp(filename, "-") == 0)
doc = xmlCtxtReadFd(ctxt, STDIN_FILENO, "-", NULL,
options);
else
doc = xmlCtxtReadFile(ctxt, filename, NULL, options);
xmlFreeParserCtxt(ctxt);
}
}