Skip to content

fix(conf): reject pointer-to-map environment with a clear error - #987

Open
jaideeppyne wants to merge 3 commits into
expr-lang:masterfrom
jaideeppyne:fix/ptr-to-map-env-panic
Open

fix(conf): reject pointer-to-map environment with a clear error#987
jaideeppyne wants to merge 3 commits into
expr-lang:masterfrom
jaideeppyne:fix/ptr-to-map-env-panic

Conversation

@jaideeppyne

Copy link
Copy Markdown

Fixes #825

Passing a pointer to a map as the environment panicked at compile time:

panic: reflect: call of reflect.Value.Len on ptr to non-array Value

EnvWithCache selects the map branch using the dereferenced value's kind, but then read the length and keys from the original pointer value. This iterates the dereferenced value in the map branch, so a *map env is treated exactly like the map it points to — same strict mode and element types — matching the existing behaviour for pointer-to-struct environments.

Added a regression test under test/issues/825/ covering compile+run, strict unknown-name rejection, and element-type inference through the pointer. It panics on master and passes with this change. gofmt, go vet, and the full suite are green.

EnvWithCache selected the map branch using the dereferenced value's
kind, but then read the map length and keys from the original (pointer)
value. Passing a *map as the env therefore panicked with:

    reflect: call of reflect.Value.Len on ptr to non-array Value

Iterate the dereferenced value in the map branch so a pointer-to-map env
is treated exactly like the map it points to (same strict mode and
element types), matching the existing behaviour for pointer-to-struct
environments.

Fixes expr-lang#825

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: jaideeppyne <jaideeppyne1997@gmail.com>

@sanmaxdev sanmaxdev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and focused regression tests pass. Before this lands, please reconcile the behavior with #825: the issue is labeled wontfix, and the maintainer's direction was to reject *map input with an error. This patch instead supports non-nil pointers while nil pointers still panic as an unknown type. A decision on support versus rejection seems needed, along with a nil-pointer test for the chosen contract.

Per expr-lang#825 (wontfix for deref-support): rather than dereferencing a
*map env, reject it with a descriptive message instead of the opaque
"reflect: call of reflect.Value.Len on ptr to non-array Value" panic.
Passing a map by value is unaffected.
@jaideeppyne jaideeppyne changed the title fix(conf): deref pointer-to-map environment instead of panicking fix(conf): reject pointer-to-map environment with a clear error Aug 24, 2026
Detect pointer-to-map by type so a nil *map is rejected with the same
message as a non-nil one, instead of the generic "unknown type" panic.
Pointer-to-struct envs are unaffected.
@jaideeppyne

Copy link
Copy Markdown
Author

Reconciled with #825's direction: this now rejects *map rather than supporting it, so the wontfix stands — the only change is turning the opaque reflect: call of reflect.Value.Len on ptr to non-array Value panic into a clear environment must be a map, not a pointer to a map: *map[...].

On the nil-pointer case you flagged: I detect the pointer-to-map by type now, so a nil *map is rejected with the same clear message instead of falling through to the generic unknown type panic. Added a TestIssue825_NilPointer case alongside the non-nil one, and confirmed pointer-to-struct envs are unaffected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic when using pointer-type map as environment

2 participants