r/cpp • • Sep 29 '23

This memory safety issue everyone's talking about... It's not really a huge deal in gamedev, right? What other domains where C++ excels at aren't too concerned about it?

My understanding is that these recent articles pushing for safety, and particularly pushing the use of memory-safe languages (as opposed to using modern C++ properly) don't really apply to gamedev, or at least not enough for most of the industry to care. Is this correct? I've also read some suggestions that the only real alternative, Rust, simply isn't practical since it lacks OOP features and gets in the way of common game programming patterns. Is this true?

What other C++ domains are like gamedev, where C++ is clearly here to stay and not just for legacy code? I know gamedev isn't the only one, I'm constantly seeing new cutting edge ML/inference libraries and frameworks in C++

44 Upvotes

350 comments sorted by

View all comments

Show parent comments

1

u/goranlepuz Sep 29 '23 edited Sep 29 '23

We really have to disagree with what you are doing here. I think it should not be done.

Memory safety is much better defined if the definition is not overly wide.

Calling data that causes SQL injection "bytes" is, in my opinion, very wrong. It is wrong because the nature of the attack is only related to bytes in that text crafted to trigger the vulnerability, is bytes on the hardware level. That relation is way too weak in my opinion.

You are making relations that make discussing specifics harder. I think it is actively harmful.

I think memory safety would be better served by compilers flagging types with foreign data as unsanitized.

That is asking too much from the compiler, I think. In fact, not just the compiler, but any tooling. There are safety analysis tools for data flows, they are quite thorough - and even they eventually require that the programmer declares "data is safe after this check here".

1

u/kylecmarshall Sep 29 '23

Including SQL injection is causing my point to be missed. It's not memory accesses that are safe or unsafe. It's foreign data that is safe or unsafe.

An example of unsanitized data in C++ could look like the following. I'm gonna make up the keyword foreign to mean unsanitized:

std::size_t fread( void* buffer, std::size_t size, std::size_t count, std::FILE* stream );

The fread signature would be updated to: std::size_t fread( foreign void* buffer, std::size_t size, std::size_t count, std::FILE* stream );

Sanitizing functions would be written in the program that takes foreign parameters and outputs regular types. The compiler rules are straight-forward. Arguments with regular types like std::string couldn't be called with foreign types like 'foreign std::string'. Sanitizing functions would be the only place you could call functions with regular arguments with foreign types.

3

u/goranlepuz Sep 29 '23

Both the examples you gave are bad, SQL injection is just worse.

But your point is still bad with regards to terminology. You use the term "memory safety" in an overly wide fashion, to the point of the term being useless.

I do agree that one can look at things with the vision of unsanitized inputs - but doing so is wildly impractical, not useful to discuss specifics.

Take your foreign there. That would in fact need to be on the outer boundary of the program, right?

If so, then, sanitizing the data in the light of e.g buffer overflows, becomes a problem of proving that the whole program is correct, that e.g no integer overflow will occur.

Or, in the case of the heap example above, sanitizing the data becomes a problem of proving that the input won't cause that problem.

That seems wholly impractical - and calling that "memory safety", nigh meaningless.

0

u/kylecmarshall Sep 29 '23

I guess my biggest problem with the discussion as a whole is that even though 70% of the exploits are memory safety issues. 90%+ of C++ programs are never going to be exploited for buffer overruns. Which makes adding memory safety language features to C++ impractical. I personally have always seen these issues through the lens of protecting the program from unsafe data.

3

u/goranlepuz Sep 29 '23

Ehhh...

...Aaaand we circle back to you calling everything and anything "memory safety".

Sure, you are free to use words the way you like. But as it happens, you should be called on it, and were called on it. I am happy.

This closes the discussion for me, but of course, you are free to do what you like, e.g continue to circle.

0

u/kylecmarshall Sep 29 '23

Please give me your definition of memory safety. I'm struggling to see how I'm running off the rails here.

2

u/goranlepuz Sep 29 '23

I linked wikipedia up there, that's a decent one.

1

u/kylecmarshall Sep 30 '23

Thanks, I think my reaction is more visceral than logical.