psxbios : Add checks to index and rindex calls
[pcsx_rearmed.git] / libpcsxcore / psxbios.c
index 9341b0d..67a01e5 100644 (file)
@@ -610,7 +610,13 @@ void psxBios_strlen() { // 0x1b
 
 void psxBios_index() { // 0x1c
        char *p = (char *)Ra0;
-
+       if (a0 == 0)
+       {
+               v0 = 0;
+               pc0 = ra;
+               return;
+       }
+       
        do {
                if (*p == a1) {
                        v0 = a0 + (p - (char *)Ra0);
@@ -626,7 +632,11 @@ void psxBios_rindex() { // 0x1d
        char *p = (char *)Ra0;
 
        v0 = 0;
-
+       if (a0 == 0)
+       {
+               pc0 = ra;
+               return;
+       }
        do {
                if (*p == a1)
                        v0 = a0 + (p - (char *)Ra0);
@@ -1667,8 +1677,20 @@ void psxBios_OpenTh() { // 0e
        int th;
 
        for (th=1; th<8; th++)
+       {
                if (Thread[th].status == 0) break;
 
+       }
+       if (th == 8) {
+               // Feb 2019 - Added out-of-bounds fix caught by cppcheck:
+               // When no free TCB is found, return 0xffffffff according to Nocash doc.
+#ifdef PSXBIOS_LOG
+               PSXBIOS_LOG("\t%s() WARNING! No Free TCBs found!\n", __func__);
+#endif
+               v0 = 0xffffffff;
+               pc0 = ra;
+               return;
+       }
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s: %x\n", biosB0n[0x0e], th);
 #endif
@@ -1760,9 +1782,10 @@ void psxBios_StopPAD() { // 14
 #ifdef PSXBIOS_LOG
        PSXBIOS_LOG("psxBios_%s\n", biosB0n[0x14]);
 #endif
-
+       if (pad_buf == 0){
        pad_buf1 = NULL;
        pad_buf2 = NULL;
+       }
        pc0 = ra;
 }
 
@@ -2097,15 +2120,16 @@ void psxBios_firstfile() { // 42
                pfile = ffile+5;
                nfile = 1;
                if (!strncmp(pa0, "bu00", 4)) {
+                       // firstfile() calls _card_read() internally, so deliver it's event
+                       DeliverEvent(0x11, 0x2);
                        bufile(1);
                } else if (!strncmp(pa0, "bu10", 4)) {
+                       // firstfile() calls _card_read() internally, so deliver it's event
+                       DeliverEvent(0x11, 0x2);
                        bufile(2);
                }
        }
 
-       // firstfile() calls _card_read() internally, so deliver it's event
-       DeliverEvent(0x11, 0x2);
-
        pc0 = ra;
 }