From a4b633ae862ff870eb4f4c73672829b3cf0873c5 Mon Sep 17 00:00:00 2001 From: Hermes Agent Date: Thu, 28 May 2026 15:53:51 +0200 Subject: [PATCH] Fix crash in detectPlatform: handle negative e_lfanew offset For pure DOS executables, the e_lfanew field at MZ header offset 0x3C is uninitialized garbage. Duke Nukem 2's EXE had bytes that form a negative int32 (-1878619136), which was used as an array index causing ArrayIndexOutOfBoundsException. Now checks peOffset < 0 and returns 'dos' (no Windows signature found). --- src/main/java/com/dostalgia/UploadResource.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/dostalgia/UploadResource.java b/src/main/java/com/dostalgia/UploadResource.java index 71ae61d..758ff5c 100644 --- a/src/main/java/com/dostalgia/UploadResource.java +++ b/src/main/java/com/dostalgia/UploadResource.java @@ -468,14 +468,17 @@ public class UploadResource { // Must start with MZ if (buf[0] != 0x4D || buf[1] != 0x5A) return null; - // Read e_lfanew at offset 0x3C + // Read e_lfanew at offset 0x3C (only meaningful for PE/NE — garbage for pure DOS) if (read < 0x40) return "dos"; // too short for PE/NE, assume DOS int peOffset = (buf[0x3C] & 0xFF) | ((buf[0x3D] & 0xFF) << 8) | ((buf[0x3E] & 0xFF) << 16) | ((buf[0x3F] & 0xFF) << 24); - // If signature is beyond our buffer, read it + // e_lfanew is garbage for pure DOS executables — may be negative or absurd + if (peOffset < 0) return "dos"; + + // If signature is beyond our buffer, read it from the file byte sig1, sig2; if (peOffset + 2 <= read) { sig1 = buf[peOffset];