Fix tests based on coverity reports (#819)

* Partial #818, unused arg

* Partial #818, init var

* partail #818, fix va_args usage

* partail #818, handle f* calls and avoid possible call to malloc with negative values

* partail #818, prevent double free

* partail #818, resource leak if test fail

* partail #818, null deref fix

* partail #818, avoid double free on fp failure

* Partial #818, fix error msg

* Partial #818, leak on error

* Partial #818, null deref

* Partial #818, avoid possible negative index on failure

* partial #818, does not free if we return if requested new size overflow

* partial #818, avoid double free, free where the alloc happened
master
Pierre Joye 2022-02-01 15:09:01 +07:00 committed by GitHub
parent 3d760c2c21
commit 167ea1f4f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 62 additions and 40 deletions

View File

@ -18,18 +18,17 @@ int main()
{
gdImagePtr im, im2;
FILE *fp;
char path[2048];
fp=fopen("resampledbug.jpeg", "rb");
if (!fp) {
fprintf(stderr, "Can't load /home/pierre/IM3801.jpg\n");
fprintf(stderr, "Can't load resampledbug.jpeg\n");
return 1;
}
im = gdImageCreateFromJpeg(fp);
fclose(fp);
if (!im) {
fprintf(stderr, "Can't load TIFF image %s\n", path);
fprintf(stderr, "Can't decode JPEG image resampledbug.jpeg\n");
return 1;
}

View File

@ -3831,12 +3831,12 @@ static void gdImageSetAAPixelColor(gdImagePtr im, int x, int y, int color, int t
*/
BGD_DECLARE(void) gdImageSetStyle (gdImagePtr im, int *style, int noOfPixels)
{
if (im->style) {
gdFree (im->style);
}
if (overflow2(sizeof (int), noOfPixels)) {
return;
}
if (im->style) {
gdFree (im->style);
}
im->style = (int *) gdMalloc (sizeof (int) * noOfPixels);
if (!im->style) {
return;

View File

@ -3,7 +3,7 @@
#include <stdlib.h>
#include "gdtest.h"
int main(int argc, char *argv[])
int main()
{
int error = 0, i = 0;
gdImagePtr im, exp;
@ -27,6 +27,9 @@ int main(int argc, char *argv[])
return 1;
}
im = gdImageCreateFromGd2(fp);
if (gdTestAssert(im == NULL)) {
gdTestErrorMsg("failed, cannot decode file: %s\n", path[0]);
}
fclose(fp);
if (path_exp[i] != NULL) {

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
#include "gdtest.h"
int main(int argc, char *argv[])
int main()
{
gdImagePtr im;
FILE *fp;

View File

@ -30,34 +30,32 @@ int main()
c3 = gdImageColorExactAlpha(im, 255, 0, 255, 100);
c4 = gdImageColorExactAlpha(im, 255, 34, 255, 100);
if (gdTestAssert(c1 == 0) != 1) {
error = -1;
color = gdTrueColorAlpha(gdImageRed(im, c1), gdImageGreen(im, c1),
gdImageBlue(im, c1), 0);
if (gdTestAssert(color == 0xFF00FF) != 1) {
error = -1;
}
}
if (gdTestAssert(c2 == 1) != 1) {
error = -1;
color = gdTrueColorAlpha(gdImageRed(im, c2), gdImageGreen(im, c2),
gdImageBlue(im, c2), 0);
if (gdTestAssert(color == 0xFFC800) != 1) {
error = -1;
}
}
if (gdTestAssert(c3 == 2) != 1) {
error = -1;
color = gdTrueColorAlpha(gdImageRed(im, c3), gdImageGreen(im, c3),
gdImageBlue(im, c3), 0);
if (gdTestAssert(color == 0xFF00FF) != 1) {
error = -1;
}
}
if (gdTestAssert(c4 == -1) != 1) {
error = -1;
}
color = gdTrueColorAlpha(gdImageRed(im, c1), gdImageGreen(im, c1),
gdImageBlue(im, c1), 0);
if (gdTestAssert(color == 0xFF00FF) != 1) {
error = -1;
}
color = gdTrueColorAlpha(gdImageRed(im, c2), gdImageGreen(im, c2),
gdImageBlue(im, c2), 0);
if (gdTestAssert(color == 0xFFC800) != 1) {
error = -1;
}
color = gdTrueColorAlpha(gdImageRed(im, c3), gdImageGreen(im, c3),
gdImageBlue(im, c3), 0);
if (gdTestAssert(color == 0xFF00FF) != 1) {
error = -1;
}
gdImageDestroy(im);
return error;

View File

@ -21,13 +21,14 @@ int main()
file = gdTestTempFile("bug00002_1.png");
fp = fopen(file, "wb");
free(file);
if (fp == NULL) {
gdTestErrorMsg("Cannot create image from <%s>\n", file);
free(file);
gdImageDestroy(im);
return 1;
}
free(file);
gdImagePng(im,fp);
fclose(fp);

View File

@ -7,7 +7,7 @@ int main()
FILE *fp;
gdScatter s;
int colors[] = {0xFF0000, 0x00FF00};
CuTestImageResult r;
CuTestImageResult r = {0,0};
fp = gdTestFileOpen("gdimagescatterex/bug00208.png");
im = gdImageCreateFromPng(fp);

View File

@ -396,6 +396,7 @@ FILE *gdTestTempFp(void)
FILE *fp = fopen(file, "wb");
if (fp == NULL) {
printf("fail to open tmp file");
free(file);
return NULL;
}
free(file);
@ -425,6 +426,7 @@ char *gdTestFilePathV(const char *path, va_list args)
return NULL;
}
strcpy(file, GDTEST_TOP_DIR);
p = path;
do {
#if defined(_WIN32) && !defined(__MINGW32__) && !defined(__MINGW64__)
@ -435,7 +437,6 @@ char *gdTestFilePathV(const char *path, va_list args)
strcat(file, p);
} while ((p = va_arg(args, const char *)) != NULL);
va_end(args);
return file;
}
@ -443,8 +444,11 @@ char *gdTestFilePathV(const char *path, va_list args)
char *gdTestFilePathX(const char *path, ...)
{
va_list args;
char *res;
va_start(args, path);
return gdTestFilePathV(path, args);
res = gdTestFilePathV(path, args);
va_end(args);
return res;
}
FILE *gdTestFileOpenX(const char *path, ...)
@ -458,9 +462,12 @@ FILE *gdTestFileOpenX(const char *path, ...)
fp = fopen(file, "rb");
if (fp == NULL) {
printf("failed to open path (rb).");
free(file);
va_end(args);
return NULL;
}
free(file);
va_end(args);
return fp;
}
@ -606,6 +613,7 @@ int gdTestImageCompareToImage(const char* file, unsigned int line, const char* m
}
surface_diff = gdImageCreateTrueColor(width_a, height_a);
if (surface_diff == NULL) goto fail;
gdTestImageDiff(expected, actual, surface_diff, &result);
if (result.pixels_changed>0) {
@ -636,7 +644,7 @@ int gdTestImageCompareToImage(const char* file, unsigned int line, const char* m
gdImagePng(surface_diff,fp);
fclose(fp);
gdImageDestroy(surface_diff);
surface_diff = NULL;
fp = fopen(file_out, "wb");
if (!fp) goto fail;
gdImagePng(actual, fp);
@ -645,6 +653,7 @@ int gdTestImageCompareToImage(const char* file, unsigned int line, const char* m
} else {
if (surface_diff) {
gdImageDestroy(surface_diff);
surface_diff = NULL;
}
return 1;
}

View File

@ -7,7 +7,13 @@ int main()
{
gdImagePtr im;
FILE *fp = gdTestFileOpen("tga/bug00084.tga");
if (gdTestAssert(fp == NULL)) {
return 1;
}
im = gdImageCreateFromTga(fp);
gdImageDestroy(im);
fclose(fp);
if (gdTestAssert(im != NULL)) {
gdImageDestroy(im);
}
return 0;
}

View File

@ -28,7 +28,9 @@ static void check_file(char *basename)
size = read_test_file(&buffer, basename);
im = gdImageCreateFromTgaPtr(size, (void *) buffer);
gdTestAssert(im == NULL);
if (!gdTestAssert(im == NULL)) {
gdImageDestroy(im);
}
free(buffer);
}

View File

@ -25,13 +25,15 @@ int main()
static void check_file(char *basename)
{
gdImagePtr im;
char *buffer;
char *buffer = NULL;
size_t size;
size = read_test_file(&buffer, basename);
im = gdImageCreateFromTiffPtr(size, (void *) buffer);
gdTestAssert(im == NULL);
free(buffer);
if (buffer != NULL) {
free(buffer);
}
}
@ -43,18 +45,20 @@ static size_t read_test_file(char **buffer, char *basename)
filename = gdTestFilePath2("tiff", basename);
fp = fopen(filename, "rb");
gdTestAssert(fp != NULL);
if (gdTestAssert(fp != NULL)) goto fail3;
fseek(fp, 0, SEEK_END);
if (fseek(fp, 0, SEEK_END) != 0) goto fail2;
exp_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (fseek(fp, 0, SEEK_SET) != 0) goto fail2;
*buffer = malloc(exp_size);
gdTestAssert(*buffer != NULL);
if (gdTestAssert(*buffer != NULL)) goto fail2;
act_size = fread(*buffer, sizeof(**buffer), exp_size, fp);
gdTestAssert(act_size == exp_size);
fail2:
fclose(fp);
fail3:
free(filename);
return act_size;