aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2024-06-19 10:00:39 +0100
committerRon Yorston <rmy@pobox.com>2024-06-19 10:00:39 +0100
commit0914f116c06d4724f25e23b685279024f809f434 (patch)
treeeb6273f5a1f4f3f0fca111c2834ec7d7b247cce9
parent80b1e4e59d0c36ef0988570d6f32f560c1b7e0ff (diff)
downloadbusybox-w32-0914f116c06d4724f25e23b685279024f809f434.tar.gz
busybox-w32-0914f116c06d4724f25e23b685279024f809f434.tar.bz2
busybox-w32-0914f116c06d4724f25e23b685279024f809f434.zip
win32: code shrink APE detection; avoid UB
Detecting Actually Portable Executable binaries used a longer signature than seems necessary. Six characters should be enough for anyone. When right shifting a byte by 24 bits, cast it to unsigned to avoid undefined behaviour. Saves 24-32 bytes. (GitHub issue #424)
-rw-r--r--win32/mingw.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/win32/mingw.c b/win32/mingw.c
index 26b046f1a..98254fdbe 100644
--- a/win32/mingw.c
+++ b/win32/mingw.c
@@ -473,16 +473,20 @@ static int has_exec_format(const char *name)
473 * the magic from the file command. 473 * the magic from the file command.
474 */ 474 */
475 if (buf[0] == 'M' && buf[1] == 'Z') { 475 if (buf[0] == 'M' && buf[1] == 'Z') {
476/* Convert four unsigned bytes to an unsigned int (little-endian) */
477#define LE4(b, o) (((unsigned)b[o+3] << 24) + (b[o+2] << 16) + \
478 (b[o+1] << 8) + b[o])
479
476 /* Actually Portable Executable */ 480 /* Actually Portable Executable */
477 /* See ape/ape.S at https://github.com/jart/cosmopolitan */ 481 /* See ape/ape.S at https://github.com/jart/cosmopolitan */
478 if (n > 9 && memcmp(buf + 2, "qFpD='\n", 7) == 0) 482 const unsigned char *qFpD = (unsigned char *)"qFpD";
483 if (n > 6 && LE4(buf, 2) == LE4(qFpD, 0))
479 return 1; 484 return 1;
480 485
481 if (n > 0x3f) { 486 if (n > 0x3f) {
482 offset = (buf[0x19] << 8) + buf[0x18]; 487 offset = (buf[0x19] << 8) + buf[0x18];
483 if (offset > 0x3f) { 488 if (offset > 0x3f) {
484 offset = (buf[0x3f] << 24) + (buf[0x3e] << 16) + 489 offset = LE4(buf, 0x3c);
485 (buf[0x3d] << 8) + buf[0x3c];
486 if (offset < sizeof(buf)-100) { 490 if (offset < sizeof(buf)-100) {
487 if (memcmp(buf+offset, "PE\0\0", 4) == 0) { 491 if (memcmp(buf+offset, "PE\0\0", 4) == 0) {
488 sig = (buf[offset+25] << 8) + buf[offset+24]; 492 sig = (buf[offset+25] << 8) + buf[offset+24];