From 78c2b81ccb511a740049ffe8ca5410433aae7d4e Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Fri, 2 Jan 2015 21:29:39 +0000 Subject: [PATCH] Bug + portability fixes from the NetBSD build. --- src/apprentice.c | 10 ++++++---- src/cdf.c | 26 ++++++++++++++------------ src/compress.c | 5 ++--- src/file.c | 8 +++++++- src/file.h | 5 ++++- src/funcs.c | 11 ++++++----- src/magic.c | 12 ++++++------ src/print.c | 3 ++- src/readcdf.c | 6 +++++- src/readelf.c | 8 +++++--- src/softmagic.c | 4 ++-- 11 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/apprentice.c b/src/apprentice.c index 02e081fb..bb6f51cd 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.228 2014/12/16 23:18:40 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.229 2015/01/01 17:07:34 christos Exp $") #endif /* lint */ #include "magic.h" @@ -2199,7 +2199,7 @@ parse_extra(struct magic_set *ms, struct magic_entry *me, const char *line, size_t i; const char *l = line; struct magic *m = &me->mp[me->cont_count == 0 ? 0 : me->cont_count - 1]; - char *buf = (char *)m + off; + char *buf = CAST(char *, CAST(void *, m)) + off; if (buf[0] != '\0') { len = nt ? strlen(buf) : len; @@ -2248,7 +2248,8 @@ parse_apple(struct magic_set *ms, struct magic_entry *me, const char *line) { struct magic *m = &me->mp[0]; - return parse_extra(ms, me, line, offsetof(struct magic, apple), + return parse_extra(ms, me, line, + CAST(off_t, offsetof(struct magic, apple)), sizeof(m->apple), "APPLE", "!+-./", 0); } @@ -2261,7 +2262,8 @@ parse_mime(struct magic_set *ms, struct magic_entry *me, const char *line) { struct magic *m = &me->mp[0]; - return parse_extra(ms, me, line, offsetof(struct magic, mimetype), + return parse_extra(ms, me, line, + CAST(off_t, offsetof(struct magic, mimetype)), sizeof(m->mimetype), "MIME", "+-/.", 1); } diff --git a/src/cdf.c b/src/cdf.c index 2768de86..9d843a6c 100644 --- a/src/cdf.c +++ b/src/cdf.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: cdf.c,v 1.68 2014/10/22 19:27:36 christos Exp $") +FILE_RCSID("@(#)$File: cdf.c,v 1.69 2014/12/04 15:56:46 christos Exp $") #endif #include @@ -73,8 +73,11 @@ static union { #define CDF_TOLE8(x) ((uint64_t)(NEED_SWAP ? _cdf_tole8(x) : (uint64_t)(x))) #define CDF_TOLE4(x) ((uint32_t)(NEED_SWAP ? _cdf_tole4(x) : (uint32_t)(x))) #define CDF_TOLE2(x) ((uint16_t)(NEED_SWAP ? _cdf_tole2(x) : (uint16_t)(x))) -#define CDF_TOLE(x) (sizeof(x) == 2 ? CDF_TOLE2(x) : (sizeof(x) == 4 ? \ - CDF_TOLE4(x) : CDF_TOLE8(x))) +#define CDF_TOLE(x) (/*CONSTCOND*/sizeof(x) == 2 ? \ + CDF_TOLE2(CAST(uint16_t, x)) : \ + (/*CONSTCOND*/sizeof(x) == 4 ? \ + CDF_TOLE4(CAST(uint32_t, x)) : \ + CDF_TOLE8(CAST(uint64_t, x)))) #define CDF_GETUINT32(x, y) cdf_getuint32(x, y) @@ -271,7 +274,7 @@ cdf_check_stream_offset(const cdf_stream_t *sst, const cdf_header_t *h, const char *e = ((const char *)p) + tail; size_t ss = sst->sst_dirlen < h->h_min_size_standard_stream ? CDF_SHORT_SEC_SIZE(h) : CDF_SEC_SIZE(h); - (void)&line; + /*LINTED*/(void)&line; if (e >= b && (size_t)(e - b) <= ss * sst->sst_len) return 0; DPRINTF(("%d: offset begin %p < end %p || %" SIZE_T_FORMAT "u" @@ -998,9 +1001,9 @@ cdf_unpack_summary_info(const cdf_stream_t *sst, const cdf_header_t *h, } -#define extract_catalog_field(f, l) \ +#define extract_catalog_field(t, f, l) \ memcpy(&ce[i].f, b + (l), sizeof(ce[i].f)); \ - ce[i].f = CDF_TOLE(ce[i].f) + ce[i].f = CAST(t, CDF_TOLE(ce[i].f)) int cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst, @@ -1028,18 +1031,17 @@ cdf_unpack_catalog(const cdf_header_t *h, const cdf_stream_t *sst, ce = (*cat)->cat_e; b = CAST(const char *, sst->sst_tab); for (i = 0; i < nr; i++) { - extract_catalog_field(ce_namlen, 0); - extract_catalog_field(ce_num, 2); - extract_catalog_field(ce_timestamp, 6); + extract_catalog_field(uint16_t, ce_namlen, 0); + extract_catalog_field(uint16_t, ce_num, 2); + extract_catalog_field(uint64_t, ce_timestamp, 6); reclen = ce[i].ce_namlen; ce[i].ce_namlen = sizeof(ce[i].ce_name) / sizeof(ce[i].ce_name[0]) - 1; if (ce[i].ce_namlen > reclen - 14) ce[i].ce_namlen = reclen - 14; - np = CAST(const uint16_t *, (b + 16)); + np = CAST(const uint16_t *, CAST(const void *, (b + 16))); for (k = 0; k < ce[i].ce_namlen; k++) { - ce[i].ce_name[k] = np[k]; - CDF_TOLE2(ce[i].ce_name[k]); + ce[i].ce_name[k] = np[k]; /* XXX: CDF_TOLE2? */ } ce[i].ce_name[ce[i].ce_namlen] = 0; b += reclen; diff --git a/src/compress.c b/src/compress.c index 30be5691..207c4c19 100644 --- a/src/compress.c +++ b/src/compress.c @@ -35,7 +35,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: compress.c,v 1.76 2014/12/11 11:47:08 christos Exp $") +FILE_RCSID("@(#)$File: compress.c,v 1.77 2014/12/12 16:33:01 christos Exp $") #endif #include "magic.h" @@ -383,7 +383,6 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, int fdin[2], fdout[2]; int status; ssize_t r; - pid_t pid; #ifdef BUILTIN_DECOMPRESS /* FIXME: This doesn't cope with bzip2 */ @@ -397,7 +396,7 @@ uncompressbuf(struct magic_set *ms, int fd, size_t method, file_error(ms, errno, "cannot create pipe"); return NODATA; } - switch (pid = fork()) { + switch (fork()) { case 0: /* child */ (void) close(0); if (fd != -1) { diff --git a/src/file.c b/src/file.c index f1312cff..58589bd3 100644 --- a/src/file.c +++ b/src/file.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.159 2014/11/28 02:46:39 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.160 2014/12/16 23:18:40 christos Exp $") #endif /* lint */ #include "magic.h" @@ -130,8 +130,14 @@ private struct { private char *progname; /* used throughout */ +#ifdef __dead +__dead +#endif private void usage(void); private void docprint(const char *); +#ifdef __dead +__dead +#endif private void help(void); private int unwrap(struct magic_set *, const char *); diff --git a/src/file.h b/src/file.h index 9a780733..5fc8d698 100644 --- a/src/file.h +++ b/src/file.h @@ -27,7 +27,7 @@ */ /* * file.h - definitions for file(1) program - * @(#)$File: file.h,v 1.163 2014/12/16 23:18:40 christos Exp $ + * @(#)$File: file.h,v 1.164 2015/01/01 17:07:34 christos Exp $ */ #ifndef __file_h__ @@ -590,5 +590,8 @@ static const char *rcsid(const char *p) { \ #else #define FILE_RCSID(id) #endif +#ifndef __RCSID +#define __RCSID(a) +#endif #endif /* __file_h__ */ diff --git a/src/funcs.c b/src/funcs.c index b44f5623..ea7d80dd 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.78 2014/12/11 12:34:24 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.79 2014/12/16 20:52:49 christos Exp $") #endif /* lint */ #include "magic.h" @@ -159,8 +159,9 @@ file_badread(struct magic_set *ms) } #ifndef COMPILE_ONLY +/*ARGSUSED*/ protected int -file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)), +file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((__unused__)), const void *buf, size_t nb) { int m = 0, rv = 0, looks_text = 0; @@ -549,9 +550,9 @@ file_printable(char *buf, size_t bufsiz, const char *str) if (ptr >= eptr - 3) break; *ptr++ = '\\'; - *ptr++ = ((*s >> 6) & 7) + '0'; - *ptr++ = ((*s >> 3) & 7) + '0'; - *ptr++ = ((*s >> 0) & 7) + '0'; + *ptr++ = ((CAST(unsigned int, *s) >> 6) & 7) + '0'; + *ptr++ = ((CAST(unsigned int, *s) >> 3) & 7) + '0'; + *ptr++ = ((CAST(unsigned int, *s) >> 0) & 7) + '0'; } *ptr = '\0'; return buf; diff --git a/src/magic.c b/src/magic.c index 0c3371bf..c8044ed3 100644 --- a/src/magic.c +++ b/src/magic.c @@ -33,7 +33,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.90 2014/12/04 15:56:46 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.91 2014/12/16 23:18:40 christos Exp $") #endif /* lint */ #include "magic.h" @@ -543,19 +543,19 @@ magic_setparam(struct magic_set *ms, int param, const void *val) { switch (param) { case MAGIC_PARAM_INDIR_MAX: - ms->indir_max = *(const size_t *)val; + ms->indir_max = (uint16_t)*(const size_t *)val; return 0; case MAGIC_PARAM_NAME_MAX: - ms->name_max = *(const size_t *)val; + ms->name_max = (uint16_t)*(const size_t *)val; return 0; case MAGIC_PARAM_ELF_PHNUM_MAX: - ms->elf_phnum_max = *(const size_t *)val; + ms->elf_phnum_max = (uint16_t)*(const size_t *)val; return 0; case MAGIC_PARAM_ELF_SHNUM_MAX: - ms->elf_shnum_max = *(const size_t *)val; + ms->elf_shnum_max = (uint16_t)*(const size_t *)val; return 0; case MAGIC_PARAM_ELF_NOTES_MAX: - ms->elf_notes_max = *(const size_t *)val; + ms->elf_notes_max = (uint16_t)*(const size_t *)val; return 0; default: errno = EINVAL; diff --git a/src/print.c b/src/print.c index f651cf4f..3216b9b9 100644 --- a/src/print.c +++ b/src/print.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: print.c,v 1.75 2012/10/30 23:11:51 christos Exp $") +FILE_RCSID("@(#)$File: print.c,v 1.76 2013/02/26 18:25:00 christos Exp $") #endif /* lint */ #include @@ -164,6 +164,7 @@ file_mdump(struct magic *m) case FILE_MELDATE: (void)fprintf(stderr, "%s,", file_fmttime(m->value.l, 0, tbuf)); + break; case FILE_QDATE: case FILE_LEQDATE: case FILE_BEQDATE: diff --git a/src/readcdf.c b/src/readcdf.c index e2f151a2..33868888 100644 --- a/src/readcdf.c +++ b/src/readcdf.c @@ -26,7 +26,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: readcdf.c,v 1.48 2014/09/10 18:41:51 christos Exp $") +FILE_RCSID("@(#)$File: readcdf.c,v 1.49 2014/12/04 15:56:46 christos Exp $") #endif #include @@ -39,6 +39,10 @@ FILE_RCSID("@(#)$File: readcdf.c,v 1.48 2014/09/10 18:41:51 christos Exp $") #include "cdf.h" #include "magic.h" +#ifndef __arraycount +#define __arraycount(a) (sizeof(a) / sizeof(a[0])) +#endif + #define NOTMIME(ms) (((ms)->flags & MAGIC_MIME) == 0) static const struct nv { diff --git a/src/readelf.c b/src/readelf.c index cd1f79d5..237e4bb6 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.116 2014/12/16 23:18:40 christos Exp $") +FILE_RCSID("@(#)$File: readelf.c,v 1.117 2014/12/16 23:29:42 christos Exp $") #endif #ifdef BUILTIN_ELF @@ -482,6 +482,7 @@ do_note_freebsd_version(struct magic_set *ms, int swap, void *v) } private int +/*ARGSUSED*/ do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz, size_t noff, size_t doff, int *flags) @@ -622,7 +623,7 @@ do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, return 1; for (i = 0; i < __arraycount(pax); i++) { - if (((1 << i) & desc) == 0) + if (((1 << (int)i) & desc) == 0) continue; if (file_printf(ms, "%s%s", did++ ? "," : "", pax[i]) == -1) @@ -1008,7 +1009,8 @@ doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num, } /* Read offset of name section to be able to read section names later */ - if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) < (ssize_t)xsh_sizeof) { + if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab))) + < (ssize_t)xsh_sizeof) { file_badread(ms); return -1; } diff --git a/src/softmagic.c b/src/softmagic.c index 29be5f9b..0fd2b71c 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -32,7 +32,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.205 2015/01/01 04:12:23 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.206 2015/01/01 17:07:34 christos Exp $") #endif /* lint */ #include "magic.h" @@ -1666,7 +1666,7 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m, case FILE_INDIRECT: if (m->str_flags & INDIRECT_RELATIVE) - offset += o; + offset += CAST(uint32_t, o); if (offset == 0) return 0; -- 2.40.0