Skip to content

Commit 61932ff

Browse files
committed
Make convert_libmagic_pattern() return a zend_string*
1 parent dccf074 commit 61932ff

File tree

5 files changed

+54
-58
lines changed

5 files changed

+54
-58
lines changed

ext/fileinfo/libmagic.patch

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
22
--- libmagic.orig/apprentice.c 2019-02-20 03:35:27.000000000 +0100
3-
+++ libmagic/apprentice.c 2020-08-21 17:02:45.104803600 +0200
3+
+++ libmagic/apprentice.c 2020-08-25 15:13:14.549715400 +0200
44
@@ -29,6 +29,8 @@
55
* apprentice - make one pass through /etc/magic, learning its secrets.
66
*/
@@ -635,19 +635,19 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
635635
- if (rc) {
636636
- if (ms->flags & MAGIC_CHECK)
637637
- file_regerror(&rx, rc, ms);
638-
+ zval pattern;
638+
+ zend_string *pattern;
639639
+ int options = 0;
640640
+ pcre_cache_entry *pce;
641641
+
642-
+ convert_libmagic_pattern(&pattern, m->value.s, strlen(m->value.s), options);
642+
+ pattern = convert_libmagic_pattern(m->value.s, strlen(m->value.s), options);
643643
+
644-
+ if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
645-
+ zval_ptr_dtor_nogc(&pattern);
644+
+ if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
645+
+ zend_string_release(pattern);
646646
+ return -1;
647647
}
648648
- file_regfree(&rx);
649649
- return rc ? -1 : 0;
650-
+ zval_ptr_dtor_nogc(&pattern);
650+
+ zend_string_release(pattern);
651651
+
652652
+ return 0;
653653
}
@@ -974,7 +974,7 @@ diff -u libmagic.orig/apprentice.c libmagic/apprentice.c
974974
}
975975
diff -u libmagic.orig/ascmagic.c libmagic/ascmagic.c
976976
--- libmagic.orig/ascmagic.c 2019-05-07 04:27:11.000000000 +0200
977-
+++ libmagic/ascmagic.c 2020-08-21 17:02:41.887436300 +0200
977+
+++ libmagic/ascmagic.c 2020-08-24 14:22:57.162970900 +0200
978978
@@ -51,7 +51,7 @@
979979
#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
980980
|| (x) == 0x85 || (x) == '\f')
@@ -1640,7 +1640,7 @@ diff -u libmagic.orig/elfclass.h libmagic/elfclass.h
16401640
CAST(int, elf_getu16(swap, elfhdr.e_shstrndx)),
16411641
diff -u libmagic.orig/encoding.c libmagic/encoding.c
16421642
--- libmagic.orig/encoding.c 2019-04-15 18:48:41.000000000 +0200
1643-
+++ libmagic/encoding.c 2020-08-21 17:02:41.890255300 +0200
1643+
+++ libmagic/encoding.c 2020-08-24 14:22:57.172802500 +0200
16441644
@@ -44,14 +44,14 @@
16451645
#include <stdlib.h>
16461646

@@ -1830,7 +1830,7 @@ diff -u libmagic.orig/encoding.c libmagic/encoding.c
18301830
return 0;
18311831
diff -u libmagic.orig/file.h libmagic/file.h
18321832
--- libmagic.orig/file.h 2019-05-07 04:27:11.000000000 +0200
1833-
+++ libmagic/file.h 2020-08-21 17:02:41.894108600 +0200
1833+
+++ libmagic/file.h 2020-08-25 15:11:06.907695900 +0200
18341834
@@ -33,18 +33,9 @@
18351835
#ifndef __file_h__
18361836
#define __file_h__
@@ -2004,7 +2004,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
20042004
size_t *);
20052005
protected size_t file_pstring_length_size(const struct magic *);
20062006
protected size_t file_pstring_get_length(const struct magic *, const char *);
2007-
@@ -513,34 +498,13 @@
2007+
@@ -513,34 +498,12 @@
20082008
size_t);
20092009
#endif /* __EMX__ */
20102010

@@ -2037,12 +2037,11 @@ diff -u libmagic.orig/file.h libmagic/file.h
20372037
- int);
20382038
-protected void file_regfree(file_regex_t *);
20392039
-protected void file_regerror(file_regex_t *, int, struct magic_set *);
2040-
+public void
2041-
+convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options);
2040+
+public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options);
20422041

20432042
typedef struct {
20442043
char *buf;
2045-
@@ -550,28 +514,13 @@
2044+
@@ -550,28 +513,13 @@
20462045
protected file_pushbuf_t *file_push_buffer(struct magic_set *);
20472046
protected char *file_pop_buffer(struct magic_set *, file_pushbuf_t *);
20482047

@@ -2073,7 +2072,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
20732072
size_t strlcat(char *, const char *, size_t);
20742073
#endif
20752074
#ifndef HAVE_STRCASESTR
2076-
@@ -587,39 +536,6 @@
2075+
@@ -587,39 +535,6 @@
20772076
#ifndef HAVE_ASCTIME_R
20782077
char *asctime_r(const struct tm *, char *);
20792078
#endif
@@ -2113,7 +2112,7 @@ diff -u libmagic.orig/file.h libmagic/file.h
21132112

21142113
#if defined(HAVE_MMAP) && defined(HAVE_SYS_MMAN_H) && !defined(QUICK)
21152114
#define QUICK
2116-
@@ -645,6 +561,18 @@
2115+
@@ -645,6 +560,18 @@
21172116
#else
21182117
#define FILE_RCSID(id)
21192118
#endif
@@ -2427,7 +2426,7 @@ diff -u libmagic.orig/fsmagic.c libmagic/fsmagic.c
24272426
case S_IFSOCK:
24282427
diff -u libmagic.orig/funcs.c libmagic/funcs.c
24292428
--- libmagic.orig/funcs.c 2019-05-07 04:27:11.000000000 +0200
2430-
+++ libmagic/funcs.c 2020-08-07 20:46:25.164794200 +0200
2429+
+++ libmagic/funcs.c 2020-08-25 15:11:06.872908800 +0200
24312430
@@ -31,87 +31,80 @@
24322431
#endif /* lint */
24332432

@@ -2686,25 +2685,25 @@ diff -u libmagic.orig/funcs.c libmagic/funcs.c
26862685
- nm++;
26872686
- }
26882687
- rv = nm;
2689-
+ zval patt;
2688+
+ zend_string *pattern;
26902689
+ uint32_t opts = 0;
26912690
+ pcre_cache_entry *pce;
26922691
+ zend_string *res;
26932692
+ zend_string *repl;
26942693
+ size_t rep_cnt = 0;
26952694
+
26962695
+ opts |= PCRE2_MULTILINE;
2697-
+ convert_libmagic_pattern(&patt, (char*)pat, strlen(pat), opts);
2698-
+ if ((pce = pcre_get_compiled_regex_cache_ex(Z_STR(patt), 0)) == NULL) {
2699-
+ zval_ptr_dtor(&patt);
2696+
+ pattern = convert_libmagic_pattern((char*)pat, strlen(pat), opts);
2697+
+ if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
2698+
+ zend_string_release(pattern);
27002699
+ rep_cnt = -1;
27012700
+ goto out;
27022701
}
27032702
-out:
27042703
- file_regfree(&rx);
27052704
- return rv;
27062705
-}
2707-
+ zval_ptr_dtor(&patt);
2706+
+ zend_string_release(pattern);
27082707

27092708
-protected int
27102709
-file_regcomp(file_regex_t *rx, const char *pat, int flags)
@@ -3263,7 +3262,7 @@ diff -u libmagic.orig/magic.c libmagic/magic.c
32633262
public const char *
32643263
magic_error(struct magic_set *ms)
32653264
diff -u libmagic.orig/magic.h libmagic/magic.h
3266-
--- libmagic.orig/magic.h 2020-08-21 17:03:04.181135100 +0200
3265+
--- libmagic.orig/magic.h 2020-08-25 15:19:32.346097700 +0200
32673266
+++ libmagic/magic.h 2020-04-07 22:25:10.548560600 +0200
32683267
@@ -124,6 +124,7 @@
32693268

@@ -3275,7 +3274,7 @@ diff -u libmagic.orig/magic.h libmagic/magic.h
32753274

32763275
diff -u libmagic.orig/print.c libmagic/print.c
32773276
--- libmagic.orig/print.c 2019-03-12 21:43:05.000000000 +0100
3278-
+++ libmagic/print.c 2020-08-07 18:30:21.155794100 +0200
3277+
+++ libmagic/print.c 2020-08-22 19:28:45.849356800 +0200
32793278
@@ -28,6 +28,7 @@
32803279
/*
32813280
* print.c - debugging printout routines
@@ -3468,7 +3467,7 @@ diff -u libmagic.orig/readcdf.c libmagic/readcdf.c
34683467
if (i != -1)
34693468
diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
34703469
--- libmagic.orig/softmagic.c 2019-05-17 04:24:59.000000000 +0200
3471-
+++ libmagic/softmagic.c 2020-04-26 00:43:35.734037100 +0200
3470+
+++ libmagic/softmagic.c 2020-08-25 15:15:35.784945600 +0200
34723471
@@ -43,6 +43,10 @@
34733472
#include <time.h>
34743473
#include "der.h"
@@ -3641,12 +3640,11 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
36413640
return rv;
36423641

36433642
case FILE_USE:
3644-
@@ -1926,6 +1904,61 @@
3643+
@@ -1926,6 +1904,60 @@
36453644
return file_strncmp(a, b, len, flags);
36463645
}
36473646

3648-
+public void
3649-
+convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options)
3647+
+public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options)
36503648
+{
36513649
+ int i, j;
36523650
+ zend_string *t;
@@ -3697,20 +3695,20 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
36973695
+ ZSTR_VAL(t)[j]='\0';
36983696
+ ZSTR_LEN(t) = j;
36993697
+
3700-
+ ZVAL_NEW_STR(pattern, t);
3698+
+ return t;
37013699
+}
37023700
+
37033701
private int
37043702
magiccheck(struct magic_set *ms, struct magic *m)
37053703
{
3706-
@@ -2104,65 +2137,77 @@
3704+
@@ -2104,65 +2136,77 @@
37073705
break;
37083706
}
37093707
case FILE_REGEX: {
37103708
- int rc;
37113709
- file_regex_t rx;
37123710
- const char *search;
3713-
+ zval pattern;
3711+
+ zend_string *pattern;
37143712
+ uint32_t options = 0;
37153713
+ pcre_cache_entry *pce;
37163714

@@ -3729,11 +3727,11 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
37293727
+ options |= PCRE2_CASELESS;
37303728
+ }
37313729
+
3732-
+ convert_libmagic_pattern(&pattern, (char *)m->value.s, m->vallen, options);
3730+
+ pattern = convert_libmagic_pattern((char *)m->value.s, m->vallen, options);
37333731
+
37343732
+ l = v = 0;
3735-
+ if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
3736-
+ zval_ptr_dtor(&pattern);
3733+
+ if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
3734+
+ zend_string_release(pattern);
37373735
+ return -1;
37383736
} else {
37393737
- regmatch_t pmatch;
@@ -3769,7 +3767,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
37693767
+
37703768
+ if (Z_LVAL(retval) < 0) {
37713769
+ zval_ptr_dtor(&subpats);
3772-
+ zval_ptr_dtor(&pattern);
3770+
+ zend_string_release(pattern);
37733771
+ return -1;
37743772
+ } else if ((Z_LVAL(retval) > 0) && (Z_TYPE(subpats) == IS_ARRAY)) {
37753773
+ /* Need to fetch global match which equals pmatch[0] */
@@ -3796,7 +3794,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
37963794
+ } else {
37973795
+error_out:
37983796
+ zval_ptr_dtor(&subpats);
3799-
+ zval_ptr_dtor(&pattern);
3797+
+ zend_string_release(pattern);
38003798
+ return -1;
38013799
+ }
38023800
} else {
@@ -3825,7 +3823,7 @@ diff -u libmagic.orig/softmagic.c libmagic/softmagic.c
38253823
- break;
38263824
}
38273825
+ zval_ptr_dtor(&subpats);
3828-
+ zval_ptr_dtor(&pattern);
3826+
+ zend_string_release(pattern);
38293827
}
38303828
- file_regfree(&rx);
38313829
- if (v == CAST(uint64_t, -1))

ext/fileinfo/libmagic/apprentice.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2590,17 +2590,17 @@ getvalue(struct magic_set *ms, struct magic *m, const char **p, int action)
25902590
return -1;
25912591
}
25922592
if (m->type == FILE_REGEX) {
2593-
zval pattern;
2593+
zend_string *pattern;
25942594
int options = 0;
25952595
pcre_cache_entry *pce;
25962596

2597-
convert_libmagic_pattern(&pattern, m->value.s, strlen(m->value.s), options);
2597+
pattern = convert_libmagic_pattern(m->value.s, strlen(m->value.s), options);
25982598

2599-
if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
2600-
zval_ptr_dtor_str(&pattern);
2599+
if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
2600+
zend_string_release(pattern);
26012601
return -1;
26022602
}
2603-
zval_ptr_dtor_str(&pattern);
2603+
zend_string_release(pattern);
26042604

26052605
return 0;
26062606
}

ext/fileinfo/libmagic/file.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ protected void buffer_init(struct buffer *, int, const zend_stat_t *,
503503
protected void buffer_fini(struct buffer *);
504504
protected int buffer_fill(const struct buffer *);
505505

506-
public void
507-
convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options);
506+
public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options);
508507

509508
typedef struct {
510509
char *buf;

ext/fileinfo/libmagic/funcs.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,21 +516,21 @@ file_printedlen(const struct magic_set *ms)
516516
protected int
517517
file_replace(struct magic_set *ms, const char *pat, const char *rep)
518518
{
519-
zval patt;
519+
zend_string *pattern;
520520
uint32_t opts = 0;
521521
pcre_cache_entry *pce;
522522
zend_string *res;
523523
zend_string *repl;
524524
size_t rep_cnt = 0;
525525

526526
opts |= PCRE2_MULTILINE;
527-
convert_libmagic_pattern(&patt, (char*)pat, strlen(pat), opts);
528-
if ((pce = pcre_get_compiled_regex_cache_ex(Z_STR(patt), 0)) == NULL) {
529-
zval_ptr_dtor(&patt);
527+
pattern = convert_libmagic_pattern((char*)pat, strlen(pat), opts);
528+
if ((pce = pcre_get_compiled_regex_cache_ex(pattern, 0)) == NULL) {
529+
zend_string_release(pattern);
530530
rep_cnt = -1;
531531
goto out;
532532
}
533-
zval_ptr_dtor(&patt);
533+
zend_string_release(pattern);
534534

535535
repl = zend_string_init(rep, strlen(rep), 0);
536536
res = php_pcre_replace_impl(pce, NULL, ms->o.buf, strlen(ms->o.buf), repl, -1, &rep_cnt);

ext/fileinfo/libmagic/softmagic.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,8 +1904,7 @@ file_strncmp16(const char *a, const char *b, size_t len, uint32_t flags)
19041904
return file_strncmp(a, b, len, flags);
19051905
}
19061906

1907-
public void
1908-
convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options)
1907+
public zend_string* convert_libmagic_pattern(char *val, size_t len, uint32_t options)
19091908
{
19101909
int i, j;
19111910
zend_string *t;
@@ -1956,7 +1955,7 @@ convert_libmagic_pattern(zval *pattern, char *val, size_t len, uint32_t options)
19561955
ZSTR_VAL(t)[j]='\0';
19571956
ZSTR_LEN(t) = j;
19581957

1959-
ZVAL_NEW_STR(pattern, t);
1958+
return t;
19601959
}
19611960

19621961
private int
@@ -2137,7 +2136,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
21372136
break;
21382137
}
21392138
case FILE_REGEX: {
2140-
zval pattern;
2139+
zend_string *pattern;
21412140
uint32_t options = 0;
21422141
pcre_cache_entry *pce;
21432142

@@ -2147,11 +2146,11 @@ magiccheck(struct magic_set *ms, struct magic *m)
21472146
options |= PCRE2_CASELESS;
21482147
}
21492148

2150-
convert_libmagic_pattern(&pattern, (char *)m->value.s, m->vallen, options);
2149+
pattern = convert_libmagic_pattern((char *)m->value.s, m->vallen, options);
21512150

21522151
l = v = 0;
2153-
if ((pce = pcre_get_compiled_regex_cache(Z_STR(pattern))) == NULL) {
2154-
zval_ptr_dtor(&pattern);
2152+
if ((pce = pcre_get_compiled_regex_cache(pattern)) == NULL) {
2153+
zend_string_release(pattern);
21552154
return -1;
21562155
} else {
21572156
/* pce now contains the compiled regex */
@@ -2172,7 +2171,7 @@ magiccheck(struct magic_set *ms, struct magic *m)
21722171

21732172
if (Z_LVAL(retval) < 0) {
21742173
zval_ptr_dtor(&subpats);
2175-
zval_ptr_dtor(&pattern);
2174+
zend_string_release(pattern);
21762175
return -1;
21772176
} else if ((Z_LVAL(retval) > 0) && (Z_TYPE(subpats) == IS_ARRAY)) {
21782177
/* Need to fetch global match which equals pmatch[0] */
@@ -2199,14 +2198,14 @@ magiccheck(struct magic_set *ms, struct magic *m)
21992198
} else {
22002199
error_out:
22012200
zval_ptr_dtor(&subpats);
2202-
zval_ptr_dtor(&pattern);
2201+
zend_string_release(pattern);
22032202
return -1;
22042203
}
22052204
} else {
22062205
v = 1;
22072206
}
22082207
zval_ptr_dtor(&subpats);
2209-
zval_ptr_dtor(&pattern);
2208+
zend_string_release(pattern);
22102209
}
22112210
break;
22122211
}

0 commit comments

Comments
 (0)