|
|
840325 |
diff --git a/src/apprentice.c b/src/apprentice.c
|
|
|
840325 |
index 781c5e1..50a91cf 100644
|
|
|
840325 |
--- a/src/apprentice.c
|
|
|
840325 |
+++ b/src/apprentice.c
|
|
|
840325 |
@@ -2724,11 +2724,9 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
|
|
|
840325 |
file_regex_t rx;
|
|
|
840325 |
int rc = file_regcomp(ms, &rx, m->value.s,
|
|
|
840325 |
REG_EXTENDED);
|
|
|
840325 |
- if (rc) {
|
|
|
840325 |
- if (ms->flags & MAGIC_CHECK)
|
|
|
840325 |
- file_regerror(&rx, rc, ms);
|
|
|
840325 |
+ if (rc == 0) {
|
|
|
840325 |
+ file_regfree(&rx);
|
|
|
840325 |
}
|
|
|
840325 |
- file_regfree(&rx);
|
|
|
840325 |
return rc ? -1 : 0;
|
|
|
840325 |
}
|
|
|
840325 |
return 0;
|
|
|
840325 |
diff --git a/src/file.h b/src/file.h
|
|
|
840325 |
index f049446..ee15855 100644
|
|
|
840325 |
--- a/src/file.h
|
|
|
840325 |
+++ b/src/file.h
|
|
|
840325 |
@@ -171,11 +171,7 @@
|
|
|
840325 |
#define FILE_COMPILE 2
|
|
|
840325 |
#define FILE_LIST 3
|
|
|
840325 |
|
|
|
840325 |
-typedef struct {
|
|
|
840325 |
- const char *pat;
|
|
|
840325 |
- int rc;
|
|
|
840325 |
- regex_t rx;
|
|
|
840325 |
-} file_regex_t;
|
|
|
840325 |
+typedef regex_t file_regex_t;
|
|
|
840325 |
|
|
|
840325 |
struct buffer {
|
|
|
840325 |
int fd;
|
|
|
840325 |
@@ -576,7 +572,6 @@ protected int file_regcomp(struct magic_set *, file_regex_t *, const char *,
|
|
|
840325 |
protected int file_regexec(struct magic_set *, file_regex_t *, const char *,
|
|
|
840325 |
size_t, regmatch_t *, int);
|
|
|
840325 |
protected void file_regfree(file_regex_t *);
|
|
|
840325 |
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
|
|
|
840325 |
|
|
|
840325 |
typedef struct {
|
|
|
840325 |
char *buf;
|
|
|
840325 |
diff --git a/src/funcs.c b/src/funcs.c
|
|
|
840325 |
index 1391a44..df436eb 100644
|
|
|
840325 |
--- a/src/funcs.c
|
|
|
840325 |
+++ b/src/funcs.c
|
|
|
840325 |
@@ -614,9 +614,7 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
|
|
|
840325 |
int rc, rv = -1;
|
|
|
840325 |
|
|
|
840325 |
rc = file_regcomp(ms, &rx, pat, REG_EXTENDED);
|
|
|
840325 |
- if (rc) {
|
|
|
840325 |
- file_regerror(&rx, rc, ms);
|
|
|
840325 |
- } else {
|
|
|
840325 |
+ if (rc == 0) {
|
|
|
840325 |
regmatch_t rm;
|
|
|
840325 |
int nm = 0;
|
|
|
840325 |
while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) {
|
|
|
840325 |
@@ -644,16 +642,22 @@ file_regcomp(struct magic_set *ms, file_regex_t *rx, const char *pat, int flags)
|
|
|
840325 |
strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old));
|
|
|
840325 |
(void)setlocale(LC_CTYPE, "C");
|
|
|
840325 |
#endif
|
|
|
840325 |
- rx->pat = pat;
|
|
|
840325 |
-
|
|
|
840325 |
- rx->rc = regcomp(&rx->rx, pat, flags);
|
|
|
840325 |
+ int rc;
|
|
|
840325 |
+ rc = regcomp(rx, pat, flags);
|
|
|
840325 |
|
|
|
840325 |
#ifdef USE_C_LOCALE
|
|
|
840325 |
uselocale(old);
|
|
|
840325 |
#else
|
|
|
840325 |
(void)setlocale(LC_CTYPE, old);
|
|
|
840325 |
#endif
|
|
|
840325 |
- return rx->rc;
|
|
|
840325 |
+ if (rc > 0 && (ms->flags & MAGIC_CHECK)) {
|
|
|
840325 |
+ char errmsg[512];
|
|
|
840325 |
+
|
|
|
840325 |
+ (void)regerror(rc, rx, errmsg, sizeof(errmsg));
|
|
|
840325 |
+ file_magerror(ms, "regex error %d for `%s', (%s)", rc, pat,
|
|
|
840325 |
+ errmsg);
|
|
|
840325 |
+ }
|
|
|
840325 |
+ return rc;
|
|
|
840325 |
}
|
|
|
840325 |
|
|
|
840325 |
protected int
|
|
|
840325 |
@@ -669,11 +673,10 @@ file_regexec(struct magic_set *ms, file_regex_t *rx, const char *str,
|
|
|
840325 |
(void)setlocale(LC_CTYPE, "C");
|
|
|
840325 |
#endif
|
|
|
840325 |
int rc;
|
|
|
840325 |
- assert(rx->rc == 0);
|
|
|
840325 |
/* XXX: force initialization because glibc does not always do this */
|
|
|
840325 |
if (nmatch != 0)
|
|
|
840325 |
memset(pmatch, 0, nmatch * sizeof(*pmatch));
|
|
|
840325 |
- rc = regexec(&rx->rx, str, nmatch, pmatch, eflags);
|
|
|
840325 |
+ rc = regexec(rx, str, nmatch, pmatch, eflags);
|
|
|
840325 |
#ifdef USE_C_LOCALE
|
|
|
840325 |
uselocale(old);
|
|
|
840325 |
#else
|
|
|
840325 |
@@ -685,18 +688,7 @@ file_regexec(struct magic_set *ms, file_regex_t *rx, const char *str,
|
|
|
840325 |
protected void
|
|
|
840325 |
file_regfree(file_regex_t *rx)
|
|
|
840325 |
{
|
|
|
840325 |
- if (rx->rc == 0)
|
|
|
840325 |
- regfree(&rx->rx);
|
|
|
840325 |
-}
|
|
|
840325 |
-
|
|
|
840325 |
-protected void
|
|
|
840325 |
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
|
|
|
840325 |
-{
|
|
|
840325 |
- char errmsg[512];
|
|
|
840325 |
-
|
|
|
840325 |
- (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
|
|
|
840325 |
- file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
|
|
|
840325 |
- errmsg);
|
|
|
840325 |
+ regfree(rx);
|
|
|
840325 |
}
|
|
|
840325 |
|
|
|
840325 |
protected file_pushbuf_t *
|
|
|
840325 |
diff --git a/src/softmagic.c b/src/softmagic.c
|
|
|
840325 |
index b4052a6..f469a12 100644
|
|
|
840325 |
--- a/src/softmagic.c
|
|
|
840325 |
+++ b/src/softmagic.c
|
|
|
840325 |
@@ -474,14 +474,13 @@ check_fmt(struct magic_set *ms, const char *fmt)
|
|
|
840325 |
{
|
|
|
840325 |
file_regex_t rx;
|
|
|
840325 |
int rc, rv = -1;
|
|
|
840325 |
+ const char* pat = "%[-0-9\\.]*s";
|
|
|
840325 |
|
|
|
840325 |
if (strchr(fmt, '%') == NULL)
|
|
|
840325 |
return 0;
|
|
|
840325 |
|
|
|
840325 |
- rc = file_regcomp(ms, &rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
|
|
|
840325 |
- if (rc) {
|
|
|
840325 |
- file_regerror(&rx, rc, ms);
|
|
|
840325 |
- } else {
|
|
|
840325 |
+ rc = file_regcomp(ms, &rx, pat, REG_EXTENDED|REG_NOSUB);
|
|
|
840325 |
+ if (rc == 0) {
|
|
|
840325 |
rc = file_regexec(ms, &rx, fmt, 0, 0, 0);
|
|
|
840325 |
rv = !rc;
|
|
|
840325 |
}
|
|
|
840325 |
@@ -2008,9 +2007,6 @@ alloc_regex(struct magic_set *ms, struct magic *m)
|
|
|
840325 |
if (rc == 0)
|
|
|
840325 |
return rx;
|
|
|
840325 |
|
|
|
840325 |
-fprintf(stderr, "regcomp %s %d\n", m->value.s, rc);
|
|
|
840325 |
- file_regerror(rx, rc, ms);
|
|
|
840325 |
- file_regfree(rx);
|
|
|
840325 |
free(rx);
|
|
|
840325 |
return NULL;
|
|
|
840325 |
}
|
|
|
840325 |
@@ -2243,12 +2239,9 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache)
|
|
|
840325 |
break;
|
|
|
840325 |
|
|
|
840325 |
default:
|
|
|
840325 |
- file_regerror(rx, rc, ms);
|
|
|
840325 |
- v = CAST(uint64_t, -1);
|
|
|
840325 |
+ return -1;
|
|
|
840325 |
break;
|
|
|
840325 |
}
|
|
|
840325 |
- if (v == CAST(uint64_t, -1))
|
|
|
840325 |
- return -1;
|
|
|
840325 |
break;
|
|
|
840325 |
}
|
|
|
840325 |
case FILE_INDIRECT:
|