malloc-fail: Report malloc failure in xmlXPathTranslateFunction

This commit is contained in:
Nick Wellnhofer 2023-12-15 14:53:44 +01:00
parent 69d88566b6
commit 6ae98ebc64

74
xpath.c
View File

@ -8413,42 +8413,46 @@ xmlXPathTranslateFunction(xmlXPathParserContextPtr ctxt, int nargs) {
goto error;
target = xmlBufCreate();
if (target) {
max = xmlUTF8Strlen(to->stringval);
for (cptr = str->stringval; (ch=*cptr); ) {
offset = xmlUTF8Strloc(from->stringval, cptr);
if (offset >= 0) {
if (offset < max) {
point = xmlUTF8Strpos(to->stringval, offset);
if (point)
xmlBufAdd(target, point, xmlUTF8Strsize(point, 1));
}
} else
xmlBufAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
/* Step to next character in input */
cptr++;
if ( ch & 0x80 ) {
/* if not simple ascii, verify proper format */
if ( (ch & 0xc0) != 0xc0 ) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathTranslateFunction: Invalid UTF8 string\n");
/* not asserting an XPath error is probably better */
break;
}
/* then skip over remaining bytes for this char */
while ( (ch <<= 1) & 0x80 )
if ( (*cptr++ & 0xc0) != 0x80 ) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathTranslateFunction: Invalid UTF8 string\n");
/* not asserting an XPath error is probably better */
break;
}
if (ch & 0x80) /* must have had error encountered */
break;
}
}
if (target == NULL) {
xmlXPathPErrMemory(ctxt, NULL);
goto error;
}
max = xmlUTF8Strlen(to->stringval);
for (cptr = str->stringval; (ch=*cptr); ) {
offset = xmlUTF8Strloc(from->stringval, cptr);
if (offset >= 0) {
if (offset < max) {
point = xmlUTF8Strpos(to->stringval, offset);
if (point)
xmlBufAdd(target, point, xmlUTF8Strsize(point, 1));
}
} else
xmlBufAdd(target, cptr, xmlUTF8Strsize(cptr, 1));
/* Step to next character in input */
cptr++;
if ( ch & 0x80 ) {
/* if not simple ascii, verify proper format */
if ( (ch & 0xc0) != 0xc0 ) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathTranslateFunction: Invalid UTF8 string\n");
/* not asserting an XPath error is probably better */
break;
}
/* then skip over remaining bytes for this char */
while ( (ch <<= 1) & 0x80 )
if ( (*cptr++ & 0xc0) != 0x80 ) {
xmlGenericError(xmlGenericErrorContext,
"xmlXPathTranslateFunction: Invalid UTF8 string\n");
/* not asserting an XPath error is probably better */
break;
}
if (ch & 0x80) /* must have had error encountered */
break;
}
}
valuePush(ctxt, xmlXPathCacheNewString(ctxt, xmlBufContent(target)));
xmlBufFree(target);
error: