Skip to content

Commit d91aeef

Browse files
committed
pr: fix read from invalid memory with tabs in separator
This was detected with: echo a > a; pr "-S$(printf "\t\t\t")" a -m a > /dev/null Resulting in ASAN triggering: ==================================================== ERROR: AddressSanitizer: global-buffer-overflow READ of size 1 at 0x00000041b622 thread T0 #0 0x40506a in print_sep_string ../src/pr.c:2241 #1 0x407ec4 in read_line ../src/pr.c:2493 #2 0x40985c in print_page ../src/pr.c:1802 #3 0x40985c in print_files ../src/pr.c:1618 #4 0x4036e0 in main ../src/pr.c:1136 * src/pr.c (init_parameters): Ensure we only override the specified separator when it's a single tab, thus matching the calculated separator length. * tests/pr/pr-tests.pl: Add a test case. * NEWS: Mention the fix.
1 parent ca99c52 commit d91aeef

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ GNU coreutils NEWS -*- outline -*-
3535
nl now resets numbering for each page section rather than just for each page.
3636
[This bug was present in "the beginning".]
3737

38+
pr now handles specified separator strings containing tabs correctly.
39+
Previously it would have output random data from memory.
40+
[This bug was detected with ASAN and present in "the beginning".]
41+
3842
sort -h -k now works even in locales that use blank as thousands separator.
3943

4044
stty --help no longer outputs extraneous gettext header lines

src/pr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ init_parameters (int number_of_files)
12331233
}
12341234
/* It's rather pointless to define a TAB separator with column
12351235
alignment */
1236-
else if (!join_lines && *col_sep_string == '\t')
1236+
else if (!join_lines && col_sep_length == 1 && *col_sep_string == '\t')
12371237
col_sep_string = column_separator;
12381238

12391239
truncate_lines = true;

tests/pr/pr-tests.pl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@
467467
{IN=>{3=>"x\ty\tz\n"}},
468468
{OUT=>join("\t", qw(a b c m n o x y z)) . "\n"} ];
469469

470+
# This resulted in reading invalid memory before coreutils-8.26
471+
push @Tests,
472+
['asan1', "-m -S'\t\t\t' -t",
473+
{IN=>{1=>"a\n"}},
474+
{IN=>{2=>"a\n"}},
475+
{OUT=>"a\t\t\t\t \t\t\ta\n"} ];
476+
470477
@Tests = triple_test \@Tests;
471478

472479
my $save_temps = $ENV{DEBUG};

0 commit comments

Comments
 (0)