Skip to content

Commit 4954f79

Browse files
committed
split: fix memory corruption during chunk extraction
ASAN reported this error for: split -n2/3 /dev/null ERROR: AddressSanitizer: negative-size-param: (size=-1) #0 0x7f0d4c36951d in __asan_memmove (/lib64/libasan.so.2+0x8d51d) #1 0x404e06 in memmove /usr/include/bits/string3.h:59 #2 0x404e06 in bytes_chunk_extract src/split.c:988 #3 0x404e06 in main src/split.c:1626 Specifically there would be invalid memory access and subsequent processing if the chunk to be extracted was beyond the initial amount read from file (which is currently capped at 128KiB). This issue is not in a released version, only being introduced in commit v8.25-4-g62e7af0 * src/split.c (bytes_chunk_extract): The initial_read != SIZE_MAX should have been combined with && rather than ||, but also this condition is always true in this function so remove entirely. * tests/split/b-chunk.sh: Add a test case. Fixes http://bugs.gnu.org/25003
1 parent 68c5eec commit 4954f79

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/split.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -982,7 +982,7 @@ bytes_chunk_extract (uintmax_t k, uintmax_t n, char *buf, size_t bufsize,
982982
start = (k - 1) * (file_size / n);
983983
end = (k == n) ? file_size : k * (file_size / n);
984984

985-
if (initial_read != SIZE_MAX || start < initial_read)
985+
if (start < initial_read)
986986
{
987987
memmove (buf, buf + start, initial_read - start);
988988
initial_read -= start;

tests/split/b-chunk.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ split -n 10 /dev/null || fail=1
2525
test "$(stat -c %s x* | uniq -c | sed 's/^ *//; s/ /x/')" = "10x0" || fail=1
2626
rm -f x??
2727

28+
# When extracting K of N where N > file size
29+
# no data is extracted, and no files are written
30+
split -n 2/3 /dev/null || fail=1
31+
returns_ 1 stat x?? 2>/dev/null || fail=1
32+
2833
# Ensure --elide-empty-files is honored
2934
split -e -n 10 /dev/null || fail=1
30-
stat x?? 2>/dev/null && fail=1
35+
returns_ 1 stat x?? 2>/dev/null || fail=1
3136

3237
printf '1\n2\n3\n4\n5\n' > input || framework_failure_
3338

0 commit comments

Comments
 (0)