Skip to content

Commit e247449

Browse files
committed
test_memory_map: guard descriptors pointer before indexing [0]
Copilot: avoid UB if num_descriptors >= 1 but descriptors is NULL. Test 3 fails explicitly when num_descriptors==1 with NULL table; Tests 4–9 FAIL as a block in that case instead of crashing. Made-with: Cursor
1 parent 67826ec commit e247449

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

test/tools/test_memory_map.c

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,34 @@ int main(int argc, char **argv)
237237
/* Test 3: Memory map has exactly 1 descriptor */
238238
printf("Test 3: num_descriptors == 1 ... ");
239239
if (captured_memmap && captured_memmap->num_descriptors == 1)
240-
printf("PASS\n");
240+
{
241+
if (captured_memmap->descriptors != NULL)
242+
printf("PASS\n");
243+
else
244+
{
245+
printf("FAIL (descriptors is NULL)\n");
246+
failures++;
247+
}
248+
}
241249
else
242250
{
243251
printf("FAIL (got %u)\n",
244252
captured_memmap ? (unsigned)captured_memmap->num_descriptors : 0);
245253
failures++;
246254
}
247255

248-
/* Test 4: Descriptor covers 2 MB system RAM at address 0 */
249-
if (captured_memmap && captured_memmap->num_descriptors >= 1)
256+
/* Tests 4–9: descriptor [0] (requires non-NULL descriptors table) */
257+
if (!captured_memmap || captured_memmap->num_descriptors < 1)
258+
{
259+
printf("Tests 4-9: SKIPPED (no descriptor)\n");
260+
failures += 6;
261+
}
262+
else if (!captured_memmap->descriptors)
263+
{
264+
printf("Tests 4-9: FAIL (descriptors is NULL)\n");
265+
failures += 6;
266+
}
267+
else
250268
{
251269
const struct retro_memory_descriptor *d = &captured_memmap->descriptors[0];
252270

@@ -307,11 +325,6 @@ int main(int argc, char **argv)
307325
}
308326
}
309327
}
310-
else
311-
{
312-
printf("Tests 4-9: SKIPPED (no descriptor)\n");
313-
failures += 6;
314-
}
315328

316329
/* Test 10: retro_get_memory_size(SYSTEM_RAM) == 0x200000 */
317330
printf("Test 10: retro_get_memory_size(SYSTEM_RAM) == 0x200000 ... ");

0 commit comments

Comments
 (0)