From 32cac377c8eada57d6f2b8bc7fa807f2de61bed7 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 17 Jun 2024 17:59:49 +0200 Subject: [PATCH] parser: Selectively reenable reading from "-" Make filename "-" mean stdin for legacy SAX1 functions and xmlReadFile. This should hopefully fix most command line utilities. See #737. --- parser.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/parser.c b/parser.c index 956945d8..73d80732 100644 --- a/parser.c +++ b/parser.c @@ -79,6 +79,10 @@ #define URI_HASH_EMPTY 0xD943A04E #define URI_HASH_XML 0xF0451F02 +#ifndef STDIN_FILENO + #define STDIN_FILENO 0 +#endif + struct _xmlStartTag { const xmlChar *prefix; const xmlChar *URI; @@ -12744,7 +12748,10 @@ xmlSAXParseFileWithData(xmlSAXHandlerPtr sax, const char *filename, ctxt->recovery = 1; } - input = xmlNewInputURL(ctxt, filename, NULL, NULL, 0); + if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0)) + input = xmlNewInputFd(ctxt, filename, STDIN_FILENO, NULL, 0); + else + input = xmlNewInputURL(ctxt, filename, NULL, NULL, 0); ret = xmlCtxtParseDocument(ctxt, input); @@ -13777,7 +13784,15 @@ xmlReadFile(const char *filename, const char *encoding, int options) xmlCtxtUseOptions(ctxt, options); - input = xmlNewInputURL(ctxt, filename, NULL, encoding, 0); + /* + * Backward compatibility for users of command line utilities like + * xmlstarlet expecting "-" to mean stdin. This is dangerous and + * should be removed at some point. + */ + if ((filename != NULL) && (filename[0] == '-') && (filename[1] == 0)) + input = xmlNewInputFd(ctxt, filename, STDIN_FILENO, encoding, 0); + else + input = xmlNewInputURL(ctxt, filename, NULL, encoding, 0); doc = xmlCtxtParseDocument(ctxt, input);