From ad763665fbbee0c45fb5862e9d49e27cf6c296f6 Mon Sep 17 00:00:00 2001 From: Christos Zoulas Date: Thu, 27 Dec 2007 16:35:58 +0000 Subject: [PATCH] Various cleanups from OpenBSD and gcc warnings. --- src/apprentice.c | 7 ++++--- src/file.c | 22 +++++++++------------- src/funcs.c | 15 ++++++++++++--- src/magic.c | 3 +-- src/print.c | 7 +++---- src/softmagic.c | 9 +++------ 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/apprentice.c b/src/apprentice.c index ed7d146e..b9b2b222 100644 --- a/src/apprentice.c +++ b/src/apprentice.c @@ -46,7 +46,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$File: apprentice.c,v 1.106 2007/10/23 19:58:59 christos Exp $") +FILE_RCSID("@(#)$File: apprentice.c,v 1.107 2007/11/08 00:31:37 christos Exp $") #endif /* lint */ #define EATAB {while (isascii((unsigned char) *l) && \ @@ -110,6 +110,7 @@ private int apprentice_compile(struct magic_set *, struct magic **, uint32_t *, const char *); private int check_format_type(const char *, int); private int check_format(struct magic_set *, struct magic *); +private int get_op(char); private size_t maxmagic = 0; private size_t magicsize = sizeof(struct magic); @@ -525,7 +526,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, private const char hdr[] = "cont\toffset\ttype\topcode\tmask\tvalue\tdesc"; FILE *f; - char line[BUFSIZ+1]; + char line[BUFSIZ]; int errs = 0; struct magic_entry *marray; uint32_t marraycount, i, mentrycount = 0; @@ -554,7 +555,7 @@ apprentice_file(struct magic_set *ms, struct magic **magicp, uint32_t *nmagicp, (void)fprintf(stderr, "%s\n", hdr); /* read and parse this file */ - for (ms->line = 1; fgets(line, BUFSIZ, f) != NULL; ms->line++) { + for (ms->line = 1; fgets(line, sizeof(line), f) != NULL; ms->line++) { size_t len; len = strlen(line); if (len == 0) /* null line, garbage, etc */ diff --git a/src/file.c b/src/file.c index b4c96dad..782b61cf 100644 --- a/src/file.c +++ b/src/file.c @@ -71,7 +71,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$File: file.c,v 1.115 2007/10/25 15:33:17 christos Exp $") +FILE_RCSID("@(#)$File: file.c,v 1.116 2007/10/29 00:54:08 christos Exp $") #endif /* lint */ @@ -122,7 +122,8 @@ private void load(const char *, int); int main(int argc, char *argv[]) { - int c, i; + int c; + size_t i; int action = 0, didsomefiles = 0, errflg = 0; int flags = 0; char *home, *usermagic; @@ -331,9 +332,9 @@ main(int argc, char *argv[]) } } else { - int i, wid, nw; - for (wid = 0, i = optind; i < argc; i++) { - nw = file_mbswidth(argv[i]); + size_t j, wid, nw; + for (wid = 0, j = (size_t)optind; j < (size_t)argc; j++) { + nw = file_mbswidth(argv[j]); if (nw > wid) wid = nw; } @@ -380,7 +381,6 @@ unwrap(char *fn) char buf[MAXPATHLEN]; FILE *f; int wid = 0, cwid; - size_t len; if (strcmp("-", fn) == 0) { f = stdin; @@ -393,9 +393,7 @@ unwrap(char *fn) } while (fgets(buf, MAXPATHLEN, f) != NULL) { - len = strlen(buf); - if (len > 0 && buf[len - 1] == '\n') - buf[len - 1] = '\0'; + buf[strcspn(buf, "\n")] = '\0'; cwid = file_mbswidth(buf); if (cwid > wid) wid = cwid; @@ -404,10 +402,8 @@ unwrap(char *fn) rewind(f); } - while (fgets(buf, MAXPATHLEN, f) != NULL) { - len = strlen(buf); - if (len > 0 && buf[len - 1] == '\n') - buf[len - 1] = '\0'; + while (fgets(buf, sizeof(buf), f) != NULL) { + buf[strcspn(buf, "\n")] = '\0'; process(buf, wid); if(nobuffer) (void)fflush(stdout); diff --git a/src/funcs.c b/src/funcs.c index 5885ff39..54e3dc18 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -48,7 +48,7 @@ #endif #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.33 2007/06/15 00:01:15 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.34 2007/10/17 19:33:31 christos Exp $") #endif /* lint */ #ifndef HAVE_VSNPRINTF @@ -62,12 +62,16 @@ protected int file_printf(struct magic_set *ms, const char *fmt, ...) { va_list ap; - size_t len, size; + size_t size; + ssize_t len; char *buf; va_start(ap, fmt); - if ((len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap)) >= ms->o.left) { + len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap); + if (len == -1) + goto out; + if (len >= (ssize_t)ms->o.left) { long diff; /* XXX: really ptrdiff_t */ va_end(ap); @@ -84,11 +88,16 @@ file_printf(struct magic_set *ms, const char *fmt, ...) va_start(ap, fmt); len = vsnprintf(ms->o.ptr, ms->o.left, fmt, ap); + if (len == -1) + goto out; } va_end(ap); ms->o.ptr += len; ms->o.left -= len; return 0; +out: + file_error(ms, errno, "vsnprintf failed"); + return -1; } /* diff --git a/src/magic.c b/src/magic.c index 11cfb20e..1fd288aa 100644 --- a/src/magic.c +++ b/src/magic.c @@ -63,7 +63,7 @@ #include "patchlevel.h" #ifndef lint -FILE_RCSID("@(#)$File: magic.c,v 1.43 2007/09/26 20:45:26 christos Exp $") +FILE_RCSID("@(#)$File: magic.c,v 1.44 2007/10/17 19:33:31 christos Exp $") #endif /* lint */ #ifdef __EMX__ @@ -260,7 +260,6 @@ file_or_fd(struct magic_set *ms, const char *inname, int fd) struct stat sb; ssize_t nbytes = 0; /* number of bytes read from a datafile */ int ispipe = 0; - int mime = ms->flags & MAGIC_MIME; /* * one extra for terminating '\0', and diff --git a/src/print.c b/src/print.c index e3241dbb..4ccd4857 100644 --- a/src/print.c +++ b/src/print.c @@ -41,7 +41,7 @@ #include #ifndef lint -FILE_RCSID("@(#)$File: print.c,v 1.59 2007/03/05 02:41:29 christos Exp $") +FILE_RCSID("@(#)$File: print.c,v 1.60 2007/11/08 00:31:37 christos Exp $") #endif /* lint */ #define SZOF(a) (sizeof(a) / sizeof(a[0])) @@ -199,7 +199,7 @@ file_magwarn(struct magic_set *ms, const char *f, ...) protected const char * file_fmttime(uint32_t v, int local) { - char *pp, *rt; + char *pp; time_t t = (time_t)v; struct tm *tm; @@ -229,7 +229,6 @@ file_fmttime(uint32_t v, int local) pp = asctime(tm); } - if ((rt = strchr(pp, '\n')) != NULL) - *rt = '\0'; + pp[strcspn(pp, "\n")] = '\0'; return pp; } diff --git a/src/softmagic.c b/src/softmagic.c index 609dcc52..5bdb457c 100644 --- a/src/softmagic.c +++ b/src/softmagic.c @@ -38,7 +38,7 @@ #ifndef lint -FILE_RCSID("@(#)$File: softmagic.c,v 1.101 2007/10/17 19:33:31 christos Exp $") +FILE_RCSID("@(#)$File: softmagic.c,v 1.102 2007/11/08 00:31:37 christos Exp $") #endif /* lint */ private int match(struct magic_set *, struct magic *, uint32_t, @@ -401,11 +401,8 @@ mprint(struct magic_set *ms, struct magic *m) t = ms->offset + m->vallen; } else { - if (*m->value.s == '\0') { - char *cp = strchr(p->s,'\n'); - if (cp) - *cp = '\0'; - } + if (*m->value.s == '\0') + p->s[strcspn(p->s, "\n")] = '\0'; if (file_printf(ms, m->desc, p->s) == -1) return -1; t = ms->offset + strlen(p->s); -- 2.40.0