Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cty: AsBigFloat does a shallow copy #114

Merged
merged 1 commit into from Aug 17, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 1 addition & 3 deletions cty/value_ops.go
Expand Up @@ -1283,9 +1283,7 @@ func (val Value) AsBigFloat() *big.Float {
}

// Copy the float so that callers can't mutate our internal state
ret := *(val.v.(*big.Float))

return &ret
return new(big.Float).Copy(val.v.(*big.Float))
}

// AsValueSlice returns a []cty.Value representation of a non-null, non-unknown
Expand Down
13 changes: 13 additions & 0 deletions cty/value_ops_test.go
Expand Up @@ -3432,3 +3432,16 @@ func TestHasWhollyKnownType(t *testing.T) {
})
}
}

func TestFloatCopy(t *testing.T) {
// ensure manipulating floats does not modify the cty.Value
v := NumberFloatVal(1.9)
vString := v.GoString()

// do something that will modify the internal big.Float mantissa
v.AsBigFloat().SetInt64(1)

if vString != v.GoString() {
t.Fatalf("original value changed from %s to %#v", vString, v)
}
}