Skip to content

setlevels() doesn't handle missing or out-of-bounds factor values #7595

Description

@sindribaldur

I'm aware this is not something you should be doing but I guess crashing is not the best way out.

setattr(factor(c(1:3), levels = c("1", "2")), "levels", c("1", "1"))

System info (where it consistently crashes R).

> version
               _
platform       x86_64-w64-mingw32
arch           x86_64
os             mingw32
crt            ucrt
system         x86_64, mingw32
status
major          4
minor          5.1
year           2025
month          06
day            13
svn rev        88306
language       R
version.string R version 4.5.1 (2025-06-13 ucrt)
nickname       Great Square Root

Activity

  1. added theissue type on Jan 14, 2026
  2. aitap commented on Jan 14, 2026

    @aitap
    Member

    Thank you for the report! Confirmed on R-devel / gcc -fsanitize=address,undefined / Linux and latest master:

    wrappers.c:48:54: runtime error: signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'
    Error in data.table::setattr(factor(c(1:3), levels = c("1", "2")), "levels",  : 
      attempt access index 2147483647/2 in STRING_ELT
    Execution halted
    

    For your future reports, sessionInfo() would be more informative than just version, even with sensitive parts redacted.

  3. changed the title [-]setattr() crashes with duplicate levels[/-] [+]`setlevels()` doesn't handle missing factor values[/+] on Jan 14, 2026
  4. changed the title [-]`setlevels()` doesn't handle missing factor values[/-] [+]`setlevels()` doesn't handle missing or out-of-bounds factor values[/+] on Jan 14, 2026
  5. added this to the 1.18.2 milestone on Jan 14, 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

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions