diff options
Diffstat (limited to 'progs/util/readtex.c')
-rw-r--r-- | progs/util/readtex.c | 51 |
1 files changed, 43 insertions, 8 deletions
diff --git a/progs/util/readtex.c b/progs/util/readtex.c index 8e923b6eb47..f6f511481ca 100644 --- a/progs/util/readtex.c +++ b/progs/util/readtex.c @@ -9,6 +9,7 @@ #include <GL/gl.h> #include <GL/glu.h> +#include <assert.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -89,6 +90,7 @@ static rawImageRec *RawImageOpen(const char *fileName) rawImageRec *raw; GLenum swapFlag; int x; + size_t result; endianTest.testWord = 1; if (endianTest.testByte[0] == 1) { @@ -109,14 +111,21 @@ static rawImageRec *RawImageOpen(const char *fileName) raw->file = fopen(baseName + 1, "rb"); if(raw->file == NULL) { perror(fileName); + free(raw); return NULL; } } - fread(raw, 1, 12, raw->file); + result = fread(raw, 1, 12, raw->file); + assert(result == 12); if (swapFlag) { - ConvertShort(&raw->imagic, 6); + ConvertShort(&raw->imagic, 1); + ConvertShort(&raw->type, 1); + ConvertShort(&raw->dim, 1); + ConvertShort(&raw->sizeX, 1); + ConvertShort(&raw->sizeY, 1); + ConvertShort(&raw->sizeZ, 1); } raw->tmp = (unsigned char *)malloc(raw->sizeX*256); @@ -129,6 +138,12 @@ static rawImageRec *RawImageOpen(const char *fileName) if (raw->tmp == NULL || raw->tmpR == NULL || raw->tmpG == NULL || raw->tmpB == NULL) { fprintf(stderr, "Out of memory!\n"); + free(raw->tmp); + free(raw->tmpR); + free(raw->tmpG); + free(raw->tmpB); + free(raw->tmpA); + free(raw); return NULL; } @@ -138,12 +153,22 @@ static rawImageRec *RawImageOpen(const char *fileName) raw->rowSize = (GLint *)malloc(x); if (raw->rowStart == NULL || raw->rowSize == NULL) { fprintf(stderr, "Out of memory!\n"); + free(raw->tmp); + free(raw->tmpR); + free(raw->tmpG); + free(raw->tmpB); + free(raw->tmpA); + free(raw->rowStart); + free(raw->rowSize); + free(raw); return NULL; } raw->rleEnd = 512 + (2 * x); fseek(raw->file, 512, SEEK_SET); - fread(raw->rowStart, 1, x, raw->file); - fread(raw->rowSize, 1, x, raw->file); + result = fread(raw->rowStart, 1, x, raw->file); + assert(result == x); + result = fread(raw->rowSize, 1, x, raw->file); + assert(result == x); if (swapFlag) { ConvertLong(raw->rowStart, (long) (x/sizeof(GLuint))); ConvertLong((GLuint *)raw->rowSize, (long) (x/sizeof(GLint))); @@ -173,11 +198,13 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z) { unsigned char *iPtr, *oPtr, pixel; int count, done = 0; + size_t result; if ((raw->type & 0xFF00) == 0x0100) { fseek(raw->file, (long) raw->rowStart[y+z*raw->sizeY], SEEK_SET); - fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY], - raw->file); + result = fread(raw->tmp, 1, (unsigned int)raw->rowSize[y+z*raw->sizeY], + raw->file); + assert(result == (unsigned int)raw->rowSize[y+z*raw->sizeY]); iPtr = raw->tmp; oPtr = buf; @@ -202,7 +229,8 @@ static void RawImageGetRow(rawImageRec *raw, unsigned char *buf, int y, int z) } else { fseek(raw->file, 512+(y*raw->sizeX)+(z*raw->sizeX*raw->sizeY), SEEK_SET); - fread(buf, 1, raw->sizeX, raw->file); + result = fread(buf, 1, raw->sizeX, raw->file); + assert(result == raw->sizeX); } } @@ -215,6 +243,7 @@ static void RawImageGetData(rawImageRec *raw, TK_RGBImageRec *final) final->data = (unsigned char *)malloc((raw->sizeX+1)*(raw->sizeY+1)*4); if (final->data == NULL) { fprintf(stderr, "Out of memory!\n"); + return; } ptr = final->data; @@ -250,6 +279,7 @@ static TK_RGBImageRec *tkRGBImageLoad(const char *fileName) final = (TK_RGBImageRec *)malloc(sizeof(TK_RGBImageRec)); if (final == NULL) { fprintf(stderr, "Out of memory!\n"); + RawImageClose(raw); return NULL; } final->sizeX = raw->sizeX; @@ -305,6 +335,7 @@ GLboolean LoadRGBMipmaps2( const char *imageFile, GLenum target, fprintf(stderr, "Error in LoadRGBMipmaps %d-component images not implemented\n", image->components ); + FreeImage(image); return GL_FALSE; } @@ -356,6 +387,7 @@ GLubyte *LoadRGBImage( const char *imageFile, GLint *width, GLint *height, fprintf(stderr, "Error in LoadRGBImage %d-component images not implemented\n", image->components ); + FreeImage(image); return NULL; } @@ -364,8 +396,10 @@ GLubyte *LoadRGBImage( const char *imageFile, GLint *width, GLint *height, bytes = image->sizeX * image->sizeY * image->components; buffer = (GLubyte *) malloc(bytes); - if (!buffer) + if (!buffer) { + FreeImage(image); return NULL; + } memcpy( (void *) buffer, (void *) image->data, bytes ); @@ -438,6 +472,7 @@ GLushort *LoadYUVImage( const char *imageFile, GLint *width, GLint *height ) fprintf(stderr, "Error in LoadYUVImage %d-component images not implemented\n", image->components ); + FreeImage(image); return NULL; } |