[CHG] A bit more secure, dealing with NULL pointer after memory allocation

This commit is contained in:
Crayon2000 2009-03-11 22:02:35 +00:00
parent 61db0e3998
commit eb1002f395
2 changed files with 55 additions and 58 deletions

View file

@ -67,7 +67,8 @@ void GRRLIB_SetBlend( unsigned char blendmode ) {
GX_SetBlendMode(GX_BM_SUBSTRACT, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR); GX_SetBlendMode(GX_BM_SUBSTRACT, GX_BL_SRCALPHA, GX_BL_INVSRCALPHA, GX_LO_CLEAR);
break; break;
case GRRLIB_BLEND_INV: case GRRLIB_BLEND_INV:
case 8: GX_SetBlendMode(GX_BM_BLEND, GX_BL_INVSRCCLR, GX_BL_INVSRCCLR, GX_LO_CLEAR); break; case 8:
GX_SetBlendMode(GX_BM_BLEND, GX_BL_INVSRCCLR, GX_BL_INVSRCCLR, GX_LO_CLEAR);
break; break;
} }
} }
@ -270,20 +271,21 @@ void GRRLIB_InitTileSet(struct GRRLIB_texImg *tex, unsigned int tilew, unsigned
GRRLIB_texImg *GRRLIB_LoadTexturePNG(const unsigned char my_png[]) { GRRLIB_texImg *GRRLIB_LoadTexturePNG(const unsigned char my_png[]) {
PNGUPROP imgProp; PNGUPROP imgProp;
IMGCTX ctx; IMGCTX ctx;
GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)malloc(sizeof(GRRLIB_texImg)); GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
ctx = PNGU_SelectImageFromBuffer(my_png); ctx = PNGU_SelectImageFromBuffer(my_png);
PNGU_GetImageProperties(ctx, &imgProp); PNGU_GetImageProperties(ctx, &imgProp);
my_texture->data = memalign(32, imgProp.imgWidth * imgProp.imgHeight * 4); my_texture->data = memalign(32, imgProp.imgWidth * imgProp.imgHeight * 4);
if(my_texture->data != NULL) {
PNGU_DecodeTo4x4RGBA8(ctx, imgProp.imgWidth, imgProp.imgHeight, my_texture->data, 255); PNGU_DecodeTo4x4RGBA8(ctx, imgProp.imgWidth, imgProp.imgHeight, my_texture->data, 255);
PNGU_ReleaseImageContext(ctx); PNGU_ReleaseImageContext(ctx);
my_texture->w = imgProp.imgWidth; my_texture->w = imgProp.imgWidth;
my_texture->h = imgProp.imgHeight; my_texture->h = imgProp.imgHeight;
my_texture->handlex = 0; my_texture->handley = 0;
my_texture->offsetx = 0; my_texture->offsety = 0;
my_texture->tiledtex = false;
GRRLIB_SetHandle( my_texture, 0, 0 ); GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture ); GRRLIB_FlushTex( my_texture );
}
}
return my_texture; return my_texture;
} }
@ -297,13 +299,16 @@ GRRLIB_texImg *GRRLIB_LoadTexturePNG(const unsigned char my_png[]) {
GRRLIB_texImg *GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]) { GRRLIB_texImg *GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]) {
struct jpeg_decompress_struct cinfo; struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr; struct jpeg_error_mgr jerr;
GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)malloc(sizeof(GRRLIB_texImg)); GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)calloc(1, sizeof(GRRLIB_texImg));
int n = 0; int n = 0;
unsigned int i; unsigned int i;
if ((my_jpg[0]==0xff) && (my_jpg[1]==0xd8) && (my_jpg[2]==0xff)) { if(my_texture == NULL)
return NULL;
if ((my_jpg[0]==0xFF) && (my_jpg[1]==0xD8) && (my_jpg[2]==0xFF)) {
while(true) { while(true) {
if ((my_jpg[n]==0xff) && (my_jpg[n+1]==0xd9)) if ((my_jpg[n]==0xFF) && (my_jpg[n+1]==0xD9))
break; break;
n++; n++;
} }
@ -340,9 +345,6 @@ GRRLIB_texImg *GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]) {
my_texture->w = cinfo.output_width; my_texture->w = cinfo.output_width;
my_texture->h = cinfo.output_height; my_texture->h = cinfo.output_height;
my_texture->handlex = 0; my_texture->handley = 0;
my_texture->offsetx = 0; my_texture->offsety = 0;
my_texture->tiledtex = false;
GRRLIB_SetHandle( my_texture, 0, 0 ); GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture ); GRRLIB_FlushTex( my_texture );
return my_texture; return my_texture;
@ -405,7 +407,7 @@ GRRLIB_bytemapFont *GRRLIB_LoadBMF(const unsigned char my_bmf[]) {
short int sizeover, sizeunder, sizeinner, numcolpal; short int sizeover, sizeunder, sizeinner, numcolpal;
u16 nbPixels; u16 nbPixels;
if (my_bmf[0]==0xE1 && my_bmf[1]==0xE6 && my_bmf[2]==0xD5 && my_bmf[3]==0x1A) { if (fontArray != NULL && my_bmf[0]==0xE1 && my_bmf[1]==0xE6 && my_bmf[2]==0xD5 && my_bmf[3]==0x1A) {
fontArray->version = my_bmf[4]; fontArray->version = my_bmf[4];
lineheight = my_bmf[5]; lineheight = my_bmf[5];
sizeover = my_bmf[6]; sizeover = my_bmf[6];
@ -482,14 +484,12 @@ GRRLIB_texImg *GRRLIB_LoadTexture(const unsigned char my_img[]) {
*/ */
GRRLIB_texImg *GRRLIB_CreateEmptyTexture(unsigned int w, unsigned int h) { GRRLIB_texImg *GRRLIB_CreateEmptyTexture(unsigned int w, unsigned int h) {
unsigned int x, y; unsigned int x, y;
GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)malloc(sizeof(GRRLIB_texImg)); GRRLIB_texImg *my_texture = (struct GRRLIB_texImg *)calloc(1, sizeof(GRRLIB_texImg));
if(my_texture != NULL) {
my_texture->data = memalign(32, h * w * 4); my_texture->data = memalign(32, h * w * 4);
my_texture->w = w; my_texture->w = w;
my_texture->h = h; my_texture->h = h;
my_texture->handlex = 0; my_texture->handley = 0;
my_texture->offsetx = 0; my_texture->offsety = 0;
my_texture->tiledtex = false;
// Initialize the texture // Initialize the texture
for (y = 0; y < h; y++) { for (y = 0; y < h; y++) {
@ -499,6 +499,7 @@ GRRLIB_texImg *GRRLIB_CreateEmptyTexture(unsigned int w, unsigned int h) {
} }
GRRLIB_SetHandle( my_texture, 0, 0 ); GRRLIB_SetHandle( my_texture, 0, 0 );
GRRLIB_FlushTex( my_texture ); GRRLIB_FlushTex( my_texture );
}
return my_texture; return my_texture;
} }
@ -513,8 +514,7 @@ GRRLIB_texImg *GRRLIB_CreateEmptyTexture(unsigned int w, unsigned int h) {
* @param color Color in RGBA format. * @param color Color in RGBA format.
*/ */
inline void GRRLIB_DrawImg(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float degrees, float scaleX, f32 scaleY, u32 color) { inline void GRRLIB_DrawImg(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float degrees, float scaleX, f32 scaleY, u32 color) {
if (tex->data == NULL) { return; } if (tex == NULL || tex->data == NULL) { return; }
if (tex == NULL) { return; }
GXTexObj texObj; GXTexObj texObj;
u16 width, height; u16 width, height;
@ -571,8 +571,7 @@ inline void GRRLIB_DrawImg(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float
* @param color Color in RGBA format. * @param color Color in RGBA format.
*/ */
inline void GRRLIB_DrawImgQuad(Vector pos[4], struct GRRLIB_texImg *tex, u32 color) { inline void GRRLIB_DrawImgQuad(Vector pos[4], struct GRRLIB_texImg *tex, u32 color) {
if (tex->data == NULL) { return; } if (tex == NULL || tex->data == NULL) { return; }
if (tex == NULL) { return; }
GXTexObj texObj; GXTexObj texObj;
Mtx m, m1, m2, mv; Mtx m, m1, m2, mv;
@ -630,8 +629,7 @@ inline void GRRLIB_DrawImgQuad(Vector pos[4], struct GRRLIB_texImg *tex, u32 col
* @param frame Specifies the frame to draw. * @param frame Specifies the frame to draw.
*/ */
inline void GRRLIB_DrawTile(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float degrees, float scaleX, f32 scaleY, u32 color, int frame) { inline void GRRLIB_DrawTile(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float degrees, float scaleX, f32 scaleY, u32 color, int frame) {
if (tex->data == NULL) { return; } if (tex == NULL || tex->data == NULL) { return; }
if (tex == NULL) { return; }
GXTexObj texObj; GXTexObj texObj;
f32 width, height; f32 width, height;
@ -699,8 +697,7 @@ inline void GRRLIB_DrawTile(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, float
* @param ... Optional arguments. * @param ... Optional arguments.
*/ */
void GRRLIB_Printf(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, u32 color, f32 zoom, const char *text, ...) { void GRRLIB_Printf(f32 xpos, f32 ypos, struct GRRLIB_texImg *tex, u32 color, f32 zoom, const char *text, ...) {
if (tex->data == NULL) { return; } if (tex == NULL || tex->data == NULL) { return; }
if (tex == NULL) { return; }
int i, size; int i, size;
char tmp[1024]; char tmp[1024];
@ -959,10 +956,10 @@ void GRRLIB_BMFX_FlipV(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) {
* @param texdest The texture destination. * @param texdest The texture destination.
* @param factor The blur factor. * @param factor The blur factor.
*/ */
void GRRLIB_BMFX_Blur(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor) { void GRRLIB_BMFX_Blur(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor) {
int numba = (1+(factor<<1))*(1+(factor<<1)); int numba = (1+(factor<<1))*(1+(factor<<1));
u32 x, y; u32 x, y;
u32 k, l; s32 k, l;
int tmp = 0; int tmp = 0;
int newr, newg, newb, newa; int newr, newg, newb, newa;
u32 colours[numba]; u32 colours[numba];
@ -1013,7 +1010,7 @@ void GRRLIB_BMFX_Blur(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int
* @param texdest The texture destination. * @param texdest The texture destination.
* @param factor The factor level of the effect. * @param factor The factor level of the effect.
*/ */
void GRRLIB_BMFX_Pixelate(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor) { void GRRLIB_BMFX_Pixelate(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor) {
unsigned int x, y; unsigned int x, y;
unsigned int xx, yy; unsigned int xx, yy;
u32 rgb; u32 rgb;
@ -1037,7 +1034,7 @@ void GRRLIB_BMFX_Pixelate(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest,
* @param texdest The texture destination. * @param texdest The texture destination.
* @param factor The factor level of the effect. * @param factor The factor level of the effect.
*/ */
void GRRLIB_BMFX_Scatter(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor) { void GRRLIB_BMFX_Scatter(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor) {
unsigned int x, y; unsigned int x, y;
u32 val1, val2; u32 val1, val2;
u32 val3, val4; u32 val3, val4;
@ -1048,7 +1045,7 @@ void GRRLIB_BMFX_Scatter(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, i
val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor;
val2 = y + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; val2 = y + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor;
if ((val1 >= texsrc->w) || (val1 < 0) || (val2 >= texsrc->h) || (val2 < 0)) { if ((val1 >= texsrc->w) || (val2 >= texsrc->h)) {
} }
else { else {
val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc); val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc);

View file

@ -25,7 +25,7 @@
*/ */
#define GRRLIB_BLEND_ALPHA 0 /**< Alpha Blending. */ #define GRRLIB_BLEND_ALPHA 0 /**< Alpha Blending. */
#define GRRLIB_BLEND_ADD 1 /**< Additive Blending. */ #define GRRLIB_BLEND_ADD 1 /**< Additive Blending. */
#define GRRLIB_BLEND_SCREEN 2 /**< Alpha Light Blending */ #define GRRLIB_BLEND_SCREEN 2 /**< Alpha Light Blending. */
#define GRRLIB_BLEND_MULTI 3 /**< Multiply Blending. */ #define GRRLIB_BLEND_MULTI 3 /**< Multiply Blending. */
#define GRRLIB_BLEND_INV 4 /**< Invert Color Blending. */ #define GRRLIB_BLEND_INV 4 /**< Invert Color Blending. */
#define GRRLIB_BLEND_NONE GRRLIB_BLEND_ALPHA #define GRRLIB_BLEND_NONE GRRLIB_BLEND_ALPHA
@ -141,9 +141,9 @@ void GRRLIB_BMFX_Grayscale(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest)
void GRRLIB_BMFX_Invert(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest); void GRRLIB_BMFX_Invert(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest);
void GRRLIB_BMFX_FlipH(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest); void GRRLIB_BMFX_FlipH(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest);
void GRRLIB_BMFX_FlipV(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest); void GRRLIB_BMFX_FlipV(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest);
void GRRLIB_BMFX_Blur(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor); void GRRLIB_BMFX_Blur(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor);
void GRRLIB_BMFX_Scatter(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor); void GRRLIB_BMFX_Scatter(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor);
void GRRLIB_BMFX_Pixelate(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, int factor); void GRRLIB_BMFX_Pixelate(struct GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, u32 factor);
void GRRLIB_GXEngine(Vector v[], u32 color[], long count, u8 fmt); void GRRLIB_GXEngine(Vector v[], u32 color[], long count, u8 fmt);