Skip to content

Commit

Permalink
Ensure the substr from is always compatible with the size of string
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoholiveira committed Jun 29, 2022
1 parent b324023 commit b3253f2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
28 changes: 28 additions & 0 deletions jsonlogic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,3 +669,31 @@ func TestIssue51_example2(t *testing.T) {
expected := `false`
assert.JSONEq(t, expected, result.String())
}

func TestIssue52_example1(t *testing.T) {
data := strings.NewReader(`{}`)
logic := strings.NewReader(`{"substr": ["jsonlogic", -10]}`)

var result bytes.Buffer
err := Apply(logic, data, &result)
if err != nil {
t.Fatal(err)
}

expected := `"jsonlogic"`
assert.JSONEq(t, expected, result.String())
}

func TestIssue52_example2(t *testing.T) {
data := strings.NewReader(`{}`)
logic := strings.NewReader(`{"substr": ["jsonlogic", 10]}`)

var result bytes.Buffer
err := Apply(logic, data, &result)
if err != nil {
t.Fatal(err)
}

expected := `"jsonlogic"`
assert.JSONEq(t, expected, result.String())
}
5 changes: 5 additions & 0 deletions strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func substr(values interface{}) interface{} {
from = length + from
}

if from < 0 || from > length {
// case from is still negative, we must stop right now and return the original string
return string(runes)
}

if len(parsed) == 3 {
length = int(toNumber(parsed[2]))
}
Expand Down

0 comments on commit b3253f2

Please sign in to comment.