Skip to content

Commit 0281945

Browse files
committed
Fix bug #65106: PHP fails to compile ext/fileinfo
Avoid ambiguous 0x tokens (as specified in C standards), by using octal.
1 parent b25347b commit 0281945

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

ext/fileinfo/create_data_file.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44
<?php
55
/*--- Initialization of our translation table ---*/
66

7+
// By default, everything gets mapped to its \o notation
8+
// (not \x, because by C's norm, \x eats as many chars as possible, while \o stops at exactly 3;
9+
// thus \x0ABACK_2_MALICE is interpreted as hex \x0ABAC (which overflows) followed by string K_2_MALICE,
10+
// while \o0120 is unambiguously a CR followed by digit 0).
711
for ($i = 0; $i < 0x100; ++$i) {
8-
$map[chr($i)] = sprintf('\x%02X', $i);
12+
$map[chr($i)] = sprintf('\%03o', $i);
913
}
1014
// \0 is a shortcut for \x00; as the majority of the input file is \0's,
1115
// we divide the generated file's size by nearly 2 (30 MB -> 16 MB).
1216
$map[chr(0)] = '\0';
17+
$map["\n"] = '\n';
18+
// Displayable ASCII can be output as is: strings for file types will appear readable.
19+
for ($i = ord(' '); $i < 0x7F; ++$i) {
20+
$map[chr($i)] = chr($i);
21+
}
22+
// … Except digits following a \0: \012 will be interpreted as octal 012, and not \0 followed by 12.
23+
// Then we have to express \0 in a full unambiguous 3-chars octal code.
24+
for ($i = ord('0'); $i <= ord('9'); ++$i) {
25+
$map[chr(0).chr($i)] = '\000'.chr($i);
26+
}
27+
// … Except " and \ because we enclose the result into quotes and escape with \.
28+
$map['"'] = '\"';
29+
$map['\\'] = '\\\\';
1330

1431
/*--- File generation ---*/
1532

@@ -24,7 +41,7 @@
2441

2542
echo 'const unsigned char php_magic_database[' . count($chunks) . '][' . CHUNK_SIZE . "] = {\n";
2643
foreach ($chunks as $chunk) {
27-
echo '"' . strtr($chunk, $map) . "\",\n";
44+
echo '"' . strtr($chunk, $map) . '",' . "\n";
2845
}
2946
echo "};\n";
3047
?>

0 commit comments

Comments
 (0)