From 9f4cb84c620bf9a6b7c4880f027e28511349a975 Mon Sep 17 00:00:00 2001 From: Nick Wellnhofer Date: Sun, 16 Jan 2022 18:39:51 +0100 Subject: [PATCH] Fix xmllint --maxmem xmlMemSetup must be called before initializing the parser, otherwise some data structures will be allocated with system malloc instead of our custom allocator. This throws off built-in memory debugging and sanitizers. --- xmllint.c | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/xmllint.c b/xmllint.c index a3fe10a2..2c7b9d04 100644 --- a/xmllint.c +++ b/xmllint.c @@ -3136,7 +3136,28 @@ main(int argc, char **argv) { usage(stderr, argv[0]); return(1); } + + /* xmlMemSetup must be called before initializing the parser. */ + for (i = 1; i < argc ; i++) { + if (!strcmp(argv[i], "-")) + break; + if (argv[i][0] != '-') + continue; + + if ((!strcmp(argv[i], "-maxmem")) || + (!strcmp(argv[i], "--maxmem"))) { + i++; + if (sscanf(argv[i], "%d", &maxmem) == 1) { + xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc, + myStrdupFunc); + } else { + maxmem = 0; + } + } + } + LIBXML_TEST_VERSION + for (i = 1; i < argc ; i++) { if (!strcmp(argv[i], "-")) break; @@ -3382,12 +3403,6 @@ main(int argc, char **argv) { else if ((!strcmp(argv[i], "-maxmem")) || (!strcmp(argv[i], "--maxmem"))) { i++; - if (sscanf(argv[i], "%d", &maxmem) == 1) { - xmlMemSetup(myFreeFunc, myMallocFunc, myReallocFunc, - myStrdupFunc); - } else { - maxmem = 0; - } } else if ((!strcmp(argv[i], "-format")) || (!strcmp(argv[i], "--format"))) {