Skip to content

Commit fe61fd1

Browse files
committed
tree: avoid index-of-bounds panics in GetAnnotation in production
We've recently seen two panics when trying to access the Annotations container and hitting the out-of-bounds condition. The annotations are used to provide additional information about unresolved object names, so if we simply return `nil` on an invalid access, we'll keep on using the unresolved object name, which seems like a better option than process crash. In test builds we'll keep on crashing. Release note: None
1 parent 5741bf6 commit fe61fd1

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

pkg/sql/sem/tree/annotation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package tree
77

8+
import "github.com/cockroachdb/cockroach/pkg/util/buildutil"
9+
810
// AnnotationIdx is the 1-based index of an annotation. AST nodes that can
911
// be annotated store such an index (unique within that AST).
1012
type AnnotationIdx int32
@@ -22,6 +24,12 @@ func (n AnnotatedNode) GetAnnotation(ann *Annotations) interface{} {
2224
if n.AnnIdx == NoAnnotation {
2325
return nil
2426
}
27+
if (len(*ann) < int(n.AnnIdx) || int(n.AnnIdx) < 1) && !buildutil.CrdbTestBuild {
28+
// Avoid index-out-of bounds panics in production builds. The impact is
29+
// that the node will be treated as if it doesn't have the annotation,
30+
// which is better than the process crash.
31+
return nil
32+
}
2533
return ann.Get(n.AnnIdx)
2634
}
2735

0 commit comments

Comments
 (0)