From ef6e6012e291c0047b8c1300132761945f3e7574 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Mon, 6 May 2024 01:13:49 +0200 Subject: [PATCH] xmllint: Fix parsing of maxmem option Fix corner cases like 'xmllint --encode --maxmem 123'. Also fixes --path without LIBXML_VALID_ENABLED. --- xmllint.c | 203 +++++++++++++++++++++++------------------------------- 1 file changed, 87 insertions(+), 116 deletions(-) diff --git a/xmllint.c b/xmllint.c index 317f2429..63b750af 100644 --- a/xmllint.c +++ b/xmllint.c @@ -3149,6 +3149,48 @@ parseInteger(const char *ctxt, const char *str, return(val); } +static int +skipArgs(const char *arg) { + if ((!strcmp(arg, "-encode")) || + (!strcmp(arg, "--encode")) || + (!strcmp(arg, "-o")) || + (!strcmp(arg, "-output")) || + (!strcmp(arg, "--output")) || + (!strcmp(arg, "-path")) || + (!strcmp(arg, "--path")) || +#ifdef LIBXML_VALID_ENABLED + (!strcmp(arg, "-dtdvalid")) || + (!strcmp(arg, "--dtdvalid")) || + (!strcmp(arg, "-dtdvalidfpi")) || + (!strcmp(arg, "--dtdvalidfpi")) || +#endif /* LIBXML_VALID_ENABLED */ + (!strcmp(arg, "-relaxng")) || + (!strcmp(arg, "--relaxng")) || + (!strcmp(arg, "-maxmem")) || + (!strcmp(arg, "--maxmem")) || + (!strcmp(arg, "-pretty")) || + (!strcmp(arg, "--pretty")) || + (!strcmp(arg, "-schema")) || + (!strcmp(arg, "--schema")) || + (!strcmp(arg, "-schematron")) || + (!strcmp(arg, "--schematron")) || +#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_PATTERN_ENABLED) + (!strcmp(arg, "-pattern")) || + (!strcmp(arg, "--pattern")) || +#endif +#ifdef LIBXML_XPATH_ENABLED + (!strcmp(arg, "-xpath")) || + (!strcmp(arg, "--xpath")) || +#endif + (!strcmp(arg, "-max-ampl")) || + (!strcmp(arg, "--max-ampl")) + ) { + return(1); + } + + return(0); +} + int main(int argc, char **argv) { int i, acount; @@ -3162,9 +3204,6 @@ main(int argc, char **argv) { /* xmlMemSetup must be called before initializing the parser. */ for (i = 1; i < argc ; i++) { - if (argv[i][0] != '-') - continue; - if ((!strcmp(argv[i], "-maxmem")) || (!strcmp(argv[i], "--maxmem"))) { i++; @@ -3174,7 +3213,9 @@ main(int argc, char **argv) { } errno = 0; maxmem = parseInteger("maxmem", argv[i], 0, INT_MAX); - } + } else if (argv[i][0] == '-') { + i += skipArgs(argv[i]); + } } if (maxmem != 0) xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc, myStrdupFunc); @@ -3662,126 +3703,56 @@ main(int argc, char **argv) { } #endif /* LIBXML_READER_ENABLED && LIBXML_PATTERN_ENABLED */ for (i = 1; i < argc ; i++) { - if ((!strcmp(argv[i], "-encode")) || - (!strcmp(argv[i], "--encode"))) { - i++; - continue; - } else if ((!strcmp(argv[i], "-o")) || - (!strcmp(argv[i], "-output")) || - (!strcmp(argv[i], "--output"))) { - i++; - continue; - } -#ifdef LIBXML_VALID_ENABLED - if ((!strcmp(argv[i], "-dtdvalid")) || - (!strcmp(argv[i], "--dtdvalid"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-path")) || - (!strcmp(argv[i], "--path"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-dtdvalidfpi")) || - (!strcmp(argv[i], "--dtdvalidfpi"))) { - i++; - continue; - } -#endif /* LIBXML_VALID_ENABLED */ - if ((!strcmp(argv[i], "-relaxng")) || - (!strcmp(argv[i], "--relaxng"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-maxmem")) || - (!strcmp(argv[i], "--maxmem"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-pretty")) || - (!strcmp(argv[i], "--pretty"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-schema")) || - (!strcmp(argv[i], "--schema"))) { - i++; - continue; - } - if ((!strcmp(argv[i], "-schematron")) || - (!strcmp(argv[i], "--schematron"))) { - i++; - continue; - } -#if defined(LIBXML_READER_ENABLED) && defined(LIBXML_PATTERN_ENABLED) - if ((!strcmp(argv[i], "-pattern")) || - (!strcmp(argv[i], "--pattern"))) { - i++; - continue; - } -#endif -#ifdef LIBXML_XPATH_ENABLED - if ((!strcmp(argv[i], "-xpath")) || - (!strcmp(argv[i], "--xpath"))) { - i++; - continue; - } -#endif - if ((!strcmp(argv[i], "-max-ampl")) || - (!strcmp(argv[i], "--max-ampl"))) { - i++; - continue; + if ((argv[i][0] == '-') && (strcmp(argv[i], "-") != 0)) { + i += skipArgs(argv[i]); + continue; } if ((timing) && (repeat)) startTimer(); - /* Remember file names. "-" means stdin. */ - if ((argv[i][0] != '-') || (strcmp(argv[i], "-") == 0)) { - if (repeat) { - xmlParserCtxtPtr ctxt; + if (repeat) { + xmlParserCtxtPtr ctxt; - ctxt = xmlNewParserCtxt(); - if (ctxt == NULL) { - progresult = XMLLINT_ERR_MEM; - goto error; + ctxt = xmlNewParserCtxt(); + if (ctxt == NULL) { + progresult = XMLLINT_ERR_MEM; + goto error; + } + if (maxAmpl > 0) + xmlCtxtSetMaxAmplification(ctxt, maxAmpl); + + for (acount = 0;acount < repeat;acount++) { +#ifdef LIBXML_READER_ENABLED + if (stream != 0) { + streamFile(argv[i]); + } else { +#endif /* LIBXML_READER_ENABLED */ + if (sax) { + testSAX(argv[i]); + } else { + parseAndPrintFile(argv[i], ctxt); + } +#ifdef LIBXML_READER_ENABLED } - if (maxAmpl > 0) - xmlCtxtSetMaxAmplification(ctxt, maxAmpl); +#endif /* LIBXML_READER_ENABLED */ + } - for (acount = 0;acount < repeat;acount++) { + xmlFreeParserCtxt(ctxt); + } else { #ifdef LIBXML_READER_ENABLED - if (stream != 0) { - streamFile(argv[i]); - } else { + if (stream != 0) + streamFile(argv[i]); + else #endif /* LIBXML_READER_ENABLED */ - if (sax) { - testSAX(argv[i]); - } else { - parseAndPrintFile(argv[i], ctxt); - } -#ifdef LIBXML_READER_ENABLED - } -#endif /* LIBXML_READER_ENABLED */ - } - - xmlFreeParserCtxt(ctxt); - } else { -#ifdef LIBXML_READER_ENABLED - if (stream != 0) - streamFile(argv[i]); - else -#endif /* LIBXML_READER_ENABLED */ - if (sax) { - testSAX(argv[i]); - } else { - parseAndPrintFile(argv[i], NULL); - } - } - files ++; - if ((timing) && (repeat)) { - endTimer("%d iterations", repeat); - } - } + if (sax) { + testSAX(argv[i]); + } else { + parseAndPrintFile(argv[i], NULL); + } + } + files ++; + if ((timing) && (repeat)) { + endTimer("%d iterations", repeat); + } } if (generate) parseAndPrintFile(NULL, NULL);