logoalt Hacker News

jmyeettoday at 4:40 AM1 replyview on HN

The C string and C++'s backwards compatibility supporting it is why I think both C and C++ are irredeemable. Beyond the bounds overflow issue, there's no concept of ownership. Like if you pass a string to a C function, who is responsible for freeing it? You? The function you called? What if freeing it is conditional somehow? How would you know? What if an error prevents that free?

C++ strings had no choice but to copy to underlying string because of this unknown ownership and then added more ownership issues by letting you call the naked pointer within to pass it to C functions. In fact, that's an issue with pretty much every C++ container, including the smart pointers: you can just call get() an break out of the lifecycle management in unpredictable ways.

string_view came much later onto the scene and doesn't have ownership so you avoid a sometimes unnecessary copy but honestly it just makes things more complex.

I honestly think that as long as we continue to use C/C++ for crucial software and operating systems, we'll be dealing with buffer overflow CVEs until the end of time.


Replies

hrmtst93837today at 7:09 AM

Irredeemable is a bit much. C APIs often bury ownership in docs or naming, so callers guess whether the callee borrows the buffer or takes it, and that guess causes a lot of the pain.

string_view helps, but only because it states "non-owning" in the type. You can still hand out a view into dead storage and get the same bug with nicer syntax.