At my tiny org, the core engine is C++ due to a compute heavy workload. I need to internally react to NSA's recommendation about replacing C++ with a memory safe language.
They're highlighting the fact that C++ (or C) allows the code to read from or write to any memory address (i.e. pointers). That can lead to bugs like read / write out of bounds, or read / write after delete (dangling pointers). These bugs can then be exploited in production by malicious actors.
I don't think my org can afford to rewrite the codebase in another language in near to medium term, and I am not even sure we need to. As long as we do the following in our codebase, can we claim memory safety? (for the purpose of this question, I want to ignore any other kinds of safety or good coding practices).
- No heap allocation ourselves. No new / delete
- No char* for string variables, only std::string
- Replace static arrays with std::array, dynamic arrays with std::vector
- No pointers. Pass objects by reference where needed
Anything else?
Edits based on comments received
6/14/24: The code is not running in a highly resource constrained environment like an embedded system. Nor is it super sensitive where failure could harm someone's health or well being
6/14/24: We use Visual Studio and GCC as our compilers. To the extent the compilers would catch something, I don't want to repeat it in our internal guidelines
6/17/24: Safety seems to mean different things to different people. In the context of this question (and NSA's paper), a bug introduced by lack of memory safety is harmful because it can be exploited by malicious actors. Think heart bleed or Morris worm where buffer overflow had a role to play. A program crashing is not the same thing as a program being tricked to do something it wasn't supposed to do (using heart bleed as an example, I would rather have it crash than leak credentials)
6/17/24: The fact that this question is about memory safety, doesn't mean that we don't care about other kinds of safety
6/17/24: Google's Safer Usage of C++ comes quite close to describing both the problems and potential solutions that I am grappling with.