Skip to content

Commit abce7d3

Browse files
committed
add rules.go to catch a possible panic
1 parent 1be6401 commit abce7d3

File tree

10 files changed

+46
-44
lines changed

10 files changed

+46
-44
lines changed

extract/parameter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func ParameterFromBlock(block *terraform.Block) (*types.Parameter, hcl.Diagnosti
159159
if ctyType != cty.NilType && pVal.Value.Type().Equals(cty.String) {
160160
// TODO: Wish we could support more types, but only string types are
161161
// allowed.
162+
//nolint:gocritic // string type asserted
162163
valStr := pVal.Value.AsString()
163164
// Apply validations to the parameter value
164165
for _, v := range p.Validations {
@@ -328,6 +329,7 @@ func requiredString(block *terraform.Block, key string) (string, *hcl.Diagnostic
328329
return "", diag
329330
}
330331

332+
// nolint:gocritic // string type asserted
331333
return tyVal.AsString(), nil
332334
}
333335

@@ -400,6 +402,7 @@ func nullableString(block *terraform.Block, key string) *string {
400402
return nil
401403
}
402404

405+
//nolint:gocritic // string type asserted
403406
str := val.AsString()
404407
return &str
405408
}
@@ -414,6 +417,7 @@ func optionalString(block *terraform.Block, key string) string {
414417
return ""
415418
}
416419

420+
//nolint:gocritic // string type asserted
417421
return val.AsString()
418422
}
419423

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ require (
9898
github.com/pion/udp v0.1.4 // indirect
9999
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
100100
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
101+
github.com/quasilyte/go-ruleguard/dsl v0.3.22 // indirect
101102
github.com/rivo/uniseg v0.4.7 // indirect
102103
github.com/robfig/cron/v3 v3.0.1 // indirect
103104
github.com/samber/lo v1.49.1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,8 @@ github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:Om
11181118
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
11191119
github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
11201120
github.com/prometheus/client_model v0.3.0/go.mod h1:LDGWKZIo7rky3hgvBe+caln+Dr3dPggB5dvjtD7w9+w=
1121+
github.com/quasilyte/go-ruleguard/dsl v0.3.22 h1:wd8zkOhSNr+I+8Qeciml08ivDt1pSXe60+5DqOpCjPE=
1122+
github.com/quasilyte/go-ruleguard/dsl v0.3.22/go.mod h1:KeCP03KrjuSO0H1kTuZQCWlQPulDV6YMIXmpQss17rU=
11211123
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
11221124
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
11231125
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=

hclext/merge.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package hclext
22

3-
import "github.com/zclconf/go-cty/cty"
3+
import (
4+
"github.com/zclconf/go-cty/cty"
5+
)
46

57
func MergeObjects(a, b cty.Value) cty.Value {
68
output := make(map[string]cty.Value)
@@ -9,6 +11,11 @@ func MergeObjects(a, b cty.Value) cty.Value {
911
output[key] = val
1012
}
1113
b.ForEachElement(func(key, val cty.Value) (stop bool) {
14+
// TODO: Should this error be captured?
15+
if key.Type() != cty.String {
16+
return true
17+
}
18+
//nolint:gocritic // string type asserted above
1219
k := key.AsString()
1320
old := output[k]
1421
if old.IsKnown() && isNotEmptyObject(old) && isNotEmptyObject(val) {

hclext/references.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func CreateDotReferenceFromTraversal(traversals ...hcl.Traversal) string {
6767
case hcl.TraverseIndex:
6868
switch {
6969
case part.Key.Type().Equals(cty.String):
70+
//nolint:gocritic // string type asserted above
7071
refParts = append(refParts, fmt.Sprintf("[%s]", part.Key.AsString()))
7172
case part.Key.Type().Equals(cty.Number):
7273
idx, _ := part.Key.AsBigFloat().Int64()

hclext/vartypes.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

paramhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func parameterContextsEvalHook(input Input) func(ctx *tfcontext.Context, blocks
3232
continue // Ignore the errors at this point
3333
}
3434

35+
//nolint:gocritic // string type asserted
3536
name := nameVal.AsString()
3637
var value cty.Value
3738
pv, ok := input.RichParameterValue(name)

scripts/rules.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,22 @@
1414
//
1515
// Note: don't forget to run `golangci-lint cache clean`!
1616
package gorules
17+
18+
import "github.com/quasilyte/go-ruleguard/dsl"
19+
20+
// asStringsIsDangerous checks for the use of AsString() on cty.Value.
21+
// This function can panic if not used correctly, so the cty.Type must be known
22+
// before calling. Ignore this lint if you are confident in your usage.
23+
func asStringsIsDangerous(m dsl.Matcher) {
24+
m.Import("github.com/zclconf/go-cty/cty")
25+
26+
m.Match(
27+
`$v.AsString()`,
28+
).
29+
Where(
30+
m["v"].Type.Is("cty.Value") &&
31+
// Ignore unit tests
32+
!m.File().Name.Matches(`_test\.go$`),
33+
).
34+
Report("'AsStrings()' can result in a panic if the type is not known. Ignore this linter with caution")
35+
}

types/primitive.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ import (
77
"github.com/zclconf/go-cty/cty"
88
)
99

10+
func CtyValueStringDefault(def string, val cty.Value) string {
11+
str, err := CtyValueString(val)
12+
if err != nil {
13+
return def
14+
}
15+
return str
16+
}
17+
1018
// CtyValueString converts a cty.Value to a string.
1119
// It supports only primitive types - bool, number, and string.
1220
// As a special case, it also supports map[string]interface{} with key "value".
@@ -47,6 +55,7 @@ func CtyValueString(val cty.Value) (string, error) {
4755
case cty.Number:
4856
return val.AsBigFloat().String(), nil
4957
case cty.String:
58+
//nolint:gocritic // string type asserted above
5059
return val.AsString(), nil
5160
// We may also have a map[string]interface{} with key "value".
5261
case cty.Map(cty.String):

types/value.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (s HCLString) AsString() string {
8080
if s.Valid() && s.Value.IsKnown() {
8181
switch {
8282
case s.Value.Type().Equals(cty.String):
83+
//nolint:gocritic // string type asserted
8384
return s.Value.AsString()
8485
case s.Value.Type().Equals(cty.Number):
8586
// TODO: Float vs Int?

0 commit comments

Comments
 (0)