Panic when using pointer-type map as environment
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Bắt đầu trong conf/env.go quanh dòng 25 và các dòng 38-43, sau đó đọc internal/deref/deref.go ở dòng 39 để so sánh giá trị đã dereference với giá trị được dùng cho các thao tác trên map. Tái hiện ví dụ con trỏ tới map và xác nhận rằng expr.Compile không còn panic, trong khi các môi trường map trực tiếp vẫn tiếp tục hoạt động.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
📝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?
- Ngôn ngữ chính
- Go
- Star
- 8k
- Fork
- 529
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của expr-lang/expr
-
docs needed
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 62/100
-
`find`, `findIndex`, `first` and `get` are type-checked as the element type where they return nil Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 65/100
-
Release latest master branch Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 35/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
Tất cả issue của expr-lang/expr
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
bug group: validation priority: low
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
codecheckers/chekhov#51 ·
-
Creating worktree from an existing remote branch with a slash in it, has unexpected behaviour Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100