Skip to content

Reflective methods should not create symbols for nonexistent elements #5169

@headius

Description

@headius

Currently our logic for reflective methods like Object#instance_variable_get, Struct#[], etc goes like this:

  • If it's a symbol, look up the value
  • If it's a string, turn it into a hard symbol and look up the value

The logic we need to match in MRI is as follows:

  • If it's a symbol, look up the value
  • If it's a string, see if a symbol exists for that string
  • If there is no such symbol, do not create it and do not search (return nil)

MRI only creates symbols for nonexistent key strings passed to the write methods, like instance_variable_set. Our logic has the following issues that should be addressed in some way before 9.2 (or if possible, we should match MRI's logic exactly):

  • Hard symbols will leak. This means arbitrary strings passed into reflective read methods could cause a symbol leak.
  • Soft symbols pollute the symbol table but would not introduce a symbol leak.

In general MRI will assume that if a string is passed in, and no such symbol exists, no such entry can exists in whatever is being searched.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions