Skip to content

Commit

Permalink
Default value should not be nil (#2995)
Browse files Browse the repository at this point in the history
* query/query.go: default value should not be nil

types.Marshal expects vars to have non-nil values, this change
set empty string as the default value for a referenced aggregate
value. This should prevent nil-reference panic in types.Marshal.

* query/query.go: retract default value change it breaks aggs.

Value var aggregations use nil values for an optimization, so don't
assign a value to defaults.

* types/conversion.go: add exception in Marshal for default->string

The marshaling of default value vars to string causes panic because
the incoming value is nil. This affects a narrow case so we check
that in fact it's a default value var and marshal to empty string.
In theory nothing should be nil by the time we Marshal, but it's
an edge case.

* wrong issue referenced in comments

* types/conversion.go: general case in nil value mashaling

If we are trying to mashal a nil value, return the zero value
instead of crashing on nil reference.

* types/conversion.go: replace the original to-value

* types/conversion.go: added check for Marshal to nil object
  • Loading branch information
srfrog authored Feb 14, 2019
1 parent 790524c commit d9b1725
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions types/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,22 @@ func Convert(from Val, toID TypeID) (Val, error) {
}

func Marshal(from Val, to *Val) error {
if to == nil {
return x.Errorf("Invalid conversion %s to nil", from.Tid.Name())
}

fromID := from.Tid
toID := to.Tid
val := from.Value
res := &to.Value

// This is a default value from sg.fillVars, don't convert it's empty.
// Fixes issue #2980.
if val == nil {
*to = ValueForType(toID)
return nil
}

switch fromID {
case BinaryID:
vc := val.([]byte)
Expand Down

0 comments on commit d9b1725

Please sign in to comment.