From 70a804e90f24b043d8ce8eafd792e9bc61a21f10 Mon Sep 17 00:00:00 2001 From: Matthew Budd Date: Fri, 8 May 2020 23:30:31 -0400 Subject: [PATCH] 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. --- src/emufile.cpp | 6 +++--- src/file.cpp | 14 +++++++++----- src/utils/endian.cpp | 6 +++--- src/utils/general.cpp | 6 +++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/emufile.cpp b/src/emufile.cpp index 162e8df0..e0e83115 100644 --- a/src/emufile.cpp +++ b/src/emufile.cpp @@ -133,7 +133,7 @@ void EMUFILE::write64le(u64 val) size_t EMUFILE::read64le(u64 *Bufo) { - u64 buf; + u64 buf=0; if(fread((char*)&buf,8) != 8) return 0; #ifndef LOCAL_BE @@ -174,7 +174,7 @@ size_t EMUFILE::read32le(s32* Bufo) { return read32le((u32*)Bufo); } size_t EMUFILE::read32le(u32* Bufo) { - u32 buf; + u32 buf=0; if(fread(&buf,4)<4) return 0; #ifndef LOCAL_BE @@ -213,7 +213,7 @@ size_t EMUFILE::read16le(s16* Bufo) { return read16le((u16*)Bufo); } size_t EMUFILE::read16le(u16* Bufo) { - u32 buf; + u32 buf=0; if(fread(&buf,2)<2) return 0; #ifndef LOCAL_BE diff --git a/src/file.cpp b/src/file.cpp index 9860d87e..dfbba02e 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -104,12 +104,14 @@ void ApplyIPS(FILE *ips, FCEUFILE* fp) if((offset+size)>(uint32)fp->size) { // Probably a little slow. - buf=(char *)realloc(buf,offset+size); - if(!buf) + char *newbuf=(char *)realloc(buf,offset+size); + 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); goto end; } + buf=newbuf; memset(buf+fp->size,0,offset+size-fp->size); fp->size=offset+size; } @@ -127,12 +129,14 @@ void ApplyIPS(FILE *ips, FCEUFILE* fp) if((offset+size)>(uint32)fp->size) { // Probably a little slow. - buf=(char *)realloc(buf,offset+size); - if(!buf) + char *newbuf=(char *)realloc(buf,offset+size); + 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); goto end; } + buf=newbuf; memset(buf+fp->size,0,offset+size-fp->size); } fread(buf+offset,1,size,ips); @@ -475,9 +479,9 @@ void FCEUI_SetDirOverride(int which, char *n) va_list ap; int ret; - va_start(ap,fmt); if(!(*strp=(char*)FCEU_dmalloc(2048))) //mbg merge 7/17/06 cast to char* return(0); + va_start(ap,fmt); ret=vsnprintf(*strp,2048,fmt,ap); va_end(ap); return(ret); diff --git a/src/utils/endian.cpp b/src/utils/endian.cpp index 71732ff1..56704d41 100644 --- a/src/utils/endian.cpp +++ b/src/utils/endian.cpp @@ -275,7 +275,7 @@ int write64le(uint64 b, EMUFILE* os) int read32le(uint32 *Bufo, EMUFILE *fp) { - uint32 buf; + uint32 buf=0; if(fp->_fread(&buf,4)<4) return 0; #ifdef LOCAL_LE @@ -288,7 +288,7 @@ int read32le(uint32 *Bufo, EMUFILE *fp) int read16le(u16 *Bufo, EMUFILE *is) { - u16 buf; + u16 buf=0; if(is->_fread((char*)&buf,2) != 2) return 0; #ifdef LOCAL_LE @@ -301,7 +301,7 @@ int read16le(u16 *Bufo, EMUFILE *is) int read64le(uint64 *Bufo, EMUFILE *is) { - uint64 buf; + uint64 buf=0; if(is->_fread((char*)&buf,8) != 8) return 0; #ifdef LOCAL_LE diff --git a/src/utils/general.cpp b/src/utils/general.cpp index c84d9e70..9f49a19b 100644 --- a/src/utils/general.cpp +++ b/src/utils/general.cpp @@ -28,10 +28,10 @@ uint32 uppow2(uint32 n) int x; for(x=31;x>=0;x--) - if(n&(1<