From 78c2b81ccb511a740049ffe8ca5410433aae7d4e Mon Sep 17 00:00:00 2001
From: Christos Zoulas <christos@zoulas.com>
Date: Fri, 2 Jan 2015 21:29:39 +0000
Subject: [PATCH] Bug + portability fixes from the NetBSD build.

---
 src/apprentice.c |    8 +++++---
 src/cdf.c        |   24 +++++++++++++-----------
 src/compress.c   |    3 +--
 src/file.c       |    6 ++++++
 src/funcs.c      |    9 +++++----
 src/magic.c      |   10 +++++-----
 src/print.c      |    1 +
 src/readcdf.c    |    4 ++++
 src/readelf.c    |    6 ++++--
 src/softmagic.c  |    2 +-
 10 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/src/apprentice.c b/src/apprentice.c
--- a/src/apprentice.c
+++ b/src/apprentice.c
@@ -2202,7 +2202,7 @@ parse_extra(struct magic_set *ms, struct
 	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;
@@ -2251,7 +2251,8 @@ parse_apple(struct magic_set *ms, struct
 {
 	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);
 }
 
@@ -2264,7 +2265,8 @@ parse_mime(struct magic_set *ms, struct
 {
 	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
--- a/src/cdf.c
+++ b/src/cdf.c
@@ -74,8 +74,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)
 
 #define _cdf_tole2(x)	bswap_16(x)
@@ -225,7 +228,7 @@ cdf_check_stream_offset(const cdf_stream
 	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"
@@ -952,9 +955,9 @@ cdf_unpack_summary_info(const cdf_stream
 }
 
 
-#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,
@@ -982,18 +985,17 @@ cdf_unpack_catalog(const cdf_header_t *h
 	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
--- a/src/compress.c
+++ b/src/compress.c
@@ -383,7 +383,6 @@ uncompressbuf(struct magic_set *ms, int
 	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
 		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
--- a/src/file.c
+++ b/src/file.c
@@ -132,8 +132,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/funcs.c b/src/funcs.c
--- a/src/funcs.c
+++ b/src/funcs.c
@@ -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,
 		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
--- a/src/magic.c
+++ b/src/magic.c
@@ -543,19 +543,19 @@ magic_setparam(struct magic_set *ms, int
 {
 	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
--- a/src/print.c
+++ b/src/print.c
@@ -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
--- a/src/readcdf.c
+++ b/src/readcdf.c
@@ -39,6 +39,10 @@ FILE_RCSID("@(#)$File: readcdf.c,v 1.49
 #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
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -482,6 +482,7 @@ do_note_freebsd_version(struct magic_set
 }
 
 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, unsign
 			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)
@@ -1010,7 +1011,8 @@ doshn(struct magic_set *ms, int clazz, i
 	}
 
 	/* 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
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -1669,7 +1669,7 @@ mget(struct magic_set *ms, const unsigne
 
 	case FILE_INDIRECT:
 		if (m->str_flags & INDIRECT_RELATIVE)
-			offset += o;
+			offset += CAST(uint32_t, o);
 		if (offset == 0)
 			return 0;
 
