Skip to content

Commit

Permalink
gopls: add non nil if check around function result highlight
Browse files Browse the repository at this point in the history
The result of funcType will be nil, if a function does not return any
values. This caused an SIGSEGV before.

Fixes golang/go#65952

Change-Id: Ibf4ac3070744f42033504220f05b35a78c97d992
GitHub-Last-Rev: 74182b2
GitHub-Pull-Request: #480
Reviewed-on: https://go-review.googlesource.com/c/tools/+/567275
Reviewed-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
Patrick Pichler authored and findleyr committed Feb 27, 2024
1 parent bbdc81d commit c1f340a
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions gopls/internal/golang/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,45 +232,47 @@ findEnclosingFunc:
}
}

// Scan fields, either adding highlights according to the highlightIndexes
// computed above, or accounting for the cursor position within the result
// list.
// (We do both at once to avoid repeating the cumbersome field traversal.)
i := 0
findField:
for _, field := range funcType.Results.List {
for j, name := range field.Names {
if inNode(name) || highlightIndexes[i+j] {
result[posRange{name.Pos(), name.End()}] = unit{}
highlightIndexes[i+j] = true
break findField // found/highlighted the specific name
}
}
// If the cursor is in a field but not in a name (e.g. in the space, or
// the type), highlight the whole field.
//
// Note that this may not be ideal if we're at e.g.
//
// (x,‸y int, z int8)
//
// ...where it would make more sense to highlight only y. But we don't
// reach this function if not in a func, return, ident, or basiclit.
if inNode(field) || highlightIndexes[i] {
result[posRange{field.Pos(), field.End()}] = unit{}
highlightIndexes[i] = true
if inNode(field) {
for j := range field.Names {
if funcType.Results != nil {
// Scan fields, either adding highlights according to the highlightIndexes
// computed above, or accounting for the cursor position within the result
// list.
// (We do both at once to avoid repeating the cumbersome field traversal.)
i := 0
findField:
for _, field := range funcType.Results.List {
for j, name := range field.Names {
if inNode(name) || highlightIndexes[i+j] {
result[posRange{name.Pos(), name.End()}] = unit{}
highlightIndexes[i+j] = true
break findField // found/highlighted the specific name
}
}
break findField // found/highlighted the field
}
// If the cursor is in a field but not in a name (e.g. in the space, or
// the type), highlight the whole field.
//
// Note that this may not be ideal if we're at e.g.
//
// (x,‸y int, z int8)
//
// ...where it would make more sense to highlight only y. But we don't
// reach this function if not in a func, return, ident, or basiclit.
if inNode(field) || highlightIndexes[i] {
result[posRange{field.Pos(), field.End()}] = unit{}
highlightIndexes[i] = true
if inNode(field) {
for j := range field.Names {
highlightIndexes[i+j] = true
}
}
break findField // found/highlighted the field
}

n := len(field.Names)
if n == 0 {
n = 1
n := len(field.Names)
if n == 0 {
n = 1
}
i += n
}
i += n
}
}
}
Expand Down

0 comments on commit c1f340a

Please sign in to comment.