1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-12-23 17:33:50 +03:00
libxml2/fuzz/uri.c
Nick Wellnhofer da996c8d0f uri: Report malloc failures
Fix many places where malloc failures weren't reported, for example
after calling xmlStrdup.

Introduce new public API functions that return a separate error code if
a memory allocation fails:

- xmlParseURISafe
- xmlBuildURISafe
- xmlBuildRelativeURISafe

Update the fuzzer to check whether malloc failures are reported.
2023-12-11 22:05:47 +01:00

105 lines
2.7 KiB
C

/*
* uri.c: a libFuzzer target to test the URI module.
*
* See Copyright for the status of this software.
*/
#include <libxml/uri.h>
#include "fuzz.h"
int
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
char ***argv ATTRIBUTE_UNUSED) {
xmlFuzzMemSetup();
xmlSetGenericErrorFunc(NULL, xmlFuzzErrorFunc);
return 0;
}
int
LLVMFuzzerTestOneInput(const char *data, size_t size) {
xmlURIPtr uri;
size_t maxAlloc;
const char *str1, *str2;
char *copy;
xmlChar *strRes;
int intRes;
if (size > 10000)
return(0);
xmlFuzzDataInit(data, size);
maxAlloc = xmlFuzzReadInt(4) % (size * 8 + 100);
str1 = xmlFuzzReadString(NULL);
str2 = xmlFuzzReadString(NULL);
xmlFuzzMemSetLimit(maxAlloc);
xmlFuzzResetMallocFailed();
intRes = xmlParseURISafe(str1, &uri);
xmlFuzzCheckMallocFailure("xmlParseURISafe", intRes == -1);
if (uri != NULL) {
xmlFuzzResetMallocFailed();
strRes = xmlSaveUri(uri);
xmlFuzzCheckMallocFailure("xmlSaveURI", strRes == NULL);
xmlFree(strRes);
xmlFreeURI(uri);
}
xmlFreeURI(xmlParseURI(str1));
uri = xmlParseURIRaw(str1, 1);
xmlFree(xmlSaveUri(uri));
xmlFreeURI(uri);
xmlFuzzResetMallocFailed();
strRes = BAD_CAST xmlURIUnescapeString(str1, -1, NULL);
xmlFuzzCheckMallocFailure("xmlURIUnescapeString",
str1 != NULL && strRes == NULL);
xmlFree(strRes);
xmlFree(xmlURIEscape(BAD_CAST str1));
xmlFuzzResetMallocFailed();
strRes = xmlCanonicPath(BAD_CAST str1);
xmlFuzzCheckMallocFailure("xmlCanonicPath",
str1 != NULL && strRes == NULL);
xmlFree(strRes);
xmlFuzzResetMallocFailed();
strRes = xmlPathToURI(BAD_CAST str1);
xmlFuzzCheckMallocFailure("xmlPathToURI", str1 != NULL && strRes == NULL);
xmlFree(strRes);
xmlFuzzResetMallocFailed();
intRes = xmlBuildURISafe(BAD_CAST str2, BAD_CAST str1, &strRes);
xmlFuzzCheckMallocFailure("xmlBuildURISafe", intRes == -1);
xmlFree(strRes);
xmlFree(xmlBuildURI(BAD_CAST str2, BAD_CAST str1));
xmlFuzzResetMallocFailed();
intRes = xmlBuildRelativeURISafe(BAD_CAST str2, BAD_CAST str1, &strRes);
xmlFuzzCheckMallocFailure("xmlBuildRelativeURISafe", intRes == -1);
xmlFree(strRes);
xmlFree(xmlBuildRelativeURI(BAD_CAST str2, BAD_CAST str1));
xmlFuzzResetMallocFailed();
strRes = xmlURIEscapeStr(BAD_CAST str1, BAD_CAST str2);
xmlFuzzCheckMallocFailure("xmlURIEscapeStr",
str1 != NULL && strRes == NULL);
xmlFree(strRes);
copy = (char *) xmlCharStrdup(str1);
xmlNormalizeURIPath(copy);
xmlFree(copy);
xmlFuzzMemSetLimit(0);
xmlFuzzDataCleanup();
return 0;
}