Message-Id: <199907091809.SAA63418@out5.ibm.net> From: "Mark E." To: djgpp-workers AT delorie DOT com Date: Fri, 9 Jul 1999 14:09:47 -0400 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: patch for chkv2prg.c X-mailer: Pegasus Mail for Win32 (v3.11) Reply-To: djgpp-workers AT delorie DOT com Hello list, _check_v2_prog will leak a file handle in the rare case when an executable is corrupt and the function was given a filename. One way to fix the problem is provided in the patch below. *** src/libc/dos/process/chkv2prg.c.orig Thu Oct 9 18:40:02 1997 --- src/libc/dos/process/chkv2prg.c Fri Jul 9 13:59:34 1999 *************** *** 1,3 **** --- 1,4 ---- + /* Copyright (C) 1999 DJ Delorie, see COPYING.DJ for details */ /* Copyright (C) 1997 DJ Delorie, see COPYING.DJ for details */ #include #include *************** *** 11,19 **** static _v2_prog_type type; static int type_initialized = 0; const _v2_prog_type *_check_v2_prog(const char *program, int pf) { ! unsigned short header[5]; if (type_initialized && type.stubinfo) free(type.stubinfo); --- 12,23 ---- static _v2_prog_type type; static int type_initialized = 0; + static + const _v2_prog_type *_check_v2_prog_internal (int pf); + const _v2_prog_type *_check_v2_prog(const char *program, int pf) { ! const _v2_prog_type *prog_type; if (type_initialized && type.stubinfo) free(type.stubinfo); *************** const _v2_prog_type *_check_v2_prog(cons *** 28,36 **** return &type; } lseek(pf, 0, SEEK_SET); if (read(pf, header, sizeof(header)) != sizeof(header)) ! return &type; if (header[0] == 0x010b || header[0] == 0x014c) { unsigned char firstbytes[1]; --- 32,54 ---- return &type; } + prog_type = _check_v2_prog_internal(pf); + + if (program) + close(pf); + + if (prog_type) + type.valid = 1; + return &type; + } + + static + const _v2_prog_type *_check_v2_prog_internal (int pf) + { + unsigned short header[5]; lseek(pf, 0, SEEK_SET); if (read(pf, header, sizeof(header)) != sizeof(header)) ! return NULL; if (header[0] == 0x010b || header[0] == 0x014c) { unsigned char firstbytes[1]; *************** const _v2_prog_type *_check_v2_prog(cons *** 64,72 **** coff_start += (long)header[1] - 512L; exe_start = (unsigned long)header[4]*16L; if (lseek(pf, exe_start, SEEK_SET) != exe_start) ! return &type; if (read(pf, go32stub, 8) != 8) ! return &type; go32stub[8] = 0; if (strcmp(go32stub, "go32stub") == 0) { --- 82,90 ---- coff_start += (long)header[1] - 512L; exe_start = (unsigned long)header[4]*16L; if (lseek(pf, exe_start, SEEK_SET) != exe_start) ! return NULL; if (read(pf, go32stub, 8) != 8) ! return NULL; go32stub[8] = 0; if (strcmp(go32stub, "go32stub") == 0) { *************** const _v2_prog_type *_check_v2_prog(cons *** 82,111 **** unsigned short coff_id; type.version.v.major = 1; if (lseek(pf, coff_start - 4, SEEK_SET) != coff_start-4) ! return &type; if (read(pf, &stub_offset, 4) != 4) ! return &type; if (read(pf, &coff_id, 2) != 2) ! return &type; if (coff_id == 0x010b || coff_id == 0x014c) { type.object_format = _V2_OBJECT_FORMAT_COFF; type.exec_format = _V2_EXEC_FORMAT_STUBCOFF; } if (lseek(pf, stub_offset, 0) != stub_offset) ! return &type; if (read(pf, magic, 16) != 16) ! return &type; if (memcmp(STUB_INFO_MAGIC, magic, 16) == 0) { if (read(pf, &struct_length, 4) != 4) ! return &type; type.stubinfo = (_v1_stubinfo *)malloc(struct_length); memcpy(type.stubinfo->magic, magic, 16); type.stubinfo->struct_length = struct_length; if (read(pf, type.stubinfo->go32, struct_length - 20) != struct_length - 20) ! return &type; type.has_stubinfo = 1; } } --- 100,129 ---- unsigned short coff_id; type.version.v.major = 1; if (lseek(pf, coff_start - 4, SEEK_SET) != coff_start-4) ! return NULL; if (read(pf, &stub_offset, 4) != 4) ! return NULL; if (read(pf, &coff_id, 2) != 2) ! return NULL; if (coff_id == 0x010b || coff_id == 0x014c) { type.object_format = _V2_OBJECT_FORMAT_COFF; type.exec_format = _V2_EXEC_FORMAT_STUBCOFF; } if (lseek(pf, stub_offset, 0) != stub_offset) ! return NULL; if (read(pf, magic, 16) != 16) ! return NULL; if (memcmp(STUB_INFO_MAGIC, magic, 16) == 0) { if (read(pf, &struct_length, 4) != 4) ! return NULL; type.stubinfo = (_v1_stubinfo *)malloc(struct_length); memcpy(type.stubinfo->magic, magic, 16); type.stubinfo->struct_length = struct_length; if (read(pf, type.stubinfo->go32, struct_length - 20) != struct_length - 20) ! return NULL; type.has_stubinfo = 1; } } *************** const _v2_prog_type *_check_v2_prog(cons *** 114,125 **** { type.exec_format = _V2_EXEC_FORMAT_UNIXSCRIPT; } - - if (program) - close(pf); - - type.valid = 1; return &type; } - --- 132,137 ---- --- Mark Elbrecht, snowball3 AT bigfoot DOT com http://snowball.frogspace.net/