* patch from Micah Heyer (GtkRadiant Mailinglist)

* fixes a segfault in gtkgensurf bitmap loading


git-svn-id: svn://svn.icculus.org/gtkradiant/GtkRadiant/trunk@326 8a3a26a2-13c4-0310-b231-cf6edde360e5
This commit is contained in:
mattn 2009-01-29 21:01:52 +00:00
parent 890656caab
commit 370c9bbf37
3 changed files with 75 additions and 75 deletions

View file

@ -77,24 +77,33 @@ void GenerateBitmapMapping ()
static unsigned char* OpenBitmapFile () static unsigned char* OpenBitmapFile ()
{ {
int bmWidth; #define INVALID_FORMAT do{\
int bmHeight; fprintf(stderr,"%s:%d: Error file '%s' is malformed.\n",__FILE__,__LINE__,gbmp.name);\
unsigned char bmPlanes; fclose(fp);\
unsigned char bmBitsPixel; return NULL;\
unsigned char m1,m2; }while(0);
unsigned long sizeimage;
short res1,res2; int32_t bmWidth;
long filesize, pixoff; int32_t bmHeight;
long bmisize, compression; uint16_t bmPlanes;
long xscale, yscale; uint16_t bmBitsPixel;
long colors, impcol; uint8_t m1,m2;
unsigned long m_bytesRead = 0; uint32_t sizeimage;
int16_t res1,res2;
int32_t filesize, pixoff;
int32_t bmisize, compression;
int32_t xscale, yscale;
int32_t colors, impcol;
uint32_t m_bytesRead = 0;
unsigned char *image; unsigned char *image;
FILE *fp; FILE *fp;
fp = fopen (gbmp.name, "rb"); fp = fopen (gbmp.name, "rb");
if (fp == NULL) if (fp == NULL)
{
fprintf(stderr,"Error: Invalid filename '%s'\n",gbmp.name);
return NULL; return NULL;
}
long rc; long rc;
rc = fread(&m1, 1, 1, fp); rc = fread(&m1, 1, 1, fp);
@ -108,62 +117,58 @@ static unsigned char* OpenBitmapFile ()
rc = fread(&m2, 1, 1, fp); rc = fread(&m2, 1, 1, fp);
m_bytesRead++; m_bytesRead++;
if ((m1 != 'B') || (m2 != 'M')) if ((m1 != 'B') || (m2 != 'M'))
{ INVALID_FORMAT;
fclose(fp);
return NULL;
}
rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(filesize),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2; rc = fread((uint16_t*)&(res1),2,1,fp); m_bytesRead+=2;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2; rc = fread((uint16_t*)&(res2),2,1,fp); m_bytesRead+=2;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(pixoff),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(bmisize),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long *)&(bmWidth),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t *)&(bmWidth),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(bmHeight),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2; rc = fread((uint16_t*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2; rc = fread((uint16_t*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(compression),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(sizeimage),4,1,fp); m_bytesRead+=4;
if (rc != 1) {fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(xscale),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(yscale),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(colors),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4; rc = fread((uint32_t*)&(impcol),4,1,fp); m_bytesRead+=4;
if (rc != 1) { fclose(fp); return NULL; } if (rc != 1) INVALID_FORMAT;
if (bmBitsPixel != 8) if (bmBitsPixel != 8)
{ {
g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.", g_FuncTable.m_pfnMessageBox (g_pWnd, "This is not an 8-bit image. GenSurf can't use it.",
"Bitmap", MB_ICONEXCLAMATION, NULL); "Bitmap", MB_ICONEXCLAMATION, NULL);
fclose(fp); INVALID_FORMAT;
return NULL;
} }
if (colors == 0) if (colors == 0)
@ -180,40 +185,35 @@ static unsigned char* OpenBitmapFile ()
m_bytesRead++; m_bytesRead++;
if (rc!=1) if (rc!=1)
{ {
fclose(fp); INVALID_FORMAT;
return NULL;
} }
rc = fread(&g, 1, 1, fp); rc = fread(&g, 1, 1, fp);
m_bytesRead++; m_bytesRead++;
if (rc!=1) if (rc!=1)
{ {
fclose(fp); INVALID_FORMAT;
return NULL;
} }
rc = fread(&r, 1, 1, fp); rc = fread(&r, 1, 1, fp);
m_bytesRead++; m_bytesRead++;
if (rc != 1) if (rc != 1)
{ {
fclose(fp); INVALID_FORMAT;
return NULL;
} }
rc = fread(&dummy, 1, 1, fp); rc = fread(&dummy, 1, 1, fp);
m_bytesRead++; m_bytesRead++;
if (rc != 1) if (rc != 1)
{ {
fclose(fp); INVALID_FORMAT;
return NULL;
} }
} }
} }
if ((long)m_bytesRead > pixoff) if ((long)m_bytesRead > pixoff)
{ {
fclose(fp); INVALID_FORMAT;
return NULL;
} }
while ((long)m_bytesRead < pixoff) while ((long)m_bytesRead < pixoff)
@ -231,8 +231,6 @@ static unsigned char* OpenBitmapFile ()
if (image != NULL) if (image != NULL)
{ {
gbmp.width = w;
gbmp.height = h;
unsigned char* outbuf = image; unsigned char* outbuf = image;
long row = 0; long row = 0;
long rowOffset = 0; long rowOffset = 0;
@ -261,8 +259,7 @@ static unsigned char* OpenBitmapFile ()
if (fread(&inbyte,1,1,fp) != 1) if (fread(&inbyte,1,1,fp) != 1)
{ {
free(image); free(image);
fclose(fp); INVALID_FORMAT;
return NULL;
} }
m_bytesRead++; m_bytesRead++;
} }
@ -283,8 +280,7 @@ static unsigned char* OpenBitmapFile ()
if (fread(&dummy,1,1,fp)!=1) if (fread(&dummy,1,1,fp)!=1)
{ {
free(image); free(image);
fclose(fp); INVALID_FORMAT;
return NULL;
} }
m_bytesRead++; m_bytesRead++;
} }
@ -402,18 +398,25 @@ static unsigned char* OpenBitmapFile ()
} }
} }
fclose(fp); fclose(fp);
gbmp.width = w;
gbmp.height = h;
if(gbmp.colors)
free(gbmp.colors);
gbmp.colors = image;
return image; return image;
} }
bool OpenBitmap () bool OpenBitmap ()
{ {
if (gbmp.colors)
free (gbmp.colors);
gbmp.colors = OpenBitmapFile (); OpenBitmapFile ();
if (!gbmp.colors) if (!gbmp.colors)
{ {
g_print("failed to load file gbmp %s\n",gbmp.name);
char Text[256]; char Text[256];
sprintf (Text, "Error opening %s", gbmp.name); sprintf (Text, "Error opening %s", gbmp.name);

View file

@ -894,11 +894,6 @@ static gint bitmap_file_entryfocusout(GtkWidget* widget, GdkEventFocus* event, g
strcpy (filename, gtk_entry_get_text (GTK_ENTRY(widget))); strcpy (filename, gtk_entry_get_text (GTK_ENTRY(widget)));
if(strcmp (filename,gbmp.name)) if(strcmp (filename,gbmp.name))
{ {
if (gbmp.colors)
{
free(gbmp.colors);
gbmp.colors=NULL;
}
strcpy (gbmp.name,filename); strcpy (gbmp.name,filename);
if (strlen(gbmp.name) ) if (strlen(gbmp.name) )
OpenBitmap (); OpenBitmap ();

View file

@ -133,11 +133,12 @@ bool GenSurfInit ()
else */ else */
Game = QUAKE3; Game = QUAKE3;
ReadIniFile (gszIni);
if (g_pWnd == NULL) if (g_pWnd == NULL)
g_pWnd = create_main_dialog (); g_pWnd = create_main_dialog ();
ReadIniFile (gszIni);
return true; return true;
} }
@ -291,9 +292,10 @@ void ReadIniFile (const char *file)
if(!strlen(Texture[GENESIS3D][0])) strcpy(Texture[GENESIS3D][0],"textures/rock13"); if(!strlen(Texture[GENESIS3D][0])) strcpy(Texture[GENESIS3D][0],"textures/rock13");
if(!strlen(Texture[QUAKE3][0])) strcpy(Texture[QUAKE3][0], "textures/organics/grass3"); if(!strlen(Texture[QUAKE3][0])) strcpy(Texture[QUAKE3][0], "textures/organics/grass3");
if(!strlen(Texture[QUAKE3][1])) strcpy(Texture[QUAKE3][1], "textures/common/caulk"); if(!strlen(Texture[QUAKE3][1])) strcpy(Texture[QUAKE3][1], "textures/common/caulk");
strcpy (gbmp.name, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","Filename","")); strcpy (gbmp.name, g_FuncTable.m_pfnProfileLoadString (file, "Bitmap","Filename",""));
gbmp.colors = NULL;
if (strlen(gbmp.name)) if (strlen(gbmp.name))
OpenBitmap (); OpenBitmap ();