News for hackers. Stuff that matters.
Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @12:36PM (#41111088)
std::vector [] has had bounds checking since forever if you set the correct compiler flag. Since they aren't using it this is a choice, presumably they prefer the speed gain.
Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Tuesday July 30, 2024 @12:43PM (#41111167)
You mean _GLIBCXX_DEBUG? It's got some issues. Linux only, it doesn't always work [1] and it's all or nothing. What's really needed is the ability to selectively opt-out on a per-instantiation level so very hot paths can keep the needed performance whilst all the rest gets opted into safety checks.

Microsoft has this:

https://learn.microsoft.com/en-us/cpp/standard-library/safe-...

but it doesn't seem to actually make std::vector[] safe.

It's frustrating that low hanging fruit like this doesn't get harvested.

[1] "although there are precondition checks for some string operations, e.g. operator[], they will not always be run when using the char and wchar_t specializations (std::string and std::wstring)."

Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @08:36PM (#41115471)
With MSVC you can use _CONTAINER_DEBUG_LEVEL=1 to get a fast bounds check that can be used in release builds. Or just use it in development to catch errors.
Re:DARPA: Translating All C to Rust (Tractor) by mike_hearn on Wednesday July 31, 2024 @03:58AM (#41117205)
Interesting thanks. Seems the reason I couldn't find anything on that is because it's internal only and not a feature you're actually meant to use?

https://github.com/microsoft/STL/issues/586

> We talked about this at the weekly maintainer meeting and decided that we're not comfortable enough with the (lack of) design of this feature to begin documenting it for wide usage.

Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:05AM (#41118463)
What you want should be _ITERATOR_DEBUG_LEVEL instead, that is the public macro for bounds checking configuration.
Re:DARPA: Translating All C to Rust (Tractor) by Calavar on Tuesday July 30, 2024 @12:51PM (#41111293)
As far as I am aware, the standard doesn't mandate bounds checking for std::vector::operator[] and probably never will for backwards compatibility reasons. Most standard library implementations have opt-out std::vector[] bounds checking in unoptimized builds, but not in optimized builds.

I tried a toy example with GCC [1], Clang [2], and MSVC [3], and none of them emit bounds checks with basic optimization flags.

[1] https://godbolt.org/z/W5e3n5oWM

[2] https://godbolt.org/z/Pe8nPPvEd

[3] https://godbolt.org/z/YTdv3nabn

Re:DARPA: Translating All C to Rust (Tractor) by TinkersW on Tuesday July 30, 2024 @08:38PM (#41115482)
As I said you need the correct flag set.. MSVC use _CONTAINER_DEBUG_LEVEL=1 and it can be used in release. They have had this feature since 2010 or so, though the flag name has changed.
Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:06AM (#41118474)
The correct name is _ITERATOR_DEBUG_LEVEL.
Re:DARPA: Translating All C to Rust (Tractor) by pjmlp on Wednesday July 31, 2024 @08:09AM (#41118496)
Add a "#define _ITERATOR_DEBUG_LEVEL 1" on top for VC++.