You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
nswitch() segfaults with no value/output pairs #87
nswitch() aborts the R process when called with no value/output pairs. It should raise a normal R error.
Reproduce
library(kit)
nswitch(1:3)
Result on R 4.6.2 (macOS):
*** caught segfault ***
address 0x8000002, cause 'invalid permissions'
Traceback:
1: nswitch(1:3)
An irrecoverable exception occurred. R is aborting now ...
nswitch(1:3, default = 0L) crashes the same way.
Cause
R/call.R:12 passes list(...) to CnswitchR. With no pairs, that list is length 0.
In src/nswitch.c, n = length(args) is 0, so the even-length check passes, and the code immediately reads PTR_ETL(args, 1) (line 42) and PTR_ETL(args, 0) (line 46) without checking that args has at least two elements. PTR_ETL on an empty list is out of bounds.
Expected
A clear R error, for example:
Error: '...' must contain at least one value/output pair.
or the same error vswitch() gives for zero-length values:
Error: Argument 'values' cannot be zero-length vector.
Fix
In nswitchR(), after n = n / 2, add:
if (n==0) {
error("Argument '...' must contain at least one value/output pair.");
}
before any PTR_ETL(args, ...) access.
This is pre-existing and unrelated to #86; I found it while reviewing #74.
Activity
davidbudzynski commentedon Oct 30, 2026
Fixed in #96.
fix: error instead of segfault on empty nswitch args (#87)
Merge pull request #96 from davidbudzynski/fix/87-nswitch-empty-args