镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

nswitch() segfaults with no value/output pairs #87

Closed

Description

@davidbudzynski

Summary

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

  1. davidbudzynski commented on Oct 30, 2026

    @davidbudzynski
    CollaboratorAuthor

    Fixed in #96.

  2. added a commit that references this issue on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions