Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ func (c *Config) Exists(key string, findByPath ...bool) (ok bool) {
}
case []int: // is array(is from Set)
i, err := strconv.Atoi(k)
if err != nil || i >= len(typeData) {
if err != nil || i < 0 || i >= len(typeData) {
return false
}
item = typeData[i]
case []string: // is array(is from Set)
i, err := strconv.Atoi(k)
if err != nil || i >= len(typeData) {
if err != nil || i < 0 || i >= len(typeData) {
return false
}
item = typeData[i]
case []any: // is array(load from file)
i, err := strconv.Atoi(k)
if err != nil || i >= len(typeData) {
if err != nil || i < 0 || i >= len(typeData) {
return false
}
item = typeData[i]
Expand Down Expand Up @@ -214,7 +214,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value any, ok bool) {
i, err := strconv.Atoi(k)

// check slice index
if err != nil || len(typeData) <= i {
if err != nil || i < 0 || len(typeData) <= i {
ok = false
c.addError(err)
return
Expand All @@ -223,7 +223,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value any, ok bool) {
item = typeData[i]
case []string: // is array(is from Set)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) <= i {
if err != nil || i < 0 || len(typeData) <= i {
ok = false
c.addError(err)
return
Expand All @@ -232,7 +232,7 @@ func (c *Config) GetValue(key string, findByPath ...bool) (value any, ok bool) {
item = typeData[i]
case []any: // is array(load from file)
i, err := strconv.Atoi(k)
if err != nil || len(typeData) <= i {
if err != nil || i < 0 || len(typeData) <= i {
ok = false
c.addError(err)
return
Expand Down
21 changes: 21 additions & 0 deletions read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,3 +499,24 @@ func TestConfig_Duration_getter(t *testing.T) {
is.Eq(7*time.Second, c.Duration("missing", 7*time.Second))
is.Eq(time.Duration(0), c.Duration("missing"))
}

func TestConfig_GetValue_negativeIndex(t *testing.T) {
is := assert.New(t)

c := New("test-neg")
err := c.LoadData(map[string]any{
"arr": []any{"a", "b", "c"},
})
is.NoErr(err)

// A negative slice index must not panic; it is simply not found.
is.NotPanics(func() {
v, ok := c.GetValue("arr.-1")
is.False(ok)
is.Nil(v)
})

is.NotPanics(func() {
is.False(c.Exists("arr.-1"))
})
}
4 changes: 2 additions & 2 deletions testdata/issues59.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; exported at 2025-08-20 14:11:56
; exported at 2026-09-06 00:43:11

age = 123
baseKey = value
Expand All @@ -9,8 +9,8 @@ multiWords = hello world
name = app

[map1]
key = val
key1 = val1
key2 = val2
key = val


Loading