Skip to content

Commit

Permalink
lx_dump: Don't fail for extremely small (but valid) binaries.
Browse files Browse the repository at this point in the history
Although strictly speaking, it looks like the final OS/2 releases
will fail if the OS/2 .EXE is less than ~320 bytes, choosing to
launch it as a DOS program instead (giving you the "This is an
OS/2 executable" message from the DOS stub, comically).
  • Loading branch information
icculus committed Jun 6, 2018
1 parent 4cbda85 commit bdc2857
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions lx_dump.c
Expand Up @@ -15,8 +15,13 @@

// !!! FIXME: some cut-and-paste with lx_loader.c ...

static int sanityCheckLxExe(const uint8 *exe)
static int sanityCheckLxExe(const uint8 *exe, const uint32 exelen)
{
if (sizeof (LxHeader) >= exelen) {
fprintf(stderr, "not an OS/2 EXE\n");
return 0;
}

const LxHeader *lx = (const LxHeader *) exe;
if ((lx->byte_order != 0) || (lx->word_order != 0)) {
fprintf(stderr, "Program is not little-endian!\n");
Expand Down Expand Up @@ -46,8 +51,13 @@ static int sanityCheckLxExe(const uint8 *exe)
return 1;
} // sanityCheckLxExe

static int sanityCheckNeExe(const uint8 *exe)
static int sanityCheckNeExe(const uint8 *exe, const uint32 exelen)
{
if (sizeof (NeHeader) >= exelen) {
fprintf(stderr, "not an OS/2 EXE\n");
return 0;
}

const NeHeader *ne = (const NeHeader *) exe;
if (ne->exe_type != 1) {
fprintf(stderr, "Not an OS/2 NE EXE file (exe_type is %d, not 1)\n", (int) ne->exe_type);
Expand All @@ -59,16 +69,12 @@ static int sanityCheckNeExe(const uint8 *exe)

static int sanityCheckExe(uint8 **_exe, uint32 *_exelen, int *_is_lx)
{
if (*_exelen < 196) {
if (*_exelen < 62) {
fprintf(stderr, "not an OS/2 EXE\n");
return 0;
}
const uint32 header_offset = *((uint32 *) (*_exe + 0x3C));
//printf("header offset is %u\n", (uint) header_offset);
if ((header_offset + sizeof (LxHeader)) >= *_exelen) {
fprintf(stderr, "not an OS/2 EXE\n");
return 0;
}

*_exe += header_offset; // skip the DOS stub, etc.
*_exelen -= header_offset;
Expand All @@ -77,10 +83,10 @@ static int sanityCheckExe(uint8 **_exe, uint32 *_exelen, int *_is_lx)

if ((magic[0] == 'L') && (magic[1] == 'X')) {
*_is_lx = 1;
return sanityCheckLxExe(*_exe);
return sanityCheckLxExe(*_exe, *_exelen);
} else if ((magic[0] == 'N') && (magic[1] == 'E')) {
*_is_lx = 0;
return sanityCheckNeExe(*_exe);
return sanityCheckNeExe(*_exe, *_exelen);
}

fprintf(stderr, "not an OS/2 EXE\n");
Expand Down

0 comments on commit bdc2857

Please sign in to comment.