11 ms·
A bug in GCC that may cause memory leaks in valid C++ programs
- jhasse 10y agoNice find! Btw: Is it correct that this won't get fixed in Debian because it isn't a security issue?
- matheweis 10y agoNot sure on the current Debian policy but this bug could certainly be framed as a potential security issue; exploiting the bug could result in resource exhaustion and denial of service.
- ensiferum 10y agoExploited by who? The programmers ?
- tacostakohashi 10y agoThe users. Anybody who can connect to a server implemented in C++ can compiled by a gcc with this problem.
- matheweis 10y agoI think it's not quite that severe; anybody who can connect to a server implemented in C++ that is compiled with gcc and initializes objects in the way described here could exploit the bug.
- mbrubeck 10y agoAnd the initialization needs to conditionally throw an exception based on user input.
- xorblurb 10y agoFor released distro, I doubt Debian will rebuild all their packages and upload them all on security.debian.org...
- vvanders 10y agoGood stuff. Only compiler bugs I've seen in C++ have all been around exceptions. I think this is just another reason towards force-handled error codes. You don't have a whole new control flow that needs to be verified and is much simpler from an implementation/runtime perspective.
- jhasse 10y agoI think this would just move the bugs to the applications.
- vvanders 10y agoHence the "force-handled" part of my comment :).
- gizmo686 10y agoBut what you need is force-handled-correctly. If you think that programmers will correctly deconstruct partially constructed objects at every location where construction might fail, then you have far too much faith in our profession.
- wvenable 10y agoIt doesn't solve the problem because you'd have move to 2 stage construction/initialization and that creates even more problems than this.
- vvanders 10y agoNope, if you have an operation that can fail then wrap it in a type that represents that(I'm partial to Rust's Result<T,E>) then you force consumers to deal with potential errors and you can do 1-call construct + init if you want to. Exceptions cause all sorts of problems(like violating the principal of only paying for what you use). They aren't checked so you can't enforce handling via type system and in general they're just a mess in C++.
- mnarayan01 10y agoIf you assign the initialized value to a variable then you get the expected behavior: https://wandbox.org/permlink/r6csgR44BZBWudLV https://wandbox.org/permlink/r6csgR44BZBWudLV. What exactly is the defined destruction behavior for an object that's never (necessarily) on the stack? Or is it actually required to be on the stack in this case?
- JonathonW 10y agoIn C++14, the spec specifies that a temporary object's destructor is called as the last step in evaluating the full expression that contains the point where it was created (C++14 §12.2). I'm presuming it's the same in earlier versions of the language, but I don't have those specs handy ATM.
- maxlybbert 10y agoI don't have the specs either, but I do remember that in Design and Evolution of C++, Stroustrup discusses nailing down the lifetime of temporary objects. I believe it was in the first standard (i.e. 1998).
- mnarayan01 10y agoI'm more talking about the exception behavior looking at e.g. https://wandbox.org/permlink/S5paK3Z9NpeCv3I0 https://wandbox.org/permlink/S5paK3Z9NpeCv3I0 (which exhibits the same bug). I mean you're right, and the fact that the User object is temporary should presumably not keep the first Resource destructor from firing, just it seems weird. I guess in my defense, apparently it seems weird to GCC as well.
- JonathonW 10y agoThe spec also explicitly addresses that (automatic objects' destructors must be called after an exception)-- see section 15.2 in the C++14 spec. There is no ambiguity in the spec here; this is a GCC bug.
- ambrop7 10y agoHow can it be this was reported almost two years ago and nothing has been done?
- Kenji 10y agoShow me a program that someone used in production that became unstable because of this bug, and I will give you the answer to your question ;)
- DoofusOfDeath 10y agoI imagine it's because those who maintain GCC have varying agendas for prioritizing their contributions. The fact that is hasn't been fixed yet may be a signal that, in practice, this hasn't bitten anyone hard enough to motivate a patch submission. In which case, one could argue that the bugs are being tackled in a somewhat rational order. It may be instructive to consider why you yourself haven't submitted a fix. Perhaps that reason is common across many of those capable of submitting a fix.
- dsharlet 10y agoI really disagree with this. I work on compilers, so I feel for the people who work on GCC, but, I think it is totally unreasonable to expect the average GCC user who might have encountered this bug to be capable of fixing this themselves. I hacked around in clang for a bit for a compiler project. I think it would have taken me weeks or months to get to the point where I was able to fix the equivalent bug (if it existed) in Clang. Compilers are big, hard to debug projects. And, IMHO, the Clang/LLVM code is much more approachable than GCC. Not all developers are hobbyists that can afford to detour from their work for weeks or months to ramp up on GCC to fix this. I'd also point out that this is the kind of bug that could exist and be triggering in a lot of code as we speak but no one (besides the author of this blog post) have caught it. I think it's insane to ignore a bug like this.
- boomlinde 10y ago>I really disagree with this. I work on compilers, so I feel for the people who work on GCC, but, I think it is totally unreasonable to expect the average GCC user who might have encountered this bug to be capable of fixing this themselves. That's not my take on it. It doesn't read like an urge to fix it yourself, rather it's literally an urge to consider why you haven't, in order to understand why others also have not. "Not all developers are hobbyists that can afford to detour from their work for weeks or months to ramp up on GCC to fix this" seems like an appropriate response and probably more or less the point that the GP wanted to make.
- wfunction 10y agoI would like to point out that if you write code like this (that is, creating a temporary object only to have it instantly deleted), your code smells. Backwards-compatibility notwithstanding, it would not be totally illogical for C++ to allow optimizing out construct-destruct chains even in the presence of side effects, in a similar manner to the existing copy elision behavior. After all, the object is unused, and the destructor should be merely undoing whatever the constructor did, so afterward it should be as if the object was never constructed. If you rely on the side effects of normal construction, that's not so different from relying on the side effects of copy construction, which you can't already do. Hence you shouldn't do the first either. So yes, it's a bug, and needs to be fixed, but regardless of that, you shouldn't write code like this in the first place. EDIT: See comment below, apparently the bug comes up in more cases than those I referred to here.
- MereInterest 10y agoI don't think it seems like a code smell at all. For example, I could be adding together multiple vectors, some of which are created as temporaries. auto total_offset = Vector{x_offset, y_offset} + global_offset; Granted, in this case, there are no resources being held by such a Vector class, but having immediately-destroyed temporary objects is not necessarily a code smell.
- wfunction 10y agoThat's definitely not "immediately destroyed", there's operator+ being called between construction and destruction. I didn't realize this can trigger the bug though, so if it does, I should clarify I didn't mean to include it in my comment. I was only referring to cases where there is no intervening operation between construction and destruction.
- SolarNet 10y agoIt's a minimal testcase; minimal. I thought the blog post was pretty clear that it had nothing to do with how the object was used after (e.g. if it was immediately destructed or not) and even linked to a real world example that would disprove that. Hence why your holier than thou attitude about not writing smelly code is just asinine and not helpful. It plays into a mindset that people who write "good code" don't have to worry about 'esoteric' bug reports (e.g. like security advisories).
- homerowilson 10y agoread http://250bpm.com/blog:4 http://250bpm.com/blog:4 for an interesting related discussion.
- rsfinn 10y agoIf you're curious (as I was), this bug does not occur in clang, as you can demonstrate for yourself by following the link to wandbox.org in the original story and changing the compiler.
- isaac_is_goat 10y agoI tested it with Visual Studio 2017 and it seems to work correctly there as well.
- Uptrenda 10y agoReminds me of this: https://www.win.tue.nl/~aeb/linux/hh/thompson/trust.html https://www.win.tue.nl/~aeb/linux/hh/thompson/trust.html Trusting Trust.
- 72deluxe 10y agoVery interesting. This is impossible in C++03; this must be related to the very useful changes in aggregate initialisation for C++11 (particularly useful as there's one great syntax for that and uniform initialisation).
- targeted 10y agoThis reminds me of a bug that I found in Borland C++ 14 years ago: http://qc.embarcadero.com/wc/qcmain.aspx?d=3478 http://qc.embarcadero.com/wc/qcmain.aspx?d=3478 Sad to know that after all that time compilers still can't get even the core semantics right.
- faragon 10y agoC++ exceptions are dangerous, even when working as expected. In my opinion, C++ without exceptions (or the minimal for e.g. logging the error -division by 0, invalid memory access, etc.-) is a fine language.
- xorblurb 10y agoNote: there is no such thing as standard C++ exceptions on invalid memory accesses. I'm not sure about division by 0, might be in the same case.
- faragon 10y agoStandard or not, if you use an OS with memory protection (MMU), you'll get those exceptions in C++.
- lmm 10y agoWithout exceptions you can't do RAII (because you can't do nontrivial work in constructors, because you have no way to report failure) which is probably C++'s one real selling point. (There are other reasons I'd say it isn't a fine language - mainly the lack of sum types and the way templates are only checked after full expansion (that one will at least be fixed if and when concepts make it into the language)).
- nathell 10y agoRAII used to be incompatible with exceptions (I'm not sure whether anything changed for the better in C++17). The reason is that you can't throw exceptions from destructors, and e.g., on Linux, destruction of the file object (wrapping a file descriptor) can result in an error because close() may well return -1 setting errno to EIO. See `man 2 close` for details.
- tcbawo 10y agoThat's really not true. It takes a little discipline to ensure your constructor will succeed prior to calling it, but it's worth it. You generally want to make your destructor noexcept, so you get free implicit move optimizations.