microsoft / bond

Bond is a cross-platform framework for working with schematized data. It supports cross-language de/serialization and powerful generic mechanisms for efficiently manipulating data. Bond is broadly used at Microsoft in high scale services.
MIT License
2.61k stars 321 forks source link

Avoid `stdext::checked_array_iterator` for modern MSVC #1205

Closed StephanTLavavej closed 3 weeks ago

StephanTLavavej commented 5 months ago

I work on MSVC's STL, and we'd like you to stop using one of our old non-Standard extensions, as we're working towards eventually removing it.

In VS 2005, MSVC's non-Standard extension stdext::checked_array_iterator was added to provide guaranteed bounds checking. At the same time, MSVC began emitting Microsoft "deprecation" warnings for using raw pointers as output iterators.

In VS 2017 15.8 (_MSC_VER 1915; see our handy decoder table), we stopped emitting those Microsoft "deprecation" warnings for using raw pointers as output iterators. Those warnings were incredibly annoying since they warned about perfectly correct, Standard-conforming code, and led users to generally disregard and silence such warnings. Also, the library was never the right place to implement them because of insufficient context. Instead, we began encouraging wider use of static analysis, which has sufficient context to emit better warnings about writing too many elements into a raw pointer. See our old blog post STL Features and Fixes in VS 2017 15.8 for more info. (This was internal MSVC-PR-120709.)

In VS 2022 17.8 (_MSC_VER 1938), we began emitting proper Microsoft deprecation warnings for the stdext::checked_array_iterator family, as this has been superseded not only by static analysis but also by C++20 std::span and downlevel-available gsl::span. ("Proper", in this case, because this is about actual Microsoft machinery.) These warnings were initially emitted for /std:c++17 and later, as a way to initially mitigate their impact while encouraging an ecosystem cleanup. (This was microsoft/STL#3818.)

In the upcoming VS 2022 17.11, we are unconditionally deprecating the stdext::checked_array_iterator family, as we're getting serious about eventual removal. (This was microsoft/STL#4605.)

In this PR, because Bond apparently supports compilers down to VS 2015, I'm refining the guard to use stdext::checked_array_iterator for MSVC older than VS 2017 15.8, where avoiding those "raw pointers as output iterators" warnings was necessary. For all recent MSVC versions, the portable (and still bounds-checked by your logic!) codepath is selected. In the long term, you may want to consider using C++20 std::span or taking a dependency on gsl::span, or similar changes (e.g. upgrading builtin arrays to std::arrays, etc.).