Skip to content

Commit a817465

Browse files
committed
Merge branch 'PHP-5.3' into PHP-5.4
* PHP-5.3: - fix test for 5.11 Fix bug #61504, potential vuln. in fileinfo. update to 5.11
2 parents 50bdc48 + d0e3289 commit a817465

26 files changed

+75986
-41256
lines changed

ext/fileinfo/data_file.c

Lines changed: 72204 additions & 39177 deletions
Large diffs are not rendered by default.

ext/fileinfo/libmagic.patch

Lines changed: 624 additions & 688 deletions
Large diffs are not rendered by default.

ext/fileinfo/libmagic/apprentice.c

Lines changed: 272 additions & 81 deletions
Large diffs are not rendered by default.

ext/fileinfo/libmagic/apptype.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "file.h"
2828

2929
#ifndef lint
30-
FILE_RCSID("@(#)$File: apptype.c,v 1.11 2009/02/04 18:24:32 christos Exp $")
30+
FILE_RCSID("@(#)$File: apptype.c,v 1.13 2011/09/07 21:57:15 christos Exp $")
3131
#endif /* lint */
3232

3333
#include <stdlib.h>
@@ -72,6 +72,7 @@ file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
7272
if (fwrite(buf, 1, nb, fp) != nb) {
7373
file_error(ms, errno, "cannot write tmp file `%s'",
7474
path);
75+
(void)fclose(fp);
7576
return -1;
7677
}
7778
(void)fclose(fp);

ext/fileinfo/libmagic/ascmagic.c

Lines changed: 50 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
* SUCH DAMAGE.
2727
*/
2828
/*
29-
* ASCII magic -- file types that we know based on keywords
30-
* that can appear anywhere in the file.
29+
* ASCII magic -- try to detect text encoding.
3130
*
3231
* Extensively modified by Eric Fischer <enf@pobox.com> in July, 2000,
3332
* to handle character codes other than ASCII on a unified basis.
@@ -36,7 +35,7 @@
3635
#include "file.h"
3736

3837
#ifndef lint
39-
FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
38+
FILE_RCSID("@(#)$File: ascmagic.c,v 1.84 2011/12/08 12:38:24 rrt Exp $")
4039
#endif /* lint */
4140

4241
#include "magic.h"
@@ -47,13 +46,11 @@ FILE_RCSID("@(#)$File: ascmagic.c,v 1.75 2009/02/03 20:27:51 christos Exp $")
4746
#ifdef HAVE_UNISTD_H
4847
#include <unistd.h>
4948
#endif
50-
#include "names.h"
5149

5250
#define MAXLINELEN 300 /* longest sane line length */
5351
#define ISSPC(x) ((x) == ' ' || (x) == '\t' || (x) == '\r' || (x) == '\n' \
5452
|| (x) == 0x85 || (x) == '\f')
5553

56-
private int ascmatch(const unsigned char *, const unichar *, size_t);
5754
private unsigned char *encode_utf8(unsigned char *, size_t, unichar *, size_t);
5855
private size_t trim_nuls(const unsigned char *, size_t);
5956

@@ -71,7 +68,8 @@ trim_nuls(const unsigned char *buf, size_t nbytes)
7168
}
7269

7370
protected int
74-
file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
71+
file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes,
72+
int text)
7573
{
7674
unichar *ubuf = NULL;
7775
size_t ulen;
@@ -88,29 +86,24 @@ file_ascmagic(struct magic_set *ms, const unsigned char *buf, size_t nbytes)
8886

8987
/* If file doesn't look like any sort of text, give up. */
9088
if (file_encoding(ms, buf, nbytes, &ubuf, &ulen, &code, &code_mime,
91-
&type) == 0) {
89+
&type) == 0)
9290
rv = 0;
93-
goto done;
94-
}
91+
else
92+
rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
93+
type, text);
9594

96-
rv = file_ascmagic_with_encoding(ms, buf, nbytes, ubuf, ulen, code,
97-
type);
98-
99-
done:
100-
if (ubuf)
101-
free(ubuf);
95+
free(ubuf);
10296

10397
return rv;
10498
}
10599

106100
protected int
107101
file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
108102
size_t nbytes, unichar *ubuf, size_t ulen, const char *code,
109-
const char *type)
103+
const char *type, int text)
110104
{
111105
unsigned char *utf8_buf = NULL, *utf8_end;
112106
size_t mlen, i;
113-
const struct names *p;
114107
int rv = -1;
115108
int mime = ms->flags & MAGIC_MIME;
116109

@@ -125,6 +118,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
125118
int n_lf = 0;
126119
int n_cr = 0;
127120
int n_nel = 0;
121+
int executable = 0;
128122

129123
size_t last_line_end = (size_t)-1;
130124
int has_long_lines = 0;
@@ -140,54 +134,21 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
140134
goto done;
141135
}
142136

143-
/* Convert ubuf to UTF-8 and try text soft magic */
144-
/* malloc size is a conservative overestimate; could be
145-
improved, or at least realloced after conversion. */
146-
mlen = ulen * 6;
147-
utf8_buf = emalloc(mlen);
148-
149-
if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen)) == NULL)
150-
goto done;
151-
if ((rv = file_softmagic(ms, utf8_buf, (size_t)(utf8_end - utf8_buf),
152-
TEXTTEST)) != 0)
153-
goto done;
154-
else
155-
rv = -1;
156-
157-
/* look for tokens from names.h - this is expensive! */
158-
if ((ms->flags & MAGIC_NO_CHECK_TOKENS) != 0)
159-
goto subtype_identified;
160-
161-
i = 0;
162-
while (i < ulen) {
163-
size_t end;
164-
165-
/* skip past any leading space */
166-
while (i < ulen && ISSPC(ubuf[i]))
167-
i++;
168-
if (i >= ulen)
169-
break;
170-
171-
/* find the next whitespace */
172-
for (end = i + 1; end < nbytes; end++)
173-
if (ISSPC(ubuf[end]))
174-
break;
175-
176-
/* compare the word thus isolated against the token list */
177-
for (p = names; p < names + NNAMES; p++) {
178-
if (ascmatch((const unsigned char *)p->name, ubuf + i,
179-
end - i)) {
180-
subtype = types[p->type].human;
181-
subtype_mime = types[p->type].mime;
182-
goto subtype_identified;
183-
}
184-
}
137+
if ((ms->flags & MAGIC_NO_CHECK_SOFT) == 0) {
138+
/* Convert ubuf to UTF-8 and try text soft magic */
139+
/* malloc size is a conservative overestimate; could be
140+
improved, or at least realloced after conversion. */
141+
mlen = ulen * 6;
142+
utf8_buf = emalloc(mlen);
185143

186-
i = end;
144+
if ((utf8_end = encode_utf8(utf8_buf, mlen, ubuf, ulen))
145+
== NULL)
146+
goto done;
147+
if ((rv = file_softmagic(ms, utf8_buf,
148+
(size_t)(utf8_end - utf8_buf), TEXTTEST, text)) == 0)
149+
rv = -1;
187150
}
188151

189-
subtype_identified:
190-
191152
/* Now try to discover other details about the file. */
192153
for (i = 0; i < ulen; i++) {
193154
if (ubuf[i] == '\n') {
@@ -230,7 +191,7 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
230191
goto done;
231192
}
232193
if (mime) {
233-
if ((mime & MAGIC_MIME_TYPE) != 0) {
194+
if (!file_printedlen(ms) && (mime & MAGIC_MIME_TYPE) != 0) {
234195
if (subtype_mime) {
235196
if (file_printf(ms, "%s", subtype_mime) == -1)
236197
goto done;
@@ -240,6 +201,28 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
240201
}
241202
}
242203
} else {
204+
if (file_printedlen(ms)) {
205+
switch (file_replace(ms, " text$", ", ")) {
206+
case 0:
207+
switch (file_replace(ms, " text executable$",
208+
", ")) {
209+
case 0:
210+
if (file_printf(ms, ", ") == -1)
211+
goto done;
212+
case -1:
213+
goto done;
214+
default:
215+
executable = 1;
216+
break;
217+
}
218+
break;
219+
case -1:
220+
goto done;
221+
default:
222+
break;
223+
}
224+
}
225+
243226
if (file_printf(ms, "%s", code) == -1)
244227
goto done;
245228

@@ -251,6 +234,10 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
251234
if (file_printf(ms, " %s", type) == -1)
252235
goto done;
253236

237+
if (executable)
238+
if (file_printf(ms, " executable") == -1)
239+
goto done;
240+
254241
if (has_long_lines)
255242
if (file_printf(ms, ", with very long lines") == -1)
256243
goto done;
@@ -313,22 +300,6 @@ file_ascmagic_with_encoding(struct magic_set *ms, const unsigned char *buf,
313300
return rv;
314301
}
315302

316-
private int
317-
ascmatch(const unsigned char *s, const unichar *us, size_t ulen)
318-
{
319-
size_t i;
320-
321-
for (i = 0; i < ulen; i++) {
322-
if (s[i] != us[i])
323-
return 0;
324-
}
325-
326-
if (s[i])
327-
return 0;
328-
else
329-
return 1;
330-
}
331-
332303
/*
333304
* Encode Unicode string as UTF-8, returning pointer to character
334305
* after end of string, or NULL if an invalid character is found.

0 commit comments

Comments
 (0)