if (*nmagicp + 1 >= maxmagic){
maxmagic += ALLOC_INCR;
/*@-unqualifiedtrans @*/
- if ((m = (struct magic *) realloc(*magicp,
- sizeof(struct magic) * maxmagic)) == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n",
- progname, strerror(errno));
-/*@-usereleased@*/
- if (*magicp != NULL)
- free(*magicp);
-/*@=usereleased@*/
- if (action == CHECK)
- return -1;
- else
- exit(EXIT_FAILURE);
- }
+ *magicp = xrealloc(*magicp, sizeof(struct magic) * maxmagic);
/*@=unqualifiedtrans @*/
- *magicp = m;
- memset(&(*magicp)[*nmagicp], 0, sizeof(struct magic)
- * ALLOC_INCR);
- }
- m = &(*magicp)[*nmagicp];
+ m = &(*magicp)[*nmagicp];
+ memset(m, 0, sizeof(struct magic) * ALLOC_INCR);
+ } else
+ m = &(*magicp)[*nmagicp];
m->flag = 0;
m->cont_level = 0;
/*
* make a dbname
*/
-/*@null@*/
static char *
mkdbname(const char *fn)
/*@globals fileSystem @*/
static const char ext[] = ".mgc";
/*@only@*/
static char *buf = NULL;
- size_t len = strlen(fn) + sizeof(ext) + 1;
- if (buf == NULL)
- buf = malloc(len);
- else
- buf = realloc(buf, len);
- if (buf == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
- strerror(errno));
- return NULL;
- }
+
+ buf = xrealloc(buf, strlen(fn) + sizeof(ext) + 1);
(void)strcpy(buf, fn);
(void)strcat(buf, ext);
return buf;
}
maxmagic = MAXMAGIS;
- *magicp = (struct magic *) calloc(sizeof(struct magic), maxmagic);
- if (*magicp == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
- strerror(errno));
- if (action == CHECK)
- return -1;
- }
+ *magicp = (struct magic *) xcalloc(sizeof(struct magic), maxmagic);
/* parse it */
if (action == CHECK) /* print silly verbose header for USG compat. */
{
int fd;
char *dbname = mkdbname(fn);
+ /*@observer@*/
static const uint32_t ar[] = {
MAGICNO, VERSIONNO
};
goto error;
}
#else
- if ((mm = malloc((size_t)st.st_size)) == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
- strerror(errno));
- goto error;
- }
+ mm = xmalloc((size_t)st.st_size);
if (read(fd, mm, (size_t)st.st_size) != (size_t)st.st_size) {
(void) fprintf(stderr, "%s: Read failed (%s).\n", progname,
strerror(errno));
if (rv != 0)
return rv;
- if ((ml = malloc(sizeof(*ml))) == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
- strerror(errno));
- if (action == CHECK)
- return -1;
- }
+ ml = xmalloc(sizeof(*ml));
if (magic == NULL || nmagic == 0)
return rv;
mlist.next = &mlist;
mlist.prev = &mlist;
/*@=immediatetrans@*/
- mfn = malloc(strlen(fn)+1);
- if (mfn == NULL) {
- (void) fprintf(stderr, "%s: Out of memory (%s).\n", progname,
- strerror(errno));
-/*@-compmempass@*/
- if (action == CHECK)
- return -1;
- else
- exit(EXIT_FAILURE);
-/*@=compmempass@*/
- }
+
+ mfn = xmalloc(strlen(fn)+1);
fn = strcpy(mfn, fn);
/*@-branchstate@*/
FILE_RCSID("@(#)Id: softmagic.c,v 1.51 2002/07/03 18:26:38 christos Exp ")
static int32_t
-mprint(union VALUETYPE *p, struct magic *m)
+sm_print(union VALUETYPE *p, struct magic *m)
/*@globals fileSystem @*/
/*@modifies p, fileSystem @*/
{
break;
default:
- error("invalid m->type (%d) in mprint().\n", m->type);
+ error("invalid m->type (%d) in sm_print().\n", m->type);
/*@notreached@*/
}
return(t);
* (unless you have a better idea)
*/
static int
-mconvert(union VALUETYPE *p, struct magic *m)
+sm_convert(union VALUETYPE *p, struct magic *m)
/*@globals fileSystem @*/
/*@modifies p, fileSystem @*/
{
case REGEX:
return 1;
default:
- error("invalid type %d in mconvert().\n", m->type);
+ error("invalid type %d in sm_convert().\n", m->type);
/*@notreached@*/
return 0;
}
static void
-mdebug(int32_t offset, char *str, int len)
+sm_debug(int32_t offset, char *str, int len)
/*@globals fileSystem @*/
/*@modifies fileSystem @*/
{
- (void) fprintf(stderr, "mget @%d: ", offset);
+ (void) fprintf(stderr, "sm_get @%d: ", offset);
showstr(stderr, (char *) str, len);
(void) fputc('\n', stderr);
(void) fputc('\n', stderr);
}
static int
-mget(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
+sm_get(union VALUETYPE *p, unsigned char *s, struct magic *m, int nbytes)
/*@globals fileSystem @*/
/*@modifies p, s, fileSystem @*/
{
/*@=branchstate@*/
if (debug) {
- mdebug(offset, (char *) p, sizeof(union VALUETYPE));
+ sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
mdump(m);
}
memcpy(p, s + offset, sizeof(union VALUETYPE));
if (debug) {
- mdebug(offset, (char *) p, sizeof(union VALUETYPE));
+ sm_debug(offset, (char *) p, sizeof(union VALUETYPE));
mdump(m);
}
}
- if (!mconvert(p, m))
+ if (!sm_convert(p, m))
return 0;
return 1;
}
static int
-mcheck(union VALUETYPE *p, struct magic *m)
+sm_check(union VALUETYPE *p, struct magic *m)
/*@globals fileSystem @*/
/*@modifies fileSystem @*/
{
}
/*@notreached@*/ break;
default:
- error("invalid type %d in mcheck().\n", m->type);
+ error("invalid type %d in sm_check().\n", m->type);
/*@notreached@*/
return 0;
}
default:
matched = 0;
- error("mcheck: can't happen: invalid relation %d.\n", m->reln);
+ error("sm_check: can't happen: invalid relation %d.\n", m->reln);
/*@notreached@*/ break;
}
* so that higher-level continuations are processed.
*/
static int
-match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
+sm_match(struct magic *m, uint32_t nmagic, unsigned char *s, int nbytes)
/*@globals fileSystem @*/
/*@modifies m, s, fileSystem @*/
{
int returnval = 0; /* if a match is found it is set to 1*/
int firstline = 1; /* a flag to print X\n X\n- X */
- if (tmpoff == NULL)
- if ((tmpoff = (int32_t *) malloc(tmplen = 20)) == NULL)
- error("out of memory\n");
-
for (magindex = 0; magindex < nmagic; magindex++) {
/* if main entry matches, print it... */
- if (!mget(&p, s, &m[magindex], nbytes) ||
- !mcheck(&p, &m[magindex])) {
+ if (!sm_get(&p, s, &m[magindex], nbytes) ||
+ !sm_check(&p, &m[magindex])) {
/*
* main entry didn't match,
* flush its continuations
}
if (! firstline) { /* we found another match */
- /* put a newline and '-' to do some simple formatting*/
+ /* put a newline and '-' to do some simple formatting */
printf("\n- ");
}
- tmpoff[cont_level] = mprint(&p, &m[magindex]);
+ if ((cont_level+1) >= tmplen)
+ tmpoff = (int32_t *) xrealloc(tmpoff, tmplen += 20);
+ tmpoff[cont_level] = sm_print(&p, &m[magindex]);
+ cont_level++;
+
/*
* If we printed something, we'll need to print
* a blank before we print something else.
*/
if (m[magindex].desc[0])
need_separator = 1;
- /* and any continuations that match */
- if (++cont_level >= tmplen)
- if ((tmpoff = (int32_t *) realloc(tmpoff,
- tmplen += 20)) == NULL)
- error("out of memory\n");
- while (m[magindex+1].cont_level != 0 &&
- ++magindex < nmagic) {
- if (cont_level >= m[magindex].cont_level) {
- if (cont_level > m[magindex].cont_level) {
- /*
- * We're at the end of the level
- * "cont_level" continuations.
- */
- cont_level = m[magindex].cont_level;
- }
- if (m[magindex].flag & OFFADD) {
- oldoff=m[magindex].offset;
- m[magindex].offset +=
- tmpoff[cont_level-1];
- }
- if (mget(&p, s, &m[magindex], nbytes) &&
- mcheck(&p, &m[magindex])) {
- /*
- * This continuation matched.
- * Print its message, with
- * a blank before it if
- * the previous item printed
- * and this item isn't empty.
- */
- /* space if previous printed */
- if (need_separator
- && (m[magindex].nospflag == 0)
- && (m[magindex].desc[0] != '\0')
- ) {
- (void) putchar(' ');
- need_separator = 0;
- }
- tmpoff[cont_level] =
- mprint(&p, &m[magindex]);
- if (m[magindex].desc[0])
- need_separator = 1;
- /*
- * If we see any continuations
- * at a higher level,
- * process them.
- */
- if (++cont_level >= tmplen)
- if ((tmpoff =
- (int32_t *) realloc(tmpoff,
- tmplen += 20)) == NULL)
- error("out of memory\n");
- }
- if (m[magindex].flag & OFFADD) {
- m[magindex].offset = oldoff;
+ /* and any continuations that match */
+ while (m[magindex+1].cont_level != 0 && ++magindex < nmagic) {
+ if (cont_level < m[magindex].cont_level)
+ continue;
+ if (cont_level > m[magindex].cont_level) {
+ /*
+ * We're at the end of the level
+ * "cont_level" continuations.
+ */
+ cont_level = m[magindex].cont_level;
+ }
+ if (m[magindex].flag & OFFADD) {
+ oldoff = m[magindex].offset;
+ m[magindex].offset += tmpoff[cont_level-1];
+ }
+ if (sm_get(&p, s, &m[magindex], nbytes) &&
+ sm_check(&p, &m[magindex]))
+ {
+ /*
+ * This continuation matched.
+ * Print its message, with
+ * a blank before it if
+ * the previous item printed
+ * and this item isn't empty.
+ */
+ /* space if previous printed */
+ if (need_separator
+ && (m[magindex].nospflag == 0)
+ && (m[magindex].desc[0] != '\0')
+ ) {
+ (void) putchar(' ');
+ need_separator = 0;
}
+ if ((cont_level+1) >= tmplen)
+ tmpoff = xrealloc(tmpoff, tmplen += 20);
+ tmpoff[cont_level] = sm_print(&p, &m[magindex]);
+ cont_level++;
+ if (m[magindex].desc[0])
+ need_separator = 1;
}
+ if (m[magindex].flag & OFFADD)
+ m[magindex].offset = oldoff;
}
firstline = 0;
returnval = 1;
- if (!kflag) {
- return 1; /* don't keep searching */
- }
+ if (!kflag) /* don't keep searching */
+ return 1;
}
return returnval; /* This is hit if -k is set or there is no match */
}
struct mlist *ml;
for (ml = mlist.next; ml != &mlist; ml = ml->next) {
- if (match(ml->magic, ml->nmagic, buf, nbytes))
+ if (sm_match(ml->magic, ml->nmagic, buf, nbytes))
return 1;
}
#define strtoul(a, b, c) strtol(a, b, c)
#endif
+/*@-declundef -incondefs @*/
+/**
+ */
+/*@mayexit@*/ /*@only@*/ /*@out@*/
+void * xmalloc (size_t size)
+ /*@globals errno @*/
+ /*@ensures maxSet(result) == (size - 1) @*/
+ /*@modifies errno @*/;
+
+/**
+ */
+/*@mayexit@*/ /*@only@*/
+void * xcalloc (size_t nmemb, size_t size)
+ /*@ensures maxSet(result) == (nmemb - 1) @*/
+ /*@*/;
+
+/**
+ * @todo Annotate ptr with returned/out.
+ */
+/*@mayexit@*/ /*@only@*/
+void * xrealloc (/*@null@*/ /*@only@*/ void * ptr, size_t size)
+ /*@ensures maxSet(result) == (size - 1) @*/
+ /*@modifies *ptr @*/;
+
+/**
+ */
+/*@mayexit@*/ /*@only@*/
+char * xstrdup (const char *str)
+ /*@*/;
+/*@=declundef =incondefs @*/
+
+#if HAVE_MCHECK_H
+#include <mcheck.h>
+#if defined(__LCLINT__)
+/*@-declundef -incondefs @*/ /* LCL: missing annotations */
+extern int mcheck (void (*__abortfunc) (enum mcheck_status)) __THROW
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+extern int mcheck_pedantic (void (*__abortfunc) (enum mcheck_status)) __THROW
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+extern void mcheck_check_all (void)
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+extern enum mcheck_status mprobe (void *__ptr) __THROW
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+extern void mtrace (void) __THROW
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+extern void muntrace (void) __THROW
+ /*@globals internalState@*/
+ /*@modifies internalState @*/;
+/*@=declundef =incondefs @*/
+#endif /* defined(__LCLINT__) */
+#endif /* HAVE_MCHECK_H */
+
+/* Memory allocation via macro defs to get meaningful locations from mtrace() */
+#define xmalloc(_size) (malloc(_size) ? : (error("out of memory"), NULL))
+#define xcalloc(_nmemb, _size) (calloc((_nmemb), (_size)) ? : (error("out of memory"), NULL))
+#define xrealloc(_ptr, _size) (realloc((_ptr), (_size)) ? : (error("out of memory"), NULL))
+#define xstrdup(_str) (strcpy(xmalloc(strlen(_str)+1), (_str)))
+
#if HAVE_LOCALE_H
# include <locale.h>
#endif