Hacker News new | past | comments | ask | show | jobs | submit login
Two C++ bugs I found (ahelwer.ca)
3 points by ahelwer on Feb 5, 2023 | hide | past | favorite | 2 comments



While I agree that Rust and the likes are the future, your arguments fall sort of flat. These two examples are a classic case of not following RTFM.

Bug #1: If you know you're working with strings at any level, there are almost no reasons for not using `std::string`. `atoi` is an historic C function so it expects a C style string. String has a perfectly fine `.data` accessor just like vector. However it is safer as by the C++11 standard it is guaranteed to be null terminated. Not to mention the allocation scheme that vectors follow compared to strings.

Bug #2: While the docs could be better at showing cases of undefined behavior it does mention it in the second sentence. And while using undefined behavior can be ok at times; blaming the language when it breaks is not right. C and C++ give absolutely 0 shits about what you do as long as it doesn't violate its syntax or its defined interfaces.

All in all while Rust is a good step in the right direction you're still gonna have to RTFM. There are plenty of pitfalls in Rust as well even though undefined behavior is not allowed.


std::string wasn't included in the hardcoded emscripten imports, as I said.

The point of the blog post is that even though things are working as they are supposed to, it is easy to write weird bugs. I hope we can at least agree that making the undefined behavior of calling pop_back() on an empty vector to have that vector contain the entire memory of computer is an absurd choice.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: