Skip to content

Commit f2456ad

Browse files
authored
file: fix hex escapes in magic file (#1051)
A magicfile string can contain common C escapes, defined in ESC table, but also hex and octal ones. Octal escapes do not need a leading zero. Hex escapes need a leading "x". Previously the code attempted to handle hex and octal in one hit, but hex values were incorrectly being fed to builtin oct() function.
1 parent 3833c47 commit f2456ad

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

bin/file

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ sub readMagicEntry {
499499
$$MF[1] = $line;
500500
return length($thisDepth);
501501
}
502-
elsif (@$entry) {
502+
elsif (defined $entry && @{ $entry }) {
503503
# already have an entry. this is not a continuation.
504504
# save this line for the next call and exit.
505505
$$MF[1] = $line;
@@ -636,7 +636,8 @@ sub readMagicLine {
636636
$testval = $line;
637637

638638
# do octal/hex conversion
639-
$testval =~ s/\\([x0-7][0-7]?[0-7]?)/chr(oct($1))/eg;
639+
$testval =~ s/\\x([0-9A-Fa-f]{1,2})/chr(hex($1))/eg;
640+
$testval =~ s/\\([0-7][0-7]?[0-7]?)/chr(oct($1))/eg;
640641

641642
# do single char escapes
642643
$testval =~ s/\\(.)/$ESC{$1}||$1/eg;

0 commit comments

Comments
 (0)