1
0
mirror of https://github.com/GNOME/libxml2.git synced 2025-10-14 02:58:39 +08:00

tests: Always use xmlMalloc/xmlFree

This commit is contained in:
Nick Wellnhofer
2025-05-16 22:07:39 +02:00
parent c136118dcc
commit 997830a338
2 changed files with 56 additions and 55 deletions

View File

@@ -166,13 +166,13 @@ static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
if (hFind == INVALID_HANDLE_VALUE)
return(0);
nb_paths = 20;
ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
ret->gl_pathv = (char **) xmlMalloc(nb_paths * sizeof(char *));
if (ret->gl_pathv == NULL) {
FindClose(hFind);
return(-1);
}
strncpy(directory + len, FindFileData.cFileName, 499 - len);
ret->gl_pathv[ret->gl_pathc] = strdup(directory);
ret->gl_pathv[ret->gl_pathc] = xmlMemStrdup(directory);
if (ret->gl_pathv[ret->gl_pathc] == NULL)
goto done;
ret->gl_pathc++;
@@ -180,14 +180,14 @@ static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
if (FindFileData.cFileName[0] == '.')
continue;
if (ret->gl_pathc + 2 > nb_paths) {
char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
char **tmp = xmlRealloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
if (tmp == NULL)
break;
ret->gl_pathv = tmp;
nb_paths *= 2;
}
strncpy(directory + len, FindFileData.cFileName, 499 - len);
ret->gl_pathv[ret->gl_pathc] = strdup(directory);
ret->gl_pathv[ret->gl_pathc] = xmlMemStrdup(directory);
if (ret->gl_pathv[ret->gl_pathc] == NULL)
break;
ret->gl_pathc++;
@@ -208,8 +208,9 @@ static void globfree(glob_t *pglob) {
for (i = 0;i < pglob->gl_pathc;i++) {
if (pglob->gl_pathv[i] != NULL)
free(pglob->gl_pathv[i]);
xmlFree(pglob->gl_pathv[i]);
}
xmlFree(pglob->gl_pathv);
}
#elif HAVE_DECL_GLOB
@@ -366,7 +367,7 @@ static char *resultFilename(const char *filename, const char *out,
if (snprintf(res, 499, "%s%s%s", out, base, suffixbuff) >= 499)
res[499] = 0;
return(strdup(res));
return(xmlMemStrdup(res));
}
static int checkTestFile(const char *filename) {
@@ -515,12 +516,12 @@ static int loadMem(const char *filename, const char **mem, int *size) {
int siz = 0;
if (stat(filename, &info) < 0)
return(-1);
base = malloc(info.st_size + 1);
base = xmlMalloc(info.st_size + 1);
if (base == NULL)
return(-1);
fd = open(filename, RD_FLAGS);
if (fd < 0) {
free(base);
xmlFree(base);
return(-1);
}
while ((res = read(fd, &base[siz], info.st_size - siz)) > 0) {
@@ -529,7 +530,7 @@ static int loadMem(const char *filename, const char **mem, int *size) {
close(fd);
#if !defined(_WIN32)
if (siz != info.st_size) {
free(base);
xmlFree(base);
return(-1);
}
#endif
@@ -540,7 +541,7 @@ static int loadMem(const char *filename, const char **mem, int *size) {
}
static int unloadMem(const char *mem) {
free((char *)mem);
xmlFree((char *)mem);
return(0);
}
@@ -1486,7 +1487,7 @@ saxParseTest(const char *filename, const char *result,
SAXdebug = fopen(temp, "wb");
if (SAXdebug == NULL) {
fprintf(stderr, "Failed to write to %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
@@ -1559,7 +1560,7 @@ saxParseTest(const char *filename, const char *result,
done:
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(ret);
@@ -1731,7 +1732,7 @@ htmlTokenizerTest(const char *filename, const char *result,
SAXdebug = fopen(temp, "wb");
if (SAXdebug == NULL) {
fprintf(stderr, "Failed to write to %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
@@ -1783,7 +1784,7 @@ htmlTokenizerTest(const char *filename, const char *result,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(ret);
@@ -1857,7 +1858,7 @@ oldParseTest(const char *filename, const char *result,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(res);
@@ -1934,7 +1935,7 @@ pushParseTest(const char *filename, const char *result,
#endif
res = ctxt->wellFormed;
xmlFreeParserCtxt(ctxt);
free((char *)base);
xmlFree((char *)base);
if (!res) {
xmlFreeDoc(doc);
fprintf(stderr, "Failed to parse %s\n", filename);
@@ -2238,7 +2239,7 @@ pushBoundaryTest(const char *filename, const char *result,
#endif
res = ctxt->wellFormed;
xmlFreeParserCtxt(ctxt);
free((char *)base);
xmlFree((char *)base);
if (numCallbacks > 1) {
xmlFreeDoc(doc);
fprintf(stderr, "Failed push boundary callback test (%d@%lu-%lu): %s\n",
@@ -2467,7 +2468,7 @@ noentParseTest(const char *filename, const char *result,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(res);
}
@@ -2685,7 +2686,7 @@ streamProcessTest(const char *filename, const char *result, const char *err,
t = fopen(temp, "wb");
if (t == NULL) {
fprintf(stderr, "Can't open temp file %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
}
@@ -2698,7 +2699,7 @@ streamProcessTest(const char *filename, const char *result, const char *err,
fclose(t);
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(0);
}
@@ -2725,7 +2726,7 @@ streamProcessTest(const char *filename, const char *result, const char *err,
ret = compareFiles(temp, result);
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
if (ret) {
fprintf(stderr, "Result for %s failed in %s\n", filename, result);
@@ -2820,7 +2821,7 @@ streamMemParseTest(const char *filename, const char *result, const char *err,
xmlTextReaderSetStructuredErrorHandler(reader, testStructuredErrorHandler,
NULL);
ret = streamProcessTest(filename, result, err, reader, NULL, options);
free((char *)base);
xmlFree((char *)base);
xmlFreeTextReader(reader);
return(ret);
}
@@ -2898,7 +2899,7 @@ xpathCommonTest(const char *filename, const char *result,
xpathOutput = fopen(temp, "wb");
if (xpathOutput == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
@@ -2906,7 +2907,7 @@ xpathCommonTest(const char *filename, const char *result,
if (input == NULL) {
fprintf(stderr,
"Cannot open %s for reading\n", filename);
free(temp);
xmlFree(temp);
return(-1);
}
while (fgets(expression, 4500, input) != NULL) {
@@ -2935,7 +2936,7 @@ xpathCommonTest(const char *filename, const char *result,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(ret);
}
@@ -3094,7 +3095,7 @@ xmlidDocTest(const char *filename,
if (xpathOutput == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
xmlFreeDoc(xpathDocument);
free(temp);
xmlFree(temp);
return(-1);
}
@@ -3111,7 +3112,7 @@ xmlidDocTest(const char *filename,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
xmlFreeDoc(xpathDocument);
@@ -3190,7 +3191,7 @@ uriCommonTest(const char *filename,
o = fopen(temp, "wb");
if (o == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
f = fopen(filename, "rb");
@@ -3199,7 +3200,7 @@ uriCommonTest(const char *filename,
fclose(o);
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(-1);
}
@@ -3245,7 +3246,7 @@ uriCommonTest(const char *filename,
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(res);
}
@@ -4079,7 +4080,7 @@ patternTest(const char *filename,
if (o == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
fclose(f);
free(temp);
xmlFree(temp);
return(-1);
}
while (1) {
@@ -4165,7 +4166,7 @@ patternTest(const char *filename,
}
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(ret);
}
@@ -4273,7 +4274,7 @@ xmlFree(expr);
buffer = (xmlChar **) \
xmlRealloc(buffer, buffer_size * sizeof(xmlChar*)); \
if (buffer == NULL) { \
perror("realloc failed"); \
perror("xmlRealloc failed"); \
return(NULL); \
} \
}
@@ -4300,7 +4301,7 @@ parse_list(xmlChar *str) {
buffer_size = 1000;
buffer = (xmlChar **) xmlMalloc(buffer_size * sizeof(xmlChar*));
if (buffer == NULL) {
perror("malloc failed");
perror("xmlMalloc failed");
return(NULL);
}
out = buffer;
@@ -4402,7 +4403,7 @@ c14nRunTest(const char* xml_filename, int with_comments, int mode,
if (result != NULL) xmlFree(result);
if(xpath != NULL) xmlXPathFreeObject(xpath);
if (inclusive_namespaces != NULL) xmlFree(inclusive_namespaces);
if (nslist != NULL) free((char *) nslist);
if (nslist != NULL) xmlFree((char *) nslist);
xmlFreeDoc(doc);
return(ret);
@@ -4428,16 +4429,16 @@ c14nCommonTest(const char *filename, int with_comments, int mode,
if (snprintf(buf, 499, "result/c14n/%s/%s", subdir, prefix) >= 499)
buf[499] = 0;
result = strdup(buf);
result = xmlMemStrdup(buf);
if (snprintf(buf, 499, "test/c14n/%s/%s.xpath", subdir, prefix) >= 499)
buf[499] = 0;
if (checkTestFile(buf)) {
xpath = strdup(buf);
xpath = xmlMemStrdup(buf);
}
if (snprintf(buf, 499, "test/c14n/%s/%s.ns", subdir, prefix) >= 499)
buf[499] = 0;
if (checkTestFile(buf)) {
ns = strdup(buf);
ns = xmlMemStrdup(buf);
}
nb_tests++;
@@ -4445,9 +4446,9 @@ c14nCommonTest(const char *filename, int with_comments, int mode,
xpath, ns, result) < 0)
ret = 1;
if (result != NULL) free(result);
if (xpath != NULL) free(xpath);
if (ns != NULL) free(ns);
if (result != NULL) xmlFree(result);
if (xpath != NULL) xmlFree(xpath);
if (ns != NULL) xmlFree(ns);
return(ret);
}
@@ -4718,7 +4719,7 @@ regexpTest(const char *filename, const char *result, const char *err,
output = fopen(temp, "wb");
if (output == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
free(temp);
xmlFree(temp);
ret = -1;
goto done;
}
@@ -4769,7 +4770,7 @@ regexpTest(const char *filename, const char *result, const char *err,
}
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
ret = compareFileMem(err, testErrors, testErrorsSize);
@@ -4837,7 +4838,7 @@ automataTest(const char *filename, const char *result,
output = fopen(temp, "wb");
if (output == NULL) {
fprintf(stderr, "failed to open output file %s\n", temp);
free(temp);
xmlFree(temp);
return(-1);
}
@@ -5014,7 +5015,7 @@ automataTest(const char *filename, const char *result,
}
if (temp != NULL) {
unlink(temp);
free(temp);
xmlFree(temp);
}
return(res);
@@ -5343,9 +5344,9 @@ launchTests(testDescPtr tst) {
}
testErrorsSize = 0;
if (result)
free(result);
xmlFree(result);
if (error)
free(error);
xmlFree(error);
}
globfree(&globbuf);
} else {

View File

@@ -99,13 +99,13 @@ static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
if (hFind == INVALID_HANDLE_VALUE)
return(0);
nb_paths = 20;
ret->gl_pathv = (char **) malloc(nb_paths * sizeof(char *));
ret->gl_pathv = (char **) xmlMalloc(nb_paths * sizeof(char *));
if (ret->gl_pathv == NULL) {
FindClose(hFind);
return(-1);
}
strncpy(directory + len, FindFileData.cFileName, 499 - len);
ret->gl_pathv[ret->gl_pathc] = strdup(directory);
ret->gl_pathv[ret->gl_pathc] = xmlMemStrdup(directory);
if (ret->gl_pathv[ret->gl_pathc] == NULL)
goto done;
ret->gl_pathc++;
@@ -113,14 +113,14 @@ static int glob(const char *pattern, ATTRIBUTE_UNUSED int flags,
if (FindFileData.cFileName[0] == '.')
continue;
if (ret->gl_pathc + 2 > nb_paths) {
char **tmp = realloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
char **tmp = xmlRealloc(ret->gl_pathv, nb_paths * 2 * sizeof(char *));
if (tmp == NULL)
break;
ret->gl_pathv = tmp;
nb_paths *= 2;
}
strncpy(directory + len, FindFileData.cFileName, 499 - len);
ret->gl_pathv[ret->gl_pathc] = strdup(directory);
ret->gl_pathv[ret->gl_pathc] = xmlMemStrdup(directory);
if (ret->gl_pathv[ret->gl_pathc] == NULL)
break;
ret->gl_pathc++;
@@ -141,7 +141,7 @@ static void globfree(glob_t *pglob) {
for (i = 0;i < pglob->gl_pathc;i++) {
if (pglob->gl_pathv[i] != NULL)
free(pglob->gl_pathv[i]);
xmlFree(pglob->gl_pathv[i]);
}
}
@@ -430,7 +430,7 @@ static char *resultFilename(const char *filename, const char *out,
if (snprintf(res, 499, "%s%s%s", out, base, suffixbuff) >= 499)
res[499] = 0;
return(strdup(res));
return(xmlMemStrdup(res));
}
static int checkTestFile(const char *filename) {
@@ -846,9 +846,9 @@ launchTests(testDescPtr tst) {
}
}
if (result)
free(result);
xmlFree(result);
if (error)
free(error);
xmlFree(error);
}
globfree(&globbuf);
} else {