News for hackers. Stuff that matters.
Re:Modern C++ Won't Save Us by Tracist on Tuesday April 23, 2019 @12:29AM (#19725625)
> It has been many years since I shipped a memory bug in C++. It is just not a real worry for me.

The whole comment sounds so much like well written satire, but I think he's being serious.

Re:Modern C++ Won't Save Us by vasilipupkin on Tuesday April 23, 2019 @12:58AM (#19725709)
I agree with him. in many practical applications with well design class hierarchies it just really isn't much of an issue. Hasn't been for me either.
Re:Modern C++ Won't Save Us by fetbaffe on Tuesday April 23, 2019 @04:50AM (#19726666)
> with well design class hierarchies

:eyes:

Re:Modern C++ Won't Save Us by vasilipupkin on Tuesday April 23, 2019 @08:54AM (#19728073)
you can roll eyes at me all you want, but I've been programming in C++ for a long time. These memory access issues just don't seem to be a big problem for us in practice. That's because we wrap all raw memory manipulation in appropriate classes for our application, so it's just not an issue. I agree it could be an issue in theory.
Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @11:08AM (#19729361)
He rolls his eyes at "hierarchies". Libraries do make the difference.

Somebody else interjected Design Patterns. You can define a design pattern as a weakness in your language's ability to express a library function to do the job.

Re:Modern C++ Won't Save Us by aldanor on Tuesday April 23, 2019 @07:58AM (#19727697)
... and with proper use of Design Patterns!
Re:Modern C++ Won't Save Us by paulmooreparks on Tuesday April 23, 2019 @01:02AM (#19725729)
Why is it difficult to believe? I've also written plenty of C++ code without memory bugs. It's not that hard if you play by a few simple rules.
Re:Modern C++ Won't Save Us by dodobirdlord on Tuesday April 23, 2019 @02:23AM (#19726082)
> I've also written plenty of C++ code without memory bugs.

The classic response to this is "That you know of." Consider that even quality-conscious projects with careful code review like Chrome have issues like this use-after-free bug from time to time.

https://googleprojectzero.blogspot.com/2019/04/virtually-unl...

So when people claim that they personally don't write memory bugs I tend to assume that they are mistaken, and that the real truth is that they haven't yet noticed any of the memory bugs that they have written because they are too subtle or too rare to have noticed.

Re:Modern C++ Won't Save Us by tpolzer on Tuesday April 23, 2019 @02:43AM (#19726162)
Chrome is in an exceptionally hard place because of its JIT. Your language cannot tell you if it's safe for your JIT to omit a bounds check.
Re:Modern C++ Won't Save Us by comex on Tuesday April 23, 2019 @05:29AM (#19726846)
That post describes two vulnerabilities: one is in the JIT, but the other one is in regular old C++ code. More generally, JIT bugs are a relatively small minority of browser vulnerabilities. More often you see issues like use-after-free in C++ code that interacts with JS, such as implementations of DOM interfaces, but the issues are not directly JIT related and would be avoided in a fully memory-safe language.
Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @08:22PM (#19734395)
Chrome, like Firefox, is not an example of modern C++ code. Google's and Mozilla's coding standards enforce a late-'90s style. It is astonishing they get it to work at all.
Re:Modern C++ Won't Save Us by paulmooreparks on Tuesday April 23, 2019 @10:45PM (#19735121)
In this case, I mean a subsystem that has been in production since 2006 and has been processing hundreds of thousands of messages a day. I don't claim that it's perfect or bug-free, but if it had significant memory errors I'd have heard about it. I designed and implemented it to use patterns like RAII to manage memory, and it's worked quite well.
Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @03:15AM (#19726292)
That is why use tools like valgrind to verify that you got it right.
Re:Modern C++ Won't Save Us by esrauch on Tuesday April 23, 2019 @03:39AM (#19726386)
When I worked on a mobile C++ project at Google, we went exceptionally out of our way to avoid memory issues.

We ran under valgrind and multiple sanitizers (and continuously ran those with high coverage unit and integration tests). We ran fuzzers. We had strictly enforced style guides.

We still shipped multiple use after frees and ub-tripping behavior. I also saw multiple issues in other major libraries that we were building from source so it can't be pointed at as just incompetency on my team.

Basically, it might be possible but I think it's exceptionally more difficult to write memory safe C++ than this thread is making it sound.

Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @05:17AM (#19726793)
Writing memory safe programs in C++ is possible. Most coding styles and some problem domains don't lend themselves to it naturally, though. In my experience, restricted subsets used for embedded software vastly reduce the risk of introducing errors and make actual errors easier to spot and fix.
Re:Modern C++ Won't Save Us by sgift on Tuesday April 23, 2019 @06:31AM (#19727174)
> Writing memory safe programs in C++ is possible.

Everything "is possible" in the sense that in theory you can do it. But if time and time again people fail to do it. Even people who invest almost heroic levels of effort (see above: valgrind, multiple sanitizers, and so on) you get to the point where you have to accept that what is possible in theory doesn't work in practice.

Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @07:47AM (#19727627)
I have seen it done in practice, on rather large systems. But it requires actual, slow software engineering instead of the freestyle coding processes that are used in most places.
Re:Modern C++ Won't Save Us by paulmooreparks on Tuesday April 23, 2019 @10:48PM (#19735135)
My main rule is "no naked new," meaning that the only place the new operator is allowed is in a constructor, and the only place delete is allowed is in a destructor (unless there's some very special circumstance). This style lends itself to RAII. The other rule is to use the standard library containers unless there's a very good reason not to do so. That seems to cover most of the really basic errors.
Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @11:14AM (#19729417)
Yes, I know how you code are obliged to code at Google. It is astonishing that anything works.

The "strictly enforced style guides" strictly enforce '90s coding habits.

Re:Modern C++ Won't Save Us by adrianN on Tuesday April 23, 2019 @03:44AM (#19726406)
Together with a test-suite that covers the exponential number of paths through your code...
Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @05:12AM (#19726767)
Changing programming language neither reduces the need for test coverage nor does it magically increase coverage.
Re:Modern C++ Won't Save Us by adrianN on Tuesday April 23, 2019 @05:16AM (#19726783)
A type system changes the need for test coverage because it eliminates whole classes of bugs statically that would need an infinite amount of tests to eliminate dynamically.
Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @05:19AM (#19726802)
That leaves an infinite amount logic bugs to be tested for. Types cannot fix interface misuse at integration and system level. So no, this does not reduce the need for testing.
Re:Modern C++ Won't Save Us by comex on Tuesday April 23, 2019 @05:33AM (#19726864)
Whether they reduce the need for testing overall is arguable. But what matters in this discussion is that types can guarantee memory safety, meaning that the cases that you forgot to test – and there will always be such cases, no matter how careful you are (just look at SQLite) – are less likely to be exploitable.
Re:Modern C++ Won't Save Us by gmueckl on Tuesday April 23, 2019 @05:52AM (#19726972)
Types can only provide limited memory safety. There is a real need to deal with data structures that are so dynamic as to be essentially untyped. Granted, this usually happens in driver code for particularly interesting hardware, but it happens. Also, I have not yet seen a type system that is both memory safe and does not prohibit certain optimizations.
Re:Modern C++ Won't Save Us by kibibu on Tuesday April 23, 2019 @08:25AM (#19727869)
I haven't written c++ seriously for a number of years. Do you still have to do all that "rule of three" boilerplate stuff to use your classes with the STL? Is it better or worse now with move constructors?
Re:Modern C++ Won't Save Us by micv on Tuesday April 23, 2019 @09:38AM (#19728419)
It's a bit better with C++11 syntax where you can use = delete to remove the default constructors/destructors, e.g.:

  class Class
  {
      Class();
      Class(const Class&) = delete;
      Class& operator = (const Class&) = delete;
      ~Class() = default;
  };
Which I find slightly cleaner than the old approach of declaring them private and not defining an implementation, but the concept hasn't changed much. I'd love a way to say 'no, compiler, I'll define the constructors, operators, and destructors I want - no defaults' but that's not part of the standard.

Move constructors are an extra that, if I remember correctly, don't get a default version, thankfully.

Re:Modern C++ Won't Save Us by ncmncm on Tuesday April 23, 2019 @10:52AM (#19729191)
So, so much better. Nowadays we "use" what has been called "rule of zero". Write a constructor if you maintain an invariant. Rely on library components and destructors for all else.
Re:Modern C++ Won't Save Us by jcelerier on Tuesday April 23, 2019 @03:30AM (#19726351)
> https://jaxenter.com/security-vulnerabilities-languages-1570...

there's a world in terms of safety between C and C++.

Re:Modern C++ Won't Save Us by comex on Tuesday April 23, 2019 @05:43AM (#19726925)
The comparison in that link is pretty meaningless; it scores languages by how many vulnerabilities have been reported in code written in them, without even making an attempt to divide by the total amount of code written in them, let alone account for factors like importance/level of public attention, what role the code plays, bias in the dataset, etc.
Re:Modern C++ Won't Save Us by eska on Tuesday April 23, 2019 @09:30AM (#19728357)
To be fair the report explicitly states this limitation. jcelerier just conveniently forgot to mention it.
Re:Modern C++ Won't Save Us by eska on Tuesday April 23, 2019 @09:28AM (#19728338)
You're misrepresenting the report in order to justify your bias. Direct quote from the report:

    This is not to say that C is less secure than the other languages. The high number of open source vulnerabilities in C can be explained by several factors. For starters, C has been in use for longer than any of the other languages we researched and has the highest volume of written code. It is also one of the languages behind major infrastructure like Open SSL and the Linux kernel. This winning combination of volume and centrality explains the high number of known open source vulnerabilities in C.`
In other words the report explains this with 1) there being more C code in volume and 2) more C code in security-relevant projects (which are reviewed more by security researchers). It also states explicitly that your conclusion is not to be drawn from this.
Re:Modern C++ Won't Save Us by majewsky on Tuesday April 23, 2019 @05:46PM (#19733262)
Readable version of the quote:

> This is not to say that C is less secure than the other languages. The high number of open source vulnerabilities in C can be explained by several factors. For starters, C has been in use for longer than any of the other languages we researched and has the highest volume of written code. It is also one of the languages behind major infrastructure like Open SSL and the Linux kernel. This winning combination of volume and centrality explains the high number of known open source vulnerabilities in C.

Please, never ever use code snippets for quotes, unless you hate mobile users. Just put "> " in front.

Re:Modern C++ Won't Save Us by leetcrew on Tuesday April 23, 2019 @09:38PM (#19734812)
> unless you hate mobile users

or just period. I'm reading this on a 4K desktop display, and I still have to scroll. it's only useful for actual code, which is very rarely posted on hn.