diff options
Diffstat (limited to 'xmllint.c')
-rw-r--r-- | xmllint.c | 104 |
1 files changed, 98 insertions, 6 deletions
@@ -102,6 +102,7 @@ static int shell = 0; static int debugent = 0; #endif static int debug = 0; +static int maxmem = 0; #ifdef LIBXML_TREE_ENABLED static int copy = 0; #endif /* LIBXML_TREE_ENABLED */ @@ -129,7 +130,7 @@ static xmlSchemaPtr wxschemas = NULL; #endif static int repeat = 0; static int insert = 0; -#ifdef LIBXML_HTML_ENABLED +#if defined(LIBXML_HTML_ENABLED) || defined(LIBXML_VALID_ENABLED) static int html = 0; static int xmlout = 0; #endif @@ -160,6 +161,7 @@ static int stream = 0; static int walker = 0; #endif /* LIBXML_READER_ENABLED */ static int chkregister = 0; +static int nbregister = 0; #ifdef LIBXML_SAX1_ENABLED static int sax1 = 0; #endif /* LIBXML_SAX1_ENABLED */ @@ -169,10 +171,75 @@ static xmlPatternPtr patternc = NULL; #endif static int options = 0; -/* - * Internal timing routines to remove the necessity to have unix-specific - * function calls - */ +/************************************************************************ + * * + * Memory allocation consumption debugging * + * * + ************************************************************************/ + +static void +OOM(void) +{ + fprintf(stderr, "Ran out of memory needs > %d bytes\n", maxmem); + progresult = 9; +} + +static void +myFreeFunc(void *mem) +{ + xmlMemFree(mem); +} +static void * +myMallocFunc(size_t size) +{ + void *ret; + + ret = xmlMemMalloc(size); + if (ret != NULL) { + if (xmlMemUsed() > maxmem) { + OOM(); + xmlMemFree(ret); + return (NULL); + } + } + return (ret); +} +static void * +myReallocFunc(void *mem, size_t size) +{ + void *ret; + + ret = xmlMemRealloc(mem, size); + if (ret != NULL) { + if (xmlMemUsed() > maxmem) { + OOM(); + xmlMemFree(ret); + return (NULL); + } + } + return (ret); +} +static char * +myStrdupFunc(const char *str) +{ + char *ret; + + ret = xmlMemoryStrdup(str); + if (ret != NULL) { + if (xmlMemUsed() > maxmem) { + OOM(); + xmlFree(ret); + return (NULL); + } + } + return (ret); +} +/************************************************************************ + * * + * Internal timing routines to remove the necessity to have * + * unix-specific function calls. * + * * + ************************************************************************/ #ifndef HAVE_GETTIMEOFDAY #ifdef HAVE_SYS_TIMEB_H @@ -1079,7 +1146,7 @@ static void parseAndPrintFile(char *filename, xmlParserCtxtPtr rectxt) { if ((timing) && (!repeat)) { startTimer(); } -#ifdef LIBXML_VALID_ENABLED +#ifdef LIBXML_HTML_ENABLED if ((html) && (!xmlout)) { if (compress) { htmlSaveFile(output ? output : "-", doc); @@ -1475,6 +1542,7 @@ static void usage(const char *name) { #ifdef HAVE_SYS_MMAN_H printf("\t--memory : parse from memory\n"); #endif + printf("\t--maxmem nbbytes : limits memory allocation to nbbytes bytes\n"); printf("\t--nowarning : do not emit warnings from parser/validator\n"); printf("\t--noblanks : drop (ignorable?) blanks spaces\n"); printf("\t--nocdata : replace cdata section with text nodes\n"); @@ -1517,6 +1585,7 @@ static void registerNode(xmlNodePtr node) { node->_private = malloc(sizeof(long)); *(long*)node->_private = (long) 0x81726354; + nbregister++; } static void deregisterNode(xmlNodePtr node) @@ -1524,6 +1593,7 @@ static void deregisterNode(xmlNodePtr node) assert(node->_private != NULL); assert(*(long*)node->_private == (long) 0x81726354); free(node->_private); + nbregister--; } int @@ -1731,6 +1801,16 @@ main(int argc, char **argv) { noblanks++; xmlKeepBlanksDefault(0); } + 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"))) { noblanks++; @@ -1940,6 +2020,11 @@ main(int argc, char **argv) { i++; continue; } + if ((!strcmp(argv[i], "-maxmem")) || + (!strcmp(argv[i], "--maxmem"))) { + i++; + continue; + } if ((!strcmp(argv[i], "-schema")) || (!strcmp(argv[i], "--schema"))) { i++; @@ -1975,12 +2060,19 @@ main(int argc, char **argv) { if (ctxt != NULL) xmlFreeParserCtxt(ctxt); } else { + nbregister = 0; + #ifdef LIBXML_READER_ENABLED if (stream != 0) streamFile(argv[i]); else #endif /* LIBXML_READER_ENABLED */ parseAndPrintFile(argv[i], NULL); + + if ((chkregister) && (nbregister != 0)) { + fprintf(stderr, "Registration count off: %d\n", nbregister); + progresult = 8; + } } files ++; if ((timing) && (repeat)) { |