Skip to content

Commit c1f340a

Browse files
Patrick Pichlerfindleyr
Patrick Pichler
authored andcommitted
gopls: add non nil if check around function result highlight
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bbdc81d commit c1f340a

File tree

1 file changed

+36
-34
lines changed

1 file changed

+36
-34
lines changed

gopls/internal/golang/highlight.go

+36-34
Original file line numberDiff line numberDiff line change
@@ -232,45 +232,47 @@ findEnclosingFunc:
232232
}
233233
}
234234

235-
// Scan fields, either adding highlights according to the highlightIndexes
236-
// computed above, or accounting for the cursor position within the result
237-
// list.
238-
// (We do both at once to avoid repeating the cumbersome field traversal.)
239-
i := 0
240-
findField:
241-
for _, field := range funcType.Results.List {
242-
for j, name := range field.Names {
243-
if inNode(name) || highlightIndexes[i+j] {
244-
result[posRange{name.Pos(), name.End()}] = unit{}
245-
highlightIndexes[i+j] = true
246-
break findField // found/highlighted the specific name
247-
}
248-
}
249-
// If the cursor is in a field but not in a name (e.g. in the space, or
250-
// the type), highlight the whole field.
251-
//
252-
// Note that this may not be ideal if we're at e.g.
253-
//
254-
// (x,‸y int, z int8)
255-
//
256-
// ...where it would make more sense to highlight only y. But we don't
257-
// reach this function if not in a func, return, ident, or basiclit.
258-
if inNode(field) || highlightIndexes[i] {
259-
result[posRange{field.Pos(), field.End()}] = unit{}
260-
highlightIndexes[i] = true
261-
if inNode(field) {
262-
for j := range field.Names {
235+
if funcType.Results != nil {
236+
// Scan fields, either adding highlights according to the highlightIndexes
237+
// computed above, or accounting for the cursor position within the result
238+
// list.
239+
// (We do both at once to avoid repeating the cumbersome field traversal.)
240+
i := 0
241+
findField:
242+
for _, field := range funcType.Results.List {
243+
for j, name := range field.Names {
244+
if inNode(name) || highlightIndexes[i+j] {
245+
result[posRange{name.Pos(), name.End()}] = unit{}
263246
highlightIndexes[i+j] = true
247+
break findField // found/highlighted the specific name
264248
}
265249
}
266-
break findField // found/highlighted the field
267-
}
250+
// If the cursor is in a field but not in a name (e.g. in the space, or
251+
// the type), highlight the whole field.
252+
//
253+
// Note that this may not be ideal if we're at e.g.
254+
//
255+
// (x,‸y int, z int8)
256+
//
257+
// ...where it would make more sense to highlight only y. But we don't
258+
// reach this function if not in a func, return, ident, or basiclit.
259+
if inNode(field) || highlightIndexes[i] {
260+
result[posRange{field.Pos(), field.End()}] = unit{}
261+
highlightIndexes[i] = true
262+
if inNode(field) {
263+
for j := range field.Names {
264+
highlightIndexes[i+j] = true
265+
}
266+
}
267+
break findField // found/highlighted the field
268+
}
268269

269-
n := len(field.Names)
270-
if n == 0 {
271-
n = 1
270+
n := len(field.Names)
271+
if n == 0 {
272+
n = 1
273+
}
274+
i += n
272275
}
273-
i += n
274276
}
275277
}
276278
}

0 commit comments

Comments
 (0)