Skip to content

Commit

Permalink
fix memory bugs found by coverity in this badly written fxt1 code
Browse files Browse the repository at this point in the history
  • Loading branch information
richard42 committed Mar 27, 2015
1 parent 1ed2d5c commit 438dd4f
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/GlideHQ/tc-1.1+/fxt1.c
Expand Up @@ -1092,7 +1092,7 @@ fxt1_encode (dword width, dword height, int comps,
dword x, y;
const byte *data;
dword *encoded = (dword *)dest;
void *newSource = NULL, *newSourcetmp = NULL;
void *newSource = NULL;

assert(comps == 3 || comps == 4);

Expand All @@ -1101,29 +1101,28 @@ fxt1_encode (dword width, dword height, int comps,
if (comps == 4)
newSource = reorder_source_4_alloc(source, width, height, srcRowStride);
if (!newSource)
goto cleanUp;
source = newSource;
return;

/* Replicate image if width is not M8 or height is not M4 */
if ((width & 7) | (height & 3)) {
int newWidth = (width + 7) & ~7;
int newHeight = (height + 3) & ~3;
newSourcetmp = malloc(comps * newWidth * newHeight * sizeof(byte));
free(newSource);
newSource = newSourcetmp;
if (!newSource) {
goto cleanUp;
void *newSourcetmp = malloc(comps * newWidth * newHeight * sizeof(byte));
if (!newSourcetmp) {
free(newSource);
return;
}
upscale_teximage2d(width, height, newWidth, newHeight,
comps, (const byte *) source,
srcRowStride, (byte *) newSource);
source = newSource;
comps, (const byte *) newSource,
srcRowStride, (byte *) newSourcetmp);
free(newSource);
newSource = newSourcetmp;
width = newWidth;
height = newHeight;
srcRowStride = comps * newWidth;
}

data = (const byte *) source;
data = (const byte *) newSource;
destRowStride = (destRowStride - width * 2) / 4;
for (y = 0; y < height; y += 4) {
dword offs = 0 + (y + 0) * srcRowStride;
Expand All @@ -1141,7 +1140,6 @@ fxt1_encode (dword width, dword height, int comps,
encoded += destRowStride;
}

cleanUp:
free(newSource);
}

Expand Down

0 comments on commit 438dd4f

Please sign in to comment.