1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-23 01:52:48 +03:00

xmllint: Fix use-after-free with --maxmem

Fixes #498.
This commit is contained in:
Nick Wellnhofer
2023-03-14 13:02:36 +01:00
parent e7c3a4ca1b
commit d7daf9fd96
3 changed files with 29 additions and 9 deletions

View File

@@ -139,6 +139,8 @@ XMLPUBFUN void
/* /*
* These are specific to the XML debug memory wrapper. * These are specific to the XML debug memory wrapper.
*/ */
XMLPUBFUN size_t
xmlMemSize (void *ptr);
XMLPUBFUN int XMLPUBFUN int
xmlMemUsed (void); xmlMemUsed (void);
XMLPUBFUN int XMLPUBFUN int

View File

@@ -352,17 +352,14 @@ myMallocFunc(size_t size)
static void * static void *
myReallocFunc(void *mem, size_t size) myReallocFunc(void *mem, size_t size)
{ {
void *ret; size_t oldsize = xmlMemSize(mem);
ret = xmlMemRealloc(mem, size); if (xmlMemUsed() + size - oldsize > (size_t) maxmem) {
if (ret != NULL) {
if (xmlMemUsed() > maxmem) {
OOM(); OOM();
xmlMemFree(ret);
return (NULL); return (NULL);
} }
}
return (ret); return (xmlMemRealloc(mem, size));
} }
static char * static char *
myStrdupFunc(const char *str) myStrdupFunc(const char *str)

View File

@@ -558,6 +558,27 @@ xmlMemoryStrdup(const char *str) {
return(xmlMemStrdupLoc(str, "none", 0)); return(xmlMemStrdupLoc(str, "none", 0));
} }
/**
* xmlMemSize:
* @ptr: pointer to the memory allocation
*
* Returns the size of a memory allocation.
*/
size_t
xmlMemSize(void *ptr) {
MEMHDR *p;
if (ptr == NULL)
return(0);
p = CLIENT_2_HDR(ptr);
if (p->mh_tag != MEMTAG)
return(0);
return(p->mh_size);
}
/** /**
* xmlMemUsed: * xmlMemUsed:
* *