From 50ed76deb2c88d37e506c2bca11556899b6c69df Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 26 Dec 2022 12:02:05 -0600 Subject: [PATCH] Placate ASAN by replacing memcmp() with xmemcmp(). --- lib/env.c | 2 +- lib/lib.c | 2 +- lib/lib.h | 1 + lib/xwrap.c | 11 ++++++++ toys/other/hexedit.c | 2 +- toys/other/nbd_client.c | 2 +- toys/other/readelf.c | 4 +-- toys/other/taskset.c | 2 +- toys/posix/cpio.c | 2 +- toys/posix/file.c | 58 ++++++++++++++++++++--------------------- toys/posix/od.c | 2 +- toys/posix/ps.c | 2 +- toys/posix/tar.c | 2 +- 13 files changed, 52 insertions(+), 40 deletions(-) diff --git a/lib/env.c b/lib/env.c index facde63f..7bcf5103 100644 --- a/lib/env.c +++ b/lib/env.c @@ -61,7 +61,7 @@ char *xsetenv(char *name, char *val) for (i = 0; environ[i]; i++) { // Drop old entry, freeing as appropriate. Assumes no duplicates. - if (!memcmp(name, environ[i], len) && environ[i][len]=='=') { + if (!xmemcmp(name, environ[i], len) && environ[i][len]=='=') { if (i= 0; pos--) { - if (!memcmp(TT.data+pos, TT.search, len)) { + if (!xmemcmp(TT.data+pos, TT.search, len)) { TT.pos = pos; return; } diff --git a/toys/other/nbd_client.c b/toys/other/nbd_client.c index ad3440af..262bc0ce 100644 --- a/toys/other/nbd_client.c +++ b/toys/other/nbd_client.c @@ -59,7 +59,7 @@ void nbd_client_main(void) // Read login data xreadall(sock, toybuf, 152); - if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16)) + if (xmemcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16)) error_exit("bad login %s:%s", host, port); devsize = SWAP_BE64(*(uint64_t *)(toybuf+16)); flags = SWAP_BE32(*(int *)(toybuf+24)); diff --git a/toys/other/readelf.c b/toys/other/readelf.c index 29e27244..6d01190b 100644 --- a/toys/other/readelf.c +++ b/toys/other/readelf.c @@ -355,7 +355,7 @@ static void scan_elf() char *hdr = TT.elf; int type, machine, version, flags, entry, ehsize, phnum, shstrndx, i, j, w; - if (TT.size < 45 || memcmp(hdr, "\177ELF", 4)) + if (TT.size < 45 || xmemcmp(hdr, "\177ELF", 4)) return error_msg("%s: not ELF", TT.f); TT.bits = hdr[4] - 1; @@ -453,7 +453,7 @@ static void scan_elf() s.entsize, sh_flags, s.link, s.info, s.addralign); } } - if (FLAG(S) && TT.shnum) + if (FLAG(S) && TT.shnum) printf("Key:\n (W)rite, (A)lloc, e(X)ecute, (M)erge, (S)trings, (I)nfo\n" " (L)ink order, (O)S, (G)roup, (T)LS, (C)ompressed, x=unknown\n"); diff --git a/toys/other/taskset.c b/toys/other/taskset.c index 8af7cd19..7ca26490 100644 --- a/toys/other/taskset.c +++ b/toys/other/taskset.c @@ -127,7 +127,7 @@ void nproc_main(void) char *ss; while ((de = readdir(dd))) { - if (memcmp(de->d_name, "cpu", 3)) continue; + if (xmemcmp(de->d_name, "cpu", 3)) continue; for (ss = de->d_name+3; isdigit(*ss); ss++); if (!*ss) nproc++; } diff --git a/toys/posix/cpio.c b/toys/posix/cpio.c index 5ecd56df..b5542000 100644 --- a/toys/posix/cpio.c +++ b/toys/posix/cpio.c @@ -132,7 +132,7 @@ void cpio_main(void) if (empty) error_exit("empty archive"); else break; } - if (size != 110 || memcmp(toybuf, "070701", 6)) error_exit("bad header"); + if (size != 110 || xmemcmp(toybuf, "070701", 6)) error_exit("bad header"); tofree = name = strpad(afd, x8u(toybuf+94), 110); if (!strcmp("TRAILER!!!", name)) { free(tofree); diff --git a/toys/posix/file.c b/toys/posix/file.c index e7b989b9..acfd28da 100644 --- a/toys/posix/file.c +++ b/toys/posix/file.c @@ -177,10 +177,10 @@ static void do_elf_file(int fd) goto bad; } - if (n_namesz==4 && !memcmp(note+12, "GNU", 4) && n_type==3) { + if (n_namesz==4 && !xmemcmp(note+12, "GNU", 4) && n_type==3) { printf(", BuildID="); for (j = 0; j= 4) { printf(", for Android %d", (int)elf_int(note+20, 4)); // NDK r14 and later also include NDK version info. OS binaries @@ -244,7 +244,7 @@ static void do_regular_file(int fd, char *name) s-3, (int)peek_le(s, 2), (int)peek_le(s+2, 2)); // TODO: parsing JPEG for width/height is harder than GIF or PNG. - else if (len>32 && !memcmp(s, "\xff\xd8", 2)) xputs("JPEG image data"); + else if (len>32 && !xmemcmp(s, "\xff\xd8", 2)) xputs("JPEG image data"); else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe")) { unsigned count = peek_be(s, 4), i, arch; @@ -307,7 +307,7 @@ static void do_regular_file(int fd, char *name) else if (len>31 && peek_be(s, 7) == 0xfd377a585a0000UL) xputs("xz compressed data"); else if (len>10 && strstart(&s, "\x1f\x8b")) xputs("gzip compressed data"); - else if (len>32 && !memcmp(s+1, "\xfa\xed\xfe", 3)) { + else if (len>32 && !xmemcmp(s+1, "\xfa\xed\xfe", 3)) { int bit = (*s==0xce) ? 32 : 64; char *what = 0; @@ -325,26 +325,26 @@ static void do_regular_file(int fd, char *name) else what = NULL; if (what) xprintf("%s\n", what); else xprintf("(bad type %d)\n", s[9]); - } else if (len>36 && !memcmp(s, "OggS\x00\x02", 6)) { + } else if (len>36 && !xmemcmp(s, "OggS\x00\x02", 6)) { xprintf("Ogg data"); // https://wiki.xiph.org/MIMETypesCodecs - if (!memcmp(s+28, "CELT ", 8)) xprintf(", celt audio"); - else if (!memcmp(s+28, "CMML ", 8)) xprintf(", cmml text"); - else if (!memcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video"); - else if (!memcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio"); - else if (!memcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video"); - else if (!memcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text"); - else if (!memcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text"); - else if (!memcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video"); - else if (!memcmp(s+28, "OpusHead", 8)) xprintf(", opus audio"); - else if (!memcmp(s+28, "PCM ", 8)) xprintf(", pcm audio"); - else if (!memcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video"); - else if (!memcmp(s+28, "Speex ", 8)) xprintf(", speex audio"); - else if (!memcmp(s+28, "\x80theora", 7)) xprintf(", theora video"); - else if (!memcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio"); - else if (!memcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video"); + if (!xmemcmp(s+28, "CELT ", 8)) xprintf(", celt audio"); + else if (!xmemcmp(s+28, "CMML ", 8)) xprintf(", cmml text"); + else if (!xmemcmp(s+28, "BBCD\0", 5)) xprintf(", dirac video"); + else if (!xmemcmp(s+28, "\177FLAC", 5)) xprintf(", flac audio"); + else if (!xmemcmp(s+28, "\x8bJNG\r\n\x1a\n", 8)) xprintf(", jng video"); + else if (!xmemcmp(s+28, "\x80kate\0\0\0", 8)) xprintf(", kate text"); + else if (!xmemcmp(s+28, "OggMIDI\0", 8)) xprintf(", midi text"); + else if (!xmemcmp(s+28, "\x8aMNG\r\n\x1a\n", 8)) xprintf(", mng video"); + else if (!xmemcmp(s+28, "OpusHead", 8)) xprintf(", opus audio"); + else if (!xmemcmp(s+28, "PCM ", 8)) xprintf(", pcm audio"); + else if (!xmemcmp(s+28, "\x89PNG\r\n\x1a\n", 8)) xprintf(", png video"); + else if (!xmemcmp(s+28, "Speex ", 8)) xprintf(", speex audio"); + else if (!xmemcmp(s+28, "\x80theora", 7)) xprintf(", theora video"); + else if (!xmemcmp(s+28, "\x01vorbis", 7)) xprintf(", vorbis audio"); + else if (!xmemcmp(s+28, "YUV4MPEG", 8)) xprintf(", yuv4mpeg video"); xputc('\n'); - } else if (len>32 && !memcmp(s, "RIF", 3) && !memcmp(s+8, "WAVEfmt ", 8)) { + } else if (len>32 && !xmemcmp(s, "RIF", 3) && !xmemcmp(s+8, "WAVEfmt ", 8)) { // https://en.wikipedia.org/wiki/WAV int le = (s[3] == 'F'); int format = le ? peek_le(s+20, 2) : peek_be(s+20, 2); @@ -372,7 +372,7 @@ static void do_regular_file(int fd, char *name) else xprintf("unknown format %d", format); xputc('\n'); } else if (len>12 && peek_be(s, 4)==0x10000) xputs("TrueType font"); - else if (len>12 && !memcmp(s, "ttcf\x00", 5)) { + else if (len>12 && !xmemcmp(s, "ttcf\x00", 5)) { xprintf("TrueType font collection, version %d, %d fonts\n", (int)peek_be(s+4, 2), (int)peek_be(s+8, 4)); @@ -382,14 +382,14 @@ static void do_regular_file(int fd, char *name) else if (strstart(&s,"-----BEGIN CERTIFICATE-----")) xputs("PEM certificate"); // https://msdn.microsoft.com/en-us/library/windows/desktop/ms680547(v=vs.85).aspx - else if (len>0x70 && !memcmp(s, "MZ", 2) && - (magic=peek_le(s+0x3c,4))0x70 && !xmemcmp(s, "MZ", 2) && + (magic=peek_le(s+0x3c,4))0x32 && !memcmp(s, "BM", 2) && !peek_be(s+6, 4)) { + } else if (len>0x32 && !xmemcmp(s, "BM", 2) && !peek_be(s+6, 4)) { xprintf("BMP image, %d x %d, %d bpp\n", (int)peek_le(s+18, 4), (int)peek_le(s+22,4), (int)peek_le(s+28, 2)); @@ -432,7 +432,7 @@ static void do_regular_file(int fd, char *name) (int)peek_le(s+12, 4), (int)peek_le(s+20, 4)); // https://android.googlesource.com/platform/system/tools/mkbootimg/+/refs/heads/master/include/bootimg/bootimg.h - } else if (len>1632 && !memcmp(s, "ANDROID!", 8)) { + } else if (len>1632 && !xmemcmp(s, "ANDROID!", 8)) { xprintf("Android boot image v%d\n", (int)peek_le(s+40, 4)); // https://source.android.com/devices/architecture/dto/partitions @@ -441,7 +441,7 @@ static void do_regular_file(int fd, char *name) (int)peek_be(s+16, 4)); // frameworks/base/core/java/com/android/internal/util/BinaryXmlSerializer.java - } else if (len>4 && !memcmp(s, "ABX", 3)) { + } else if (len>4 && !xmemcmp(s, "ABX", 3)) { xprintf("Android Binary XML v%d\n", s[3]); // Text files, including shell scripts. diff --git a/toys/posix/od.c b/toys/posix/od.c index ffeb0d66..a37186cb 100644 --- a/toys/posix/od.c +++ b/toys/posix/od.c @@ -133,7 +133,7 @@ static void od_outline(void) // Handle duplciate lines as * if (!FLAG(v) && TT.j != TT.pos && TT.leftover - && !memcmp(TT.bufs[0], TT.bufs[1], TT.w)) + && !xmemcmp(TT.bufs[0], TT.bufs[1], TT.w)) { if (!TT.star) { xputs("*"); diff --git a/toys/posix/ps.c b/toys/posix/ps.c index d6a3fce3..9033cdbd 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -845,7 +845,7 @@ static int get_ps(struct dirtree *new) off_t temp = 6; sprintf(buf, "%lld/exe", slot[SLOT_tid]); - if (readfileat(fd, buf, buf, &temp) && !memcmp(buf, "\177ELF", 4)) { + if (readfileat(fd, buf, buf, &temp) && !xmemcmp(buf, "\177ELF", 4)) { if (buf[4] == 1) slot[SLOT_bits] = 32; else if (buf[4] == 2) slot[SLOT_bits] = 64; } diff --git a/toys/posix/tar.c b/toys/posix/tar.c index 6f080689..aef1397b 100644 --- a/toys/posix/tar.c +++ b/toys/posix/tar.c @@ -990,7 +990,7 @@ void tar_main(void) if (len!=512 || !is_tar_header(hdr)) { // detect gzip and bzip signatures if (SWAP_BE16(*(short *)hdr)==0x1f8b) toys.optflags |= FLAG_z; - else if (!memcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j; + else if (!xmemcmp(hdr, "BZh", 3)) toys.optflags |= FLAG_j; else if (peek_be(hdr, 7) == 0xfd377a585a0000UL) toys.optflags |= FLAG_J; else error_exit("Not tar"); -- 2.39.2