summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristos Zoulas <christos@zoulas.com>2022-04-18 21:50:49 +0000
committerChristos Zoulas <christos@zoulas.com>2022-04-18 21:50:49 +0000
commit4254a0afecfd2ae200c694dfcf93f4b4ac21652e (patch)
treea9537c1b9cc9859086c344032f044675551f362e
parentd1a00ae92b2cf09298615cf3aba474d8fec7380f (diff)
downloadfile-git-4254a0afecfd2ae200c694dfcf93f4b4ac21652e.tar.gz
From: Dirk Mueller:
Only regcomp() can return an error, regexec() only returns match or no-match. This allows to merge file_regerror into file_regcomp and thereby simplifies the callers of file_regcomp() as they no longer need to handle errors. Also this reduces memory overhead slightly as we can make file_regex_t directly a regex_t, saving ~ 16 bytes per regex.
-rw-r--r--src/apprentice.c8
-rw-r--r--src/file.h9
-rw-r--r--src/funcs.c36
-rw-r--r--src/softmagic.c17
4 files changed, 24 insertions, 46 deletions
diff --git a/src/apprentice.c b/src/apprentice.c
index 992102b4..0c422407 100644
--- a/src/apprentice.c
+++ b/src/apprentice.c
@@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: apprentice.c,v 1.322 2022/04/18 21:46:43 christos Exp $")
+FILE_RCSID("@(#)$File: apprentice.c,v 1.323 2022/04/18 21:50:49 christos Exp $")
#endif /* lint */
#include "magic.h"
@@ -2830,11 +2830,9 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
file_regex_t rx;
int rc = file_regcomp(ms, &rx, m->value.s,
REG_EXTENDED);
- if (rc) {
- if (ms->flags & MAGIC_CHECK)
- file_regerror(&rx, rc, ms);
+ if (rc == 0) {
+ file_regfree(&rx);
}
- file_regfree(&rx);
return rc ? -1 : 0;
}
return 0;
diff --git a/src/file.h b/src/file.h
index 51caa599..aea3f101 100644
--- a/src/file.h
+++ b/src/file.h
@@ -27,7 +27,7 @@
*/
/*
* file.h - definitions for file(1) program
- * @(#)$File: file.h,v 1.231 2022/04/11 18:07:38 christos Exp $
+ * @(#)$File: file.h,v 1.232 2022/04/18 21:50:49 christos Exp $
*/
#ifndef __file_h__
@@ -171,11 +171,7 @@
#define FILE_COMPILE 2
#define FILE_LIST 3
-typedef struct {
- const char *pat;
- int rc;
- regex_t rx;
-} file_regex_t;
+typedef regex_t file_regex_t;
struct buffer {
int fd;
@@ -601,7 +597,6 @@ protected int file_regcomp(struct magic_set *, file_regex_t *, const char *,
protected int file_regexec(struct magic_set *, file_regex_t *, const char *,
size_t, regmatch_t *, int);
protected void file_regfree(file_regex_t *);
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
typedef struct {
char *buf;
diff --git a/src/funcs.c b/src/funcs.c
index 2c1b2164..aab9c3bd 100644
--- a/src/funcs.c
+++ b/src/funcs.c
@@ -27,7 +27,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: funcs.c,v 1.127 2022/04/11 18:07:38 christos Exp $")
+FILE_RCSID("@(#)$File: funcs.c,v 1.128 2022/04/18 21:50:49 christos Exp $")
#endif /* lint */
#include "magic.h"
@@ -639,9 +639,7 @@ file_replace(struct magic_set *ms, const char *pat, const char *rep)
int rc, rv = -1;
rc = file_regcomp(ms, &rx, pat, REG_EXTENDED);
- if (rc) {
- file_regerror(&rx, rc, ms);
- } else {
+ if (rc == 0) {
regmatch_t rm;
int nm = 0;
while (file_regexec(ms, &rx, ms->o.buf, 1, &rm, 0) == 0) {
@@ -670,16 +668,22 @@ file_regcomp(struct magic_set *ms file_locale_used, file_regex_t *rx,
strlcpy(old, setlocale(LC_CTYPE, NULL), sizeof(old));
(void)setlocale(LC_CTYPE, "C");
#endif
- rx->pat = pat;
-
- rx->rc = regcomp(&rx->rx, pat, flags);
+ int rc;
+ rc = regcomp(rx, pat, flags);
#ifdef USE_C_LOCALE
uselocale(old);
#else
(void)setlocale(LC_CTYPE, old);
#endif
- return rx->rc;
+ if (rc > 0 && (ms->flags & MAGIC_CHECK)) {
+ char errmsg[512];
+
+ (void)regerror(rc, rx, errmsg, sizeof(errmsg));
+ file_magerror(ms, "regex error %d for `%s', (%s)", rc, pat,
+ errmsg);
+ }
+ return rc;
}
protected int
@@ -695,11 +699,10 @@ file_regexec(struct magic_set *ms file_locale_used, file_regex_t *rx,
(void)setlocale(LC_CTYPE, "C");
#endif
int rc;
- assert(rx->rc == 0);
/* XXX: force initialization because glibc does not always do this */
if (nmatch != 0)
memset(pmatch, 0, nmatch * sizeof(*pmatch));
- rc = regexec(&rx->rx, str, nmatch, pmatch, eflags);
+ rc = regexec(rx, str, nmatch, pmatch, eflags);
#ifdef USE_C_LOCALE
uselocale(old);
#else
@@ -711,18 +714,7 @@ file_regexec(struct magic_set *ms file_locale_used, file_regex_t *rx,
protected void
file_regfree(file_regex_t *rx)
{
- if (rx->rc == 0)
- regfree(&rx->rx);
-}
-
-protected void
-file_regerror(file_regex_t *rx, int rc, struct magic_set *ms)
-{
- char errmsg[512];
-
- (void)regerror(rc, &rx->rx, errmsg, sizeof(errmsg));
- file_magerror(ms, "regex error %d for `%s', (%s)", rc, rx->pat,
- errmsg);
+ regfree(rx);
}
protected file_pushbuf_t *
diff --git a/src/softmagic.c b/src/softmagic.c
index 99010da3..064196d7 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -32,7 +32,7 @@
#include "file.h"
#ifndef lint
-FILE_RCSID("@(#)$File: softmagic.c,v 1.321 2022/03/19 19:52:09 christos Exp $")
+FILE_RCSID("@(#)$File: softmagic.c,v 1.322 2022/04/18 21:50:49 christos Exp $")
#endif /* lint */
#include "magic.h"
@@ -474,14 +474,13 @@ check_fmt(struct magic_set *ms, const char *fmt)
{
file_regex_t rx;
int rc, rv = -1;
+ const char* pat = "%[-0-9\\.]*s";
if (strchr(fmt, '%') == NULL)
return 0;
- rc = file_regcomp(ms, &rx, "%[-0-9\\.]*s", REG_EXTENDED|REG_NOSUB);
- if (rc) {
- file_regerror(&rx, rc, ms);
- } else {
+ rc = file_regcomp(ms, &rx, pat, REG_EXTENDED|REG_NOSUB);
+ if (rc == 0) {
rc = file_regexec(ms, &rx, fmt, 0, 0, 0);
rv = !rc;
}
@@ -2023,9 +2022,6 @@ alloc_regex(struct magic_set *ms, struct magic *m)
if (rc == 0)
return rx;
-fprintf(stderr, "regcomp %s %d\n", m->value.s, rc);
- file_regerror(rx, rc, ms);
- file_regfree(rx);
free(rx);
return NULL;
}
@@ -2268,12 +2264,9 @@ magiccheck(struct magic_set *ms, struct magic *m, file_regex_t **m_cache)
break;
default:
- file_regerror(rx, rc, ms);
- v = CAST(uint64_t, -1);
+ return -1;
break;
}
- if (v == CAST(uint64_t, -1))
- return -1;
break;
}
case FILE_USE: