diff options
-rw-r--r-- | src/file.h | 3 | ||||
-rw-r--r-- | src/funcs.c | 13 | ||||
-rw-r--r-- | src/readelf.c | 19 |
3 files changed, 26 insertions, 9 deletions
@@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.211 2020/02/16 15:52:49 christos Exp $ + * @(#)$File: file.h,v 1.212 2020/02/20 15:50:20 christos Exp $ */ #ifndef __file_h__ @@ -467,6 +467,7 @@ protected int file_pipe2file(struct magic_set *, int, const void *, size_t); protected int file_vprintf(struct magic_set *, const char *, va_list) __attribute__((__format__(__printf__, 2, 0))); protected int file_separator(struct magic_set *); +protected char *file_copystr(char *, size_t, size_t, const char *); protected int file_checkfmt(char *, size_t, const char *); protected size_t file_printedlen(const struct magic_set *); protected int file_print_guid(char *, size_t, const uint64_t *); diff --git a/src/funcs.c b/src/funcs.c index 5d159cb8..09b965e9 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.114 2020/02/20 01:03:49 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.115 2020/02/20 15:50:20 christos Exp $") #endif /* lint */ #include "magic.h" @@ -48,6 +48,15 @@ FILE_RCSID("@(#)$File: funcs.c,v 1.114 2020/02/20 01:03:49 christos Exp $") #define SIZE_MAX ((size_t)~0) #endif +protected char * +file_copystr(char *buf, size_t blen, size_t width, const char *str) +{ + if (++width > blen) + width = blen; + strlcpy(buf, str, width); + return buf; +} + private void file_clearbuf(struct magic_set *ms) { @@ -56,7 +65,7 @@ file_clearbuf(struct magic_set *ms) ms->o.blen = 0; } -static int +private int file_checkfield(char *msg, size_t mlen, const char *what, const char **pp) { const char *p = *pp; diff --git a/src/readelf.c b/src/readelf.c index c1eddcc8..9d1d1ad5 100644 --- a/src/readelf.c +++ b/src/readelf.c @@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readelf.c,v 1.169 2019/12/17 15:27:27 christos Exp $") +FILE_RCSID("@(#)$File: readelf.c,v 1.170 2020/02/20 15:50:20 christos Exp $") #endif #ifdef BUILTIN_ELF @@ -568,8 +568,10 @@ do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, } if (namesz == 4 && strcmp(RCAST(char *, &nbuf[noff]), "Go") == 0 && type == NT_GO_BUILD_ID && descsz < 128) { - if (file_printf(ms, ", Go BuildID=%.*s", - CAST(int, descsz), RCAST(char *, &nbuf[doff])) == -1) + char buf[256]; + if (file_printf(ms, ", Go BuildID=%s", + file_copystr(buf, sizeof(buf), descsz, + RCAST(const char *, &nbuf[doff]))) == -1) return -1; return 1; } @@ -721,6 +723,7 @@ do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, size_t noff, size_t doff, int *flags, size_t size, int clazz) { #ifdef ELFCORE + char buf[256]; const char *name = RCAST(const char *, &nbuf[noff]); int os_style = -1; @@ -901,8 +904,10 @@ do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, */ while (cp > cname && isspace(cp[-1])) cp--; - if (file_printf(ms, ", from '%.*s'", - CAST(int, cp - cname), cname) == -1) + if (file_printf(ms, ", from '%s'", + file_copystr(buf, sizeof(buf), + CAST(size_t, cp - cname), + CAST(const char *, cname))) == -1) return -1; *flags |= FLAGS_DID_CORE; return 1; @@ -1128,6 +1133,7 @@ donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size, Elf64_Nhdr nh64; size_t noff, doff; uint32_t namesz, descsz; + char buf[256]; unsigned char *nbuf = CAST(unsigned char *, vbuf); if (*notecount == 0) @@ -1255,7 +1261,8 @@ donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size, str = RCAST(const char *, &nbuf[doff]); descw = CAST(int, descsz); *flags |= flag; - file_printf(ms, ", %s: %.*s", tag, descw, str); + file_printf(ms, ", %s: %s", tag, + file_copystr(buf, sizeof(buf), descw, str)); return offset; } |