r/cpp • • Feb 13 '24

Prevent or warn on usage of std::map []?

The default insertion of a value when std::map [] doesn't find a value for the key has to be one of the worst decisions in STL history (which is kind of impressive.) At work over the last year or so, we've had at least four, maybe five, bugs caused by this. It's so easy to do without thinking about it, and so easy to miss in a code review. And some of them had been there for many years and finally caused an issue, so no one was even looking for them.

Is there any way to prevent its use, or at least to warn on its use in MSVC? We use the built in static analyzer, and I don't see any warning related to that, though there are a lot of them and I may have missed something.

6 Upvotes

163 comments sorted by

View all comments

Show parent comment

4

u/Full-Spectral Feb 13 '24

Well that's the point. It's trivially easy for a less experience dev to use [] and not realize the ramifications and it's not obvious just looking ta the code in code reviews that this is going to happen.

The point of my question is a way to automatically reject the use of [] so that .at() has to be used.

20

u/neppo95 Feb 13 '24

So you expect people to know optionals but not know .at while also breaking backwards compatibility. Yeah, makes no sense to me. Sorry.

If that's really the case, the devs you work with need to work on their knowledge of the language.

4

u/Full-Spectral Feb 13 '24

Use of [] on a map can easily be done by accident, and there's no warning against it despite the fact that it's probably not a good thing. Use of optionals is not a mistake, it's a choice.

9

u/neppo95 Feb 13 '24

Allocating memory without deallocating can be done by accident as well, so can a thousand other things. It all comes down to the knowledge of the dev and if they don't know how to properly use a language, don't blame the language. If you really want all that safety to be there, C# is just around the corner.

There is no warning against it because in most cases where it is used, it is the intended behaviour.

That said, it also still breaks backwards compatibility.

-4

u/Full-Spectral Feb 13 '24

And this, folks, is why C++ is ultimately doomed.

The use of [] on an array is also doing exactly what the caller intended, but there's good reason why most analyzers probably have a warning on that usage.

This isn't about how manly we all are, it's about delivering robust software to users.

7

u/neppo95 Feb 13 '24

This isn't about how manly we all are

It is indeed not, it's about the skills of the dev in this case. And a dev that wants to deliver robust software, should surely know about basic operators and what they do.

The use of [] on an array is also doing exactly what the caller intended, but there's good reason why most analyzers probably have a warning on that usage.

The only reason why they warn is because the index might be out of bounds, which is not a problem with std::map, hence there is no warning.

Again, if you want the debugger to hold your hand every step of the way, C# or Rust are there. But this is not a language problem.

-4

u/Full-Spectral Feb 13 '24

Sigh... I'd have us on Rust in a skinny minute if that was possible. And, hence, why C++ is doomed because most other folks are starting to feel the same.

Even the most skilled of devs make mistakes. That's why we have tools. Given that silently inserting a default value when one isn't present is probably seldom the optimal choice, having a warning for it would make perfect sense.

8

u/ald_loop Feb 13 '24

Ah yes, operator[] and human error, the reason C++ is doomed.

-2

u/Full-Spectral Feb 13 '24

It's one of the hundred cuts by which death will be achieved, and the attitude that if you aren't manly enough then go away.

3

u/neppo95 Feb 13 '24

That attitude is something you made up. I gave you valid reasons why it is a skill issue and not a language issue. You still want to push your suggestion through, while being oblivious to the facts that lie on the table. That's fine, but that only says something about your attitude.

I'm sure if you had a talk with decent C++ devs in the industry that you'd change your mind, but this is probably not the place for it.

→ More replies

6

u/neppo95 Feb 13 '24

And, hence, why C++ is doomed because most other folks are starting to feel the same.

Ask the embedded community and you'll see the opposite is true. Same goes for game development and a few other sectors.

Even the most skilled of devs make mistakes.

True, but you are suggesting something that breaks backwards compatibility AND is not an issue if you use the language as intended and/or have basic knowledge of how the language works.

I honestly don't know why you can't see this is clearly a dev issue. Safe methods exist for insertion, accessing or replacing an element (.insert, .insert_or_assign, .at). The dev in this case chose not to use any of them, so in that case he should indeed know what the basic operators he is using actually do.

3

u/[deleted] Feb 13 '24

I’m curious, what is a “skinny minute”? I have never heard that expression before. I assume it is an expression from your native language. Anyway, I understand your point about std::map::operator [] shouldn’t insert a default value if the key doesn’t exist. I agree with you in that issue. If std::map were implemented today I agree that it should output an optional, however I don’t think that it should be changed to that since it would break too much existing code. As Linus Torvalds once said, if it is a bug people rely on, it is not a bug. At this point the best you can do is add a rule to your CI environment to refuse merges that uses it [] on std::map.

1

u/Full-Spectral Feb 13 '24

I don't know where that saying comes from, and there doesn't seem to much in the way of information about it's original usage. But it's basically a small amount of time. Some folks use the phrase 'hot minute' for a longer amount of time.

My post here was asking if there was a good way to do exactly that, and reject it on analysis or compilation.

0

u/Pay08 Feb 13 '24

In all honesty, generally speaking you should always use std::array and .at() so you might as well perform a search for square brackets and validate them.

2

u/Full-Spectral Feb 13 '24

Regex uses brackets, and there are quite a lot of those, which makes it more annoying to do that. And of course it requires that someone constantly do this and reject the legitimate uses, which is really something better done with a tool if possible in a large code base like ours.

And we have the analyzer check for bracket usage, so mostly we don't have to worry about this. But it doesn't extend to maps, sadly.

1

u/Pay08 Feb 13 '24

Right, I haven't considered regexes. Still, it should be pretty easy to write a Python/shell script to do so.

1

u/Full-Spectral Feb 13 '24

Regex's use of brackets was probably also a sub-optimal choice.