Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions codebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -1560,6 +1560,9 @@ func (p *CodeBuilder) refMember(typ types.Type, name string, argVal ast.Expr, sr

func (p *CodeBuilder) fieldRef(x ast.Expr, o *types.Struct, name string, src ast.Node, visited map[*types.Struct]none) bool {
var embed []*types.Var
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
Comment on lines +1563 to +1565
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing name[0] without first checking if name is empty will cause a panic if an empty string is provided as the field name. It's safer to add a length check to prevent this potential runtime error.

Suggested change
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
if len(name) > 0 && name[0] >= '0' && name[0] <= '9' { // tuple: ordinal field
name = "X_" + name
}

for i, n := 0, o.NumFields(); i < n; i++ {
fld := o.Field(i)
if fld.Name() == name {
Expand Down Expand Up @@ -1923,6 +1926,9 @@ func (p *CodeBuilder) btiMethod(

func (p *CodeBuilder) normalField(
o *types.Struct, name string, arg *Element, src ast.Node) MemberKind {
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
Comment on lines +1929 to +1931
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the other location, accessing name[0] without an empty string check can lead to a panic. Adding a length check will make this function more robust. Since this logic is repeated, consider creating a helper function to centralize it.

Suggested change
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
if len(name) > 0 && name[0] >= '0' && name[0] <= '9' { // tuple: ordinal field
name = "X_" + name
}

for i, n := 0, o.NumFields(); i < n; i++ {
fld := o.Field(i)
v := fld.Name()
Expand Down
3 changes: 3 additions & 0 deletions type_tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ func (p *CodeBuilder) tryUnpackTuple(lhs int) int {
// It checks both the original fields and the virtual fields (e.g. tuple
// fields).
func (p *CodeBuilder) LookupField(t *types.Struct, name string) int {
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
Comment on lines +205 to +207
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing name[0] without checking if name is empty will cause a panic if an empty string is passed. It's safer to add a length check to prevent this.

Suggested change
if c := name[0]; c >= '0' && c <= '9' { // tuple: ordinal field
name = "X_" + name
}
if len(name) > 0 && name[0] >= '0' && name[0] <= '9' { // tuple: ordinal field
name = "X_" + name
}

for i, n := 0, t.NumFields(); i < n; i++ {
if fld := t.Field(i); fld.Name() == name {
return i
Expand Down
8 changes: 8 additions & 0 deletions type_tuple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func TestTupleMember(t *testing.T) {
typf2 := types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(b, ok), false)
f2 := types.NewParam(token.NoPos, pkg.Types, "f2", typf2)
pkg.NewFunc(nil, "foo", types.NewTuple(a, f, f2), nil, false).BodyStart(pkg).
Val(ctxRef(pkg, "a")).
MemberRef("0").
Val(ctxRef(pkg, "a")).
MemberVal("1").
Assign(1).
EndStmt().
Val(ctxRef(pkg, "a")).
MemberRef("x").
Val(ctxRef(pkg, "a")).
Expand Down Expand Up @@ -92,6 +98,7 @@ func foo(a struct {
X_0 int
X_1 int
}, f func() (b Point), f2 func() (b Point, ok bool)) {
a.X_0 = a.X_1
a.X_0 = a.X_1
x := a
x2 := f()
Expand Down Expand Up @@ -120,6 +127,7 @@ func TestCodeBuilder_LookupField(t *testing.T) {
{"test1", types.NewStruct(newFields("a"), nil), "b", -1},
{"test2", types.NewStruct(newFields("a"), nil), "a", 0},
{"test3", p.NewTuple(true, newFields("a")...), "a", 0},
{"test4", p.NewTuple(true, newFields("a")...), "0", 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading