Skip to content

Commit 6f737dd

Browse files
committed
- reduce recursion level from 20 to 10 and make a symbolic constant for it.
- pull out the guts of saving and restoring the output buffer into functions and take care not to overwrite the error message if an error happened.
1 parent d7cdad0 commit 6f737dd

File tree

3 files changed

+74
-18
lines changed

3 files changed

+74
-18
lines changed

Diff for: src/file.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*/
2828
/*
2929
* file.h - definitions for file(1) program
30-
* @(#)$File: file.h,v 1.154 2014/09/10 18:41:51 christos Exp $
30+
* @(#)$File: file.h,v 1.155 2014/10/11 15:03:16 christos Exp $
3131
*/
3232

3333
#ifndef __file_h__
@@ -495,6 +495,14 @@ protected int file_regexec(file_regex_t *, const char *, size_t, regmatch_t *,
495495
protected void file_regfree(file_regex_t *);
496496
protected void file_regerror(file_regex_t *, int, struct magic_set *);
497497

498+
typedef struct {
499+
char *buf;
500+
uint32_t offset;
501+
} file_pushbuf_t;
502+
503+
protected file_pushbuf_t *file_push_buffer(struct magic_set *);
504+
protected char *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
505+
498506
#ifndef COMPILE_ONLY
499507
extern const char *file_names[];
500508
extern const size_t file_nnames;

Diff for: src/funcs.c

+41-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "file.h"
2828

2929
#ifndef lint
30-
FILE_RCSID("@(#)$File: funcs.c,v 1.72 2014/05/14 23:15:42 christos Exp $")
30+
FILE_RCSID("@(#)$File: funcs.c,v 1.73 2014/09/10 18:41:51 christos Exp $")
3131
#endif /* lint */
3232

3333
#include "magic.h"
@@ -491,3 +491,43 @@ file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
491491
file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
492492
errmsg);
493493
}
494+
495+
protected file_pushbuf_t *
496+
file_push_buffer(struct magic_set *ms)
497+
{
498+
file_pushbuf_t *pb;
499+
500+
if (ms->event_flags & EVENT_HAD_ERR)
501+
return NULL;
502+
503+
if ((pb = (CAST(file_pushbuf_t *, malloc(sizeof(*pb))))) == NULL)
504+
return NULL;
505+
506+
pb->buf = ms->o.buf;
507+
pb->offset = ms->offset;
508+
509+
ms->o.buf = NULL;
510+
ms->offset = 0;
511+
512+
return pb;
513+
}
514+
515+
protected char *
516+
file_pop_buffer(struct magic_set *ms, file_pushbuf_t *pb)
517+
{
518+
char *rbuf;
519+
520+
if (ms->event_flags & EVENT_HAD_ERR) {
521+
free(pb->buf);
522+
free(pb);
523+
return NULL;
524+
}
525+
526+
rbuf = ms->o.buf;
527+
528+
ms->o.buf = pb->buf;
529+
ms->offset = pb->offset;
530+
531+
free(pb);
532+
return rbuf;
533+
}

Diff for: src/softmagic.c

+24-16
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include "file.h"
3333

3434
#ifndef lint
35-
FILE_RCSID("@(#)$File: softmagic.c,v 1.196 2014/11/07 15:24:14 christos Exp $")
35+
FILE_RCSID("@(#)$File: softmagic.c,v 1.197 2014/11/11 17:48:23 christos Exp $")
3636
#endif /* lint */
3737

3838
#include "magic.h"
@@ -63,6 +63,9 @@ private void cvt_32(union VALUETYPE *, const struct magic *);
6363
private void cvt_64(union VALUETYPE *, const struct magic *);
6464

6565
#define OFFSET_OOB(n, o, i) ((n) < (o) || (i) > ((n) - (o)))
66+
67+
#define MAX_RECURSION_LEVEL 10
68+
6669
/*
6770
* softmagic - lookup one file in parsed, in-memory copy of database
6871
* Passed the name and FILE * of one file to be typed.
@@ -1217,14 +1220,15 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
12171220
int flip, int recursion_level, int *printed_something,
12181221
int *need_separator, int *returnval)
12191222
{
1220-
uint32_t soffset, offset = ms->offset;
1223+
uint32_t offset = ms->offset;
12211224
uint32_t lhs;
1225+
file_pushbuf_t *pb;
12221226
int rv, oneed_separator, in_type;
1223-
char *sbuf, *rbuf;
1227+
char *rbuf;
12241228
union VALUETYPE *p = &ms->ms_value;
12251229
struct mlist ml;
12261230

1227-
if (recursion_level >= 20) {
1231+
if (recursion_level >= MAX_RECURSION_LEVEL) {
12281232
file_error(ms, 0, "recursion nesting exceeded");
12291233
return -1;
12301234
}
@@ -1669,19 +1673,23 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
16691673
case FILE_INDIRECT:
16701674
if (offset == 0)
16711675
return 0;
1676+
16721677
if (nbytes < offset)
16731678
return 0;
1674-
sbuf = ms->o.buf;
1675-
soffset = ms->offset;
1676-
ms->o.buf = NULL;
1677-
ms->offset = 0;
1679+
1680+
if ((pb = file_push_buffer(ms)) == NULL)
1681+
return -1;
1682+
16781683
rv = file_softmagic(ms, s + offset, nbytes - offset,
16791684
recursion_level, BINTEST, text);
1685+
16801686
if ((ms->flags & MAGIC_DEBUG) != 0)
16811687
fprintf(stderr, "indirect @offs=%u[%d]\n", offset, rv);
1682-
rbuf = ms->o.buf;
1683-
ms->o.buf = sbuf;
1684-
ms->offset = soffset;
1688+
1689+
rbuf = file_pop_buffer(ms, pb);
1690+
if (rbuf == NULL)
1691+
return -1;
1692+
16851693
if (rv == 1) {
16861694
if ((ms->flags & (MAGIC_MIME|MAGIC_APPLE)) == 0 &&
16871695
file_printf(ms, F(ms, m, "%u"), offset) == -1) {
@@ -1699,13 +1707,13 @@ mget(struct magic_set *ms, const unsigned char *s, struct magic *m,
16991707
case FILE_USE:
17001708
if (nbytes < offset)
17011709
return 0;
1702-
sbuf = m->value.s;
1703-
if (*sbuf == '^') {
1704-
sbuf++;
1710+
rbuf = m->value.s;
1711+
if (*rbuf == '^') {
1712+
rbuf++;
17051713
flip = !flip;
17061714
}
1707-
if (file_magicfind(ms, sbuf, &ml) == -1) {
1708-
file_error(ms, 0, "cannot find entry `%s'", sbuf);
1715+
if (file_magicfind(ms, rbuf, &ml) == -1) {
1716+
file_error(ms, 0, "cannot find entry `%s'", rbuf);
17091717
return -1;
17101718
}
17111719

0 commit comments

Comments
 (0)