Skip to content

Panic when using pointer-type map as environment #825

@yawnBright

Description

@yawnBright

📝Bug Description

The conf.Env() function panics when passed a pointer to a map (e.g., *map[string]any ) instead of a direct map value.
However, from the code perspective, the pointer type is intended to be supported.

// file: conf/env.go // line: 25 ... v := reflect.ValueOf(env) d := deref.Value(v) ... // file: internal/deref/deref.go // line: 39 func Value(v reflect.Value) reflect.Value { for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface { if v.IsNil() { return v	} v = v.Elem()	} return v }

🌍 Environment

  • expr version : Latest (master branch)
  • Go version : 1.24
  • OS : All platforms

🔄 Steps to Reproduce

package main import (     "github.com/expr-lang/expr" ) func main() {     m := map[string]any{"foo"42}     env := &m     _, err := expr.Compile("foo > 0", expr.Env(env))      if err != nil {         panic(err)     } }

✅ Expected Behavior

The function should handle pointer-to-map types gracefully by properly dereferencing the pointer before performing map operations. 🎯

❌ Actual Behavior

panic: reflect: call of reflect.Value.Len on ptr to non-array Value goroutine 1 [running]: reflect.Value.lenNonSlice({0x1011f8ca0?, 0x140000a4378?, 0x1400009c998?}) /usr/local/go/src/reflect/value.go:1780 +0x220 reflect.Value.Len(...) /usr/local/go/src/reflect/value.go:1761 github.com/expr-lang/expr/conf.Env({0x1011f8ca0, 0x140000a4378}) /Users/bytedance/go/pkg/mod/github.com/expr-lang/expr@v1.17.6/conf/env.go:38 +0x208 github.com/expr-lang/expr/conf.(*Config).WithEnv(0x140000de000, {0x1011f8ca0?, 0x140000a4378?}) /Users/bytedance/go/pkg/mod/github.com/expr-lang/expr@v1.17.6/conf/config.go:64 +0x58 main.main.Env.func1(0x101219660?) /Users/bytedance/go/pkg/mod/github.com/expr-lang/expr@v1.17.6/expr.go:31 +0x24 github.com/expr-lang/expr.Compile({0x101187670, 0x7}, {0x1400009cf08, 0x1, 0x1400005c738?}) /Users/bytedance/go/pkg/mod/github.com/expr-lang/expr@v1.17.6/expr.go:211 +0x17c main.main() /Users/bytedance/projects/go/rule/main.go:12 +0xc8 exit status 2 

🔍 Root Cause Analysis

In conf/env.go , lines 38-43, the code uses:

  • v.Len() ❌ fails on pointer types
  • v.MapKeys() ❌ fails on pointer types
  • v.MapIndex(key) ❌ fails on pointer types

The fix is to use the dereferenced value d (from deref.Value(v) ) for these operations. 💡

I have a working implementation ready that addresses this issue. Would you like me to submit a PR for this fix?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions