Fix and improve array and mdspan static analysis warnings by StephanTLavavej · Pull Request #4856 · microsoft/STL (original) (raw)

🗺️ Overview

Fixes DevCom-10342063 VSO-1804139 "False positive C28020 iterating over single element std::array". Thanks to Hwi-sung Im from the static analysis team for explaining how to fix this with _Ret_range_(==, _Size).

The issue is that we had SAL annotations on array::operator[] explaining its precondition, but static analysis didn't know that array<T, N>::size() always returns N. (By design, it doesn't do interprocedural analysis.) Adding a return annotation allows /analyze to understand loops that are guarded by arr.size().

Updating array then revealed that <mdspan> (which uses array extensively) should be updated accordingly.

📜 Commits