Fixed a few code errors identified by cppcheck static code analyzer.

Fixed incorrect error handling of realloc function to prevent memory leak.
Fixed a few uninitialized local stack variables.
Changed (1 << 31) to (1u << 31) to fix error stating that a signed 32 bit integer shifted by 31 bits can result in undefined behavior.
This commit is contained in:
Matthew Budd 2020-05-08 23:30:31 -04:00
parent 10d57762b0
commit 70a804e90f
4 changed files with 18 additions and 14 deletions

View File

@ -133,7 +133,7 @@ void EMUFILE::write64le(u64 val)
size_t EMUFILE::read64le(u64 *Bufo) size_t EMUFILE::read64le(u64 *Bufo)
{ {
u64 buf; u64 buf=0;
if(fread((char*)&buf,8) != 8) if(fread((char*)&buf,8) != 8)
return 0; return 0;
#ifndef LOCAL_BE #ifndef LOCAL_BE
@ -174,7 +174,7 @@ size_t EMUFILE::read32le(s32* Bufo) { return read32le((u32*)Bufo); }
size_t EMUFILE::read32le(u32* Bufo) size_t EMUFILE::read32le(u32* Bufo)
{ {
u32 buf; u32 buf=0;
if(fread(&buf,4)<4) if(fread(&buf,4)<4)
return 0; return 0;
#ifndef LOCAL_BE #ifndef LOCAL_BE
@ -213,7 +213,7 @@ size_t EMUFILE::read16le(s16* Bufo) { return read16le((u16*)Bufo); }
size_t EMUFILE::read16le(u16* Bufo) size_t EMUFILE::read16le(u16* Bufo)
{ {
u32 buf; u32 buf=0;
if(fread(&buf,2)<2) if(fread(&buf,2)<2)
return 0; return 0;
#ifndef LOCAL_BE #ifndef LOCAL_BE

View File

@ -104,12 +104,14 @@ void ApplyIPS(FILE *ips, FCEUFILE* fp)
if((offset+size)>(uint32)fp->size) if((offset+size)>(uint32)fp->size)
{ {
// Probably a little slow. // Probably a little slow.
buf=(char *)realloc(buf,offset+size); char *newbuf=(char *)realloc(buf,offset+size);
if(!buf) if(!newbuf)
{ {
free(buf); buf=NULL;
FCEU_printf(" Oops. IPS patch %d(type RLE) goes beyond end of file. Could not allocate memory.\n",count); FCEU_printf(" Oops. IPS patch %d(type RLE) goes beyond end of file. Could not allocate memory.\n",count);
goto end; goto end;
} }
buf=newbuf;
memset(buf+fp->size,0,offset+size-fp->size); memset(buf+fp->size,0,offset+size-fp->size);
fp->size=offset+size; fp->size=offset+size;
} }
@ -127,12 +129,14 @@ void ApplyIPS(FILE *ips, FCEUFILE* fp)
if((offset+size)>(uint32)fp->size) if((offset+size)>(uint32)fp->size)
{ {
// Probably a little slow. // Probably a little slow.
buf=(char *)realloc(buf,offset+size); char *newbuf=(char *)realloc(buf,offset+size);
if(!buf) if(!newbuf)
{ {
free(buf); buf=NULL;
FCEU_printf(" Oops. IPS patch %d(type normal) goes beyond end of file. Could not allocate memory.\n",count); FCEU_printf(" Oops. IPS patch %d(type normal) goes beyond end of file. Could not allocate memory.\n",count);
goto end; goto end;
} }
buf=newbuf;
memset(buf+fp->size,0,offset+size-fp->size); memset(buf+fp->size,0,offset+size-fp->size);
} }
fread(buf+offset,1,size,ips); fread(buf+offset,1,size,ips);
@ -475,9 +479,9 @@ void FCEUI_SetDirOverride(int which, char *n)
va_list ap; va_list ap;
int ret; int ret;
va_start(ap,fmt);
if(!(*strp=(char*)FCEU_dmalloc(2048))) //mbg merge 7/17/06 cast to char* if(!(*strp=(char*)FCEU_dmalloc(2048))) //mbg merge 7/17/06 cast to char*
return(0); return(0);
va_start(ap,fmt);
ret=vsnprintf(*strp,2048,fmt,ap); ret=vsnprintf(*strp,2048,fmt,ap);
va_end(ap); va_end(ap);
return(ret); return(ret);

View File

@ -275,7 +275,7 @@ int write64le(uint64 b, EMUFILE* os)
int read32le(uint32 *Bufo, EMUFILE *fp) int read32le(uint32 *Bufo, EMUFILE *fp)
{ {
uint32 buf; uint32 buf=0;
if(fp->_fread(&buf,4)<4) if(fp->_fread(&buf,4)<4)
return 0; return 0;
#ifdef LOCAL_LE #ifdef LOCAL_LE
@ -288,7 +288,7 @@ int read32le(uint32 *Bufo, EMUFILE *fp)
int read16le(u16 *Bufo, EMUFILE *is) int read16le(u16 *Bufo, EMUFILE *is)
{ {
u16 buf; u16 buf=0;
if(is->_fread((char*)&buf,2) != 2) if(is->_fread((char*)&buf,2) != 2)
return 0; return 0;
#ifdef LOCAL_LE #ifdef LOCAL_LE
@ -301,7 +301,7 @@ int read16le(u16 *Bufo, EMUFILE *is)
int read64le(uint64 *Bufo, EMUFILE *is) int read64le(uint64 *Bufo, EMUFILE *is)
{ {
uint64 buf; uint64 buf=0;
if(is->_fread((char*)&buf,8) != 8) if(is->_fread((char*)&buf,8) != 8)
return 0; return 0;
#ifdef LOCAL_LE #ifdef LOCAL_LE

View File

@ -28,10 +28,10 @@ uint32 uppow2(uint32 n)
int x; int x;
for(x=31;x>=0;x--) for(x=31;x>=0;x--)
if(n&(1<<x)) if(n&(1u<<x))
{ {
if((1<<x)!=n) if((1u<<x)!=n)
return(1<<(x+1)); return(1u<<(x+1));
break; break;
} }
return n; return n;