Skip to content

Chained subscript read where the first index misses segfaults (null-fallback sentinel dereferenced as an array pointer) #526

Description

@mirchaemanuel

Summary

In a chained subscript read, a miss on the first index (so the miss fallback becomes the receiver of the second read) segfaults: for a refcounted element type the null fallback materializes the scalar sentinel 0x7fff_ffff_ffff_fffe instead of a null container, and the consuming outer read then loads the array length header from that sentinel address and faults.

A miss on the second index alone behaves correctly (warns, yields empty string, exits 0, heap clean) — only a first-index miss whose fallback is consumed as a container crashes.

Environment

  • elephc main e63d5d337 (v0.26.0-158), macOS arm64 (Apple Silicon); PHP 8.4.23 as behavioral reference.

Repro

<?php
$a = [];
for ($i = 0; $i < 3; $i++) { $a[] = ['kw', 'word' . $i]; }
// Miss on the first index: the second read consumes the miss fallback.
$x = (string) $a[7][1];
echo 'x=' . $x . "\n";
echo 'done' . "\n";
elephc miss_inner.php && ./miss_inner    # also reproduces with --heap-debug

Observed

Warning: Undefined array key 7

then SIGSEGV (exit 139). The two echo lines never run.

Expected (PHP 8.4.23)

Warning: Undefined array key 7 …
Warning: Trying to access array offset on null …
x=
done

exit 0.

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions