Commit 3c4d410
Fix ordertab() segfault when nunique is incorrectly supplied (r-lib#207)
* Fix: ordertab() segfaults when nunique is smaller than actual
The ordertab() C function was susceptible to a buffer overflow when the
'nunique' parameter passed from R was smaller than the actual number of
unique values in the data. This led to intermittent segfaults due to
out-of-bounds writes to the 'ret' array.
This commit introduces a bounds check in the C implementation of
ordertab() (r_ram_integer64_ordertab_asc). If the number of unique
values encountered exceeds the allocated size of the 'ret' vector,
the loop is now terminated early, preventing any out-of-bounds writes
and thus eliminating the segfault. The function will now return a
truncated result in such cases, consistent with R's usual handling
of mismatched lengths.
A new test case has been added to test-sortuse64.R to specifically
target this scenario, using gctorture() to reliably trigger the
condition. This test now passes, confirming the fix.
Updated NEWS.md to reflect this bug fix.
* To taste
* tighten test
* comment purpose
* citation1 parent d1ce776 commit 3c4d410
3 files changed
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
775 | 775 | | |
776 | 776 | | |
777 | 777 | | |
| 778 | + | |
| 779 | + | |
778 | 780 | | |
779 | 781 | | |
780 | 782 | | |
781 | 783 | | |
782 | 784 | | |
783 | 785 | | |
| 786 | + | |
784 | 787 | | |
785 | 788 | | |
786 | 789 | | |
787 | 790 | | |
788 | 791 | | |
| 792 | + | |
789 | 793 | | |
790 | 794 | | |
791 | 795 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
0 commit comments