From cfa5a2aff5202aadca7d19c76b61c80cec5b327c Mon Sep 17 00:00:00 2001 From: notaz Date: Mon, 28 Aug 2023 23:55:01 +0300 Subject: [PATCH] some missing error handling pointed out by gcc analyzer --- frontend/libpicofe | 2 +- frontend/main.c | 2 +- libpcsxcore/cheat.c | 3 ++- libpcsxcore/misc.c | 44 +++++++++++++++++++++++++------------------- libpcsxcore/ppf.c | 3 +++ libpcsxcore/sio.c | 2 ++ 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/frontend/libpicofe b/frontend/libpicofe index 7167e5f3..5dd225ec 160000 --- a/frontend/libpicofe +++ b/frontend/libpicofe @@ -1 +1 @@ -Subproject commit 7167e5f3376f0d0692ae102ed2df1ef5d2cc199a +Subproject commit 5dd225ecd6d5a04fd8e6f16c8f8ee65ee88c6fed diff --git a/frontend/main.c b/frontend/main.c index 1d008358..be93282e 100644 --- a/frontend/main.c +++ b/frontend/main.c @@ -736,7 +736,7 @@ static void toggle_fast_forward(int force_off) static void SignalExit(int sig) { // only to restore framebuffer/resolution on some devices plat_finish(); - exit(1); + _exit(1); } #endif diff --git a/libpcsxcore/cheat.c b/libpcsxcore/cheat.c index a016aeec..7e9dc240 100644 --- a/libpcsxcore/cheat.c +++ b/libpcsxcore/cheat.c @@ -340,7 +340,6 @@ int AddCheat(const char *descr, char *code) { } } - Cheats[NumCheats].Descr = strdup(descr[0] ? descr : _("(Untitled)")); Cheats[NumCheats].Enabled = 0; Cheats[NumCheats].WasEnabled = 0; Cheats[NumCheats].First = NumCodes; @@ -392,6 +391,7 @@ int AddCheat(const char *descr, char *code) { return -1; } + Cheats[NumCheats].Descr = strdup(descr[0] ? descr : _("(Untitled)")); NumCheats++; return 0; } @@ -400,6 +400,7 @@ void RemoveCheat(int index) { assert(index >= 0 && index < NumCheats); free(Cheats[index].Descr); + Cheats[index].Descr = NULL; while (index < NumCheats - 1) { Cheats[index] = Cheats[index + 1]; diff --git a/libpcsxcore/misc.c b/libpcsxcore/misc.c index 3d164710..702f690f 100644 --- a/libpcsxcore/misc.c +++ b/libpcsxcore/misc.c @@ -628,11 +628,12 @@ static const u32 SaveVersion = 0x8b410006; int SaveState(const char *file) { void *f; - GPUFreeze_t *gpufP; - SPUFreezeHdr_t *spufH; - SPUFreeze_t *spufP; + GPUFreeze_t *gpufP = NULL; + SPUFreezeHdr_t spufH; + SPUFreeze_t *spufP = NULL; + unsigned char *pMem = NULL; + int result = -1; int Size; - unsigned char *pMem; f = SaveFuncs.open(file, "wb"); if (f == NULL) return -1; @@ -644,7 +645,7 @@ int SaveState(const char *file) { SaveFuncs.write(f, (void *)&Config.HLE, sizeof(boolean)); pMem = (unsigned char *)malloc(128 * 96 * 3); - if (pMem == NULL) return -1; + if (pMem == NULL) goto cleanup; GPU_getScreenPic(pMem); SaveFuncs.write(f, pMem, 128 * 96 * 3); free(pMem); @@ -660,20 +661,20 @@ int SaveState(const char *file) { // gpu gpufP = (GPUFreeze_t *)malloc(sizeof(GPUFreeze_t)); + if (gpufP == NULL) goto cleanup; gpufP->ulFreezeVersion = 1; GPU_freeze(1, gpufP); SaveFuncs.write(f, gpufP, sizeof(GPUFreeze_t)); - free(gpufP); + free(gpufP); gpufP = NULL; // spu - spufH = malloc(sizeof(*spufH)); - SPU_freeze(2, (SPUFreeze_t *)spufH, psxRegs.cycle); - Size = spufH->Size; SaveFuncs.write(f, &Size, 4); - free(spufH); + SPU_freeze(2, (SPUFreeze_t *)&spufH, psxRegs.cycle); + Size = spufH.Size; SaveFuncs.write(f, &Size, 4); spufP = (SPUFreeze_t *) malloc(Size); + if (spufP == NULL) goto cleanup; SPU_freeze(1, spufP, psxRegs.cycle); SaveFuncs.write(f, spufP, Size); - free(spufP); + free(spufP); spufP = NULL; sioFreeze(f, 1); cdrFreeze(f, 1); @@ -682,19 +683,21 @@ int SaveState(const char *file) { mdecFreeze(f, 1); new_dyna_freeze(f, 1); + result = 0; +cleanup: SaveFuncs.close(f); - - return 0; + return result; } int LoadState(const char *file) { void *f; - GPUFreeze_t *gpufP; - SPUFreeze_t *spufP; + GPUFreeze_t *gpufP = NULL; + SPUFreeze_t *spufP = NULL; int Size; char header[32]; u32 version; boolean hle; + int result = -1; f = SaveFuncs.open(file, "rb"); if (f == NULL) return -1; @@ -704,8 +707,8 @@ int LoadState(const char *file) { SaveFuncs.read(f, &hle, sizeof(boolean)); if (strncmp("STv4 PCSX", header, 9) != 0 || version != SaveVersion) { - SaveFuncs.close(f); - return -1; + SysPrintf("incompatible savestate version %x\n", version); + goto cleanup; } Config.HLE = hle; @@ -726,6 +729,7 @@ int LoadState(const char *file) { // gpu gpufP = (GPUFreeze_t *)malloc(sizeof(GPUFreeze_t)); + if (gpufP == NULL) goto cleanup; SaveFuncs.read(f, gpufP, sizeof(GPUFreeze_t)); GPU_freeze(0, gpufP); free(gpufP); @@ -735,6 +739,7 @@ int LoadState(const char *file) { // spu SaveFuncs.read(f, &Size, 4); spufP = (SPUFreeze_t *)malloc(Size); + if (spufP == NULL) goto cleanup; SaveFuncs.read(f, spufP, Size); SPU_freeze(0, spufP, psxRegs.cycle); free(spufP); @@ -746,9 +751,10 @@ int LoadState(const char *file) { mdecFreeze(f, 0); new_dyna_freeze(f, 0); + result = 0; +cleanup: SaveFuncs.close(f); - - return 0; + return result; } int CheckState(const char *file) { diff --git a/libpcsxcore/ppf.c b/libpcsxcore/ppf.c index 454290d0..2ce1a9d9 100644 --- a/libpcsxcore/ppf.c +++ b/libpcsxcore/ppf.c @@ -58,6 +58,7 @@ static void FillPPFCache() { if (iPPFNum <= 0) return; pc = ppfCache = (PPF_CACHE *)malloc(iPPFNum * sizeof(PPF_CACHE)); + if (pc == NULL) return; iPPFNum--; p = ppfHead; @@ -133,6 +134,7 @@ void CheckPPFCache(unsigned char *pB, unsigned char m, unsigned char s, unsigned static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) { if (ppfHead == NULL) { ppfHead = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz); + if (ppfHead == NULL) return; ppfHead->addr = ladr; ppfHead->pNext = NULL; ppfHead->pos = pos; @@ -164,6 +166,7 @@ static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) { } padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz); + if (padd == NULL) return; padd->addr = ladr; padd->pNext = p; padd->pos = pos; diff --git a/libpcsxcore/sio.c b/libpcsxcore/sio.c index 6478338d..7aa669bf 100644 --- a/libpcsxcore/sio.c +++ b/libpcsxcore/sio.c @@ -678,6 +678,7 @@ void ConvertMcd(char *mcd, char *data) { fclose(f); } f = fopen(mcd, "r+"); + if (f == NULL) return; s = s + 3904; fputc('1', f); s--; fputc('2', f); s--; @@ -712,6 +713,7 @@ void ConvertMcd(char *mcd, char *data) { fclose(f); } f = fopen(mcd, "r+"); + if (f == NULL) return; s = s + 64; fputc('V', f); s--; fputc('g', f); s--; -- 2.39.2