Skip to content

Commit

Permalink
Fix a potential panic (#393)
Browse files Browse the repository at this point in the history
* Fix a potential panic

* Update changelog

* Update changelog
  • Loading branch information
TomWright authored Mar 14, 2024
1 parent 0652b8e commit b4b2f65
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Nothing yet.

### Fixed

- Fixed a bug that could cause a panic.

## [v2.7.0] - 2024-03-14

Expand Down
9 changes: 9 additions & 0 deletions internal/command/select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,15 @@ d.e.f`)),
}
})

t.Run("Issue 392 panic", runTest(
[]string{"-r", "csv", "--csv-comma", ";", "-w", "json", "equal([], )"},
[]byte(`Hello;There;
1;2;`),
[]byte("false\n"),
nil,
nil,
))

t.Run("Issue346", func(t *testing.T) {
t.Run("Select null or default string", runTest(
[]string{"-r", "json", "orDefault(foo,string(nope))"},
Expand Down
19 changes: 12 additions & 7 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ func (v Value) Index(i int) Value {
func (v Value) Append() Value {
currentLen := v.Len()
newLen := currentLen + 1
updatedSlice := reflect.MakeSlice(sliceInterfaceType, newLen, newLen)

updatedSlice := reflect.MakeSlice(reflect.TypeOf(v.Interface()), newLen, newLen)
// copy all existing elements into updatedSlice.
// this leaves the last element empty.
for i := 0; i < currentLen; i++ {
Expand All @@ -311,18 +312,22 @@ func (v Value) Append() Value {
)
}

v.FirstAddressable().Set(updatedSlice)
firstAddressable := v.FirstAddressable()
firstAddressable.Set(updatedSlice)

// This code was causing a panic...
// It doesn't seem necessary. Leaving here for reference in-case it was needed.
// See https://github.com/TomWright/dasel/issues/392
// Set the last element to uninitialised.
updatedSlice.Index(currentLen).Set(
v.Index(currentLen).asUninitialised().Value,
)
//updatedSlice.Index(currentLen).Set(
// v.Index(currentLen).asUninitialised().Value,
//)

return v
}

var sliceInterfaceType = reflect.TypeOf([]interface{}{})
var mapStringInterfaceType = reflect.TypeOf(map[string]interface{}{})
var sliceInterfaceType = reflect.TypeFor[[]any]()
var mapStringInterfaceType = reflect.TypeFor[map[string]interface{}]()

var UninitialisedPlaceholder interface{} = "__dasel_not_found__"

Expand Down

0 comments on commit b4b2f65

Please sign in to comment.