Add fail-safe to prevent test crashes in nullability verification
Tests with nested templates were crashing because we do not handle nested template arguments properly yet. In particular, we cannot use the current indexing strategy on nullability vectors when dealing with nested templates. This patch introduces a fail-safe to prevent out-of-bound crashes. Now, tests with nested templates may still fail (specifically, through false negatives), but should not crash. We will handle the false negatives in a future CL.
PiperOrigin-RevId: 496126213
diff --git a/nullability_verification/pointer_nullability_analysis.cc b/nullability_verification/pointer_nullability_analysis.cc
index eb8a639..6a8c2db 100644
--- a/nullability_verification/pointer_nullability_analysis.cc
+++ b/nullability_verification/pointer_nullability_analysis.cc
@@ -182,6 +182,12 @@
for (auto TA : TemplateArgs.take_front(ArgIndex)) {
PointerCount += countPointersInType(TA);
}
+ // TODO: Correctly handle the indexing of nested templates (e.g.
+ // PointerNullabilityTest.MemberFunctionTemplateOfTemplateStruct), then
+ // remove this fallback.
+ if (TemplateArgs.size() <= ArgIndex) {
+ return {};
+ }
unsigned SliceSize = countPointersInType(TemplateArgs[ArgIndex]);
if (BaseNullabilityAnnotations.size() < PointerCount + SliceSize) {
// TODO: Currently, BaseNullabilityAnnotations can be erroneously empty
diff --git a/nullability_verification/pointer_nullability_verification_test.cc b/nullability_verification/pointer_nullability_verification_test.cc
index ee2c0d1..c21ba8a 100644
--- a/nullability_verification/pointer_nullability_verification_test.cc
+++ b/nullability_verification/pointer_nullability_verification_test.cc
@@ -2040,9 +2040,9 @@
};
void target(S<int> p) {
- // *p.getTN2<0, int *, 1>(); // TODO: fix crash
- // *p.getTN2<2147483647, int * _Nonnull, -2147483647>(); // TODO: fix
- // crash *p.getTN2<4, int * _Nullable, 4>(); // TODO: fix crash
+ *p.getTN2<0, int *, 1>();
+ *p.getTN2<2147483647, int *_Nonnull, -2147483647>();
+ *p.getTN2<4, int *_Nullable, 4>(); // TODO: fix false negative.
}
)cc");
}