News for hackers. Stuff that matters.
Re:A Guide to Undefined Behavior in C and C++ (2010) by ilovecaching on Sunday November 04, 2018 @01:08AM (#18374524)
This is why if you're currently writing C or C++, please, please look into Rust.

Titus Winters just did a great talk at Pacific++ outlining how difficult it is to understand the quirks of C++. Not even Bjarn can keep all of it in his head. The dice are completely loaded against you to fail and introduce bugs, unintended behavior, and vulnerabilities in your code.

Rust isn't a silver bullet that will suddenly fix your life, but it doesn't deal with the crazyness of C undefined behavior and doesn't have to be backwards compatible. It's designed from the ground up to make comprehensive sense, and safety is a feature enforced for you by the compiler in many cases.

Re:A Guide to Undefined Behavior in C and C++ (2010) by banachtarski on Sunday November 04, 2018 @02:38AM (#18374760)
> This is why if you're currently writing C or C++, please, please look into Rust.

Ugh if you read the article, you would know that the UB described here is the same in Rust. There is this massive group think about which languages are safe and/or fast, and whenever an article like this comes up, invariably someone says "Rust!" without really looking at the problem itself.

Personally, I am in wait and see mode on Rust. Without better generics support (aka template <size_t> or similar) and a number of other things, it still doesn't meet my bar for writing fast generic code, and who knows how complex it will be at that point? I say the jury is still out.

Re:A Guide to Undefined Behavior in C and C++ (2010) by newacctjhro on Sunday November 04, 2018 @04:05AM (#18374967)
> Ugh if you read the article, you would know that the UB described here is the same in Rust.

Rust doesn't have a formal memory model yet, but it's already known that UB in Rust is quite restricted:

https://doc.rust-lang.org/nomicon/what-unsafe-does.html

Most importantly, UB in Rust should only arise if you're writing unsafe code (barring compiler bugs). Typically, most Rust code is safe. This is a huge win.

Re:A Guide to Undefined Behavior in C and C++ (2010) by saagarjha on Sunday November 04, 2018 @02:58AM (#18374801)
> Ugh if you read the article, you would know that the UB described here is the same in Rust. There is this massive group think about which languages are safe and/or fast, and whenever an article like this comes up, invariably someone says "Rust!" without really looking at the problem itself.

Rust does have vocal proponents, but I don't see how "the UB described here is the same in Rust". Rust should trap consistently when dividing by zero.

Re:A Guide to Undefined Behavior in C and C++ (2010) by oconnor663 on Sunday November 04, 2018 @08:45AM (#18375833)
> the UB described here is the same in Rust

I don't think this is correct. Dividing by zero in Rust produces a panic, and panics are observable side effects that may not be reordered with other side effects. This is sometimes annoying, because it can prevent the compiler from eliminating bounds checks in some cases, but it avoids the problems mentioned in this article.

Re:A Guide to Undefined Behavior in C and C++ (2010) by ilovecaching on Sunday November 04, 2018 @12:20PM (#18376882)
> Ugh if you read the article, you would know that the UB described here is the same in Rust.

Ugh, if you took the time to learn Rust before criticizing it, you'd know that Rust only allows undefined behavior in unsafe blocks.

> Without better generics support

So you have no idea what generics are. Templates aren't generics, they're fancy code generators expanded at compile time, which means they're incredibly loosely typed and allow you to do things like have scalar parameters like size_t n (because they're basically macros). C++ templates are essentially a form of duck typing that has led to an increased need for specification, hence concepts in C++20.

Rust's parametric polymorphism and type classes (aka generics) follows in the ML and Haskell tradition in that the type checking is like a constraint program that is run at compile time. When a generic item is invoked, the specification of that type is immediately checked for consistency at the call site, otherwise the typechecking fails.

Re:A Guide to Undefined Behavior in C and C++ (2010) by red75prime on Sunday November 04, 2018 @02:54AM (#18374792)
UB is a way to better optimize code by (silently) offloading a burden of correctness proof to a programmer. Rust prefers to be more explicit about it. Const generics will surely not change that.
Re:A Guide to Undefined Behavior in C and C++ (2010) by flohofwoe on Sunday November 04, 2018 @06:57AM (#18375446)
It's not like you stumble over UB in your daily work in C/C++, you know it exists, you know which areas to avoid, you compile and test on the 3 "big" compilers (gcc, clang, cl.exe). On clang, test with UBSan enabled.

UB isn't "crazyness" it's just a detail to be aware of.

A compiled binary that worked correctly won't suddenly run into undefined behaviour, that can only happen if you upgrade compiler versions or change compilation flags. But this only happens while the application is still actively developed and tested.

Re:A Guide to Undefined Behavior in C and C++ (2010) by the_why_of_y on Sunday November 04, 2018 @07:26AM (#18375544)
That presumes that your testing process will find the input that actually triggers the UB in your program before the release.

The CVE history of the major browser vendors indicate that this is currently an unsolved problem for their large C++ applications.

Re:A Guide to Undefined Behavior in C and C++ (2010) by flohofwoe on Sunday November 04, 2018 @08:07AM (#18375673)
Of course, and that's exactly where Rust makes sense. Few pieces of software are as safety-critical and complex as a browser though, that's why I'm not a fan of the general "UB scaremongering" ;)
Re:A Guide to Undefined Behavior in C and C++ (2010) by s3cur3 on Sunday November 04, 2018 @10:44AM (#18376357)
Full-time C++ game dev of ~5 years here. I absolutely run into UB issues at least once a month, sometimes as often as a couple times in a week. I doubt our codebase is of below average quality either. UBsan is a godsend here... without it, this stuff was truly hell to debug.
Re:A Guide to Undefined Behavior in C and C++ (2010) by oconnor663 on Sunday November 04, 2018 @08:40AM (#18375801)
> A compiled binary that worked correctly won't suddenly run into undefined behaviour, that can only happen if you upgrade compiler versions...

Upgrading the compiler version and causing undefined behavior sounds pretty sudden. Here's my favorite story along those lines: http://pzemtsov.github.io/2016/11/06/bug-story-alignment-on-...

Re:A Guide to Undefined Behavior in C and C++ (2010) by ilovecaching on Sunday November 04, 2018 @11:55AM (#18376756)
It isn't just UB. It's ADL, ODR, templates, multiple inheritance, etc. The de facto compatibility with C and its own backwards compatibility are also both curses on C++. C++ also blew their complexity budget in a lot of questionable ways that just makes it hard to keep C++ code maintainable. When things become unmaintainable, they become buggy.
Re:A Guide to Undefined Behavior in C and C++ (2010) by humanrebar on Sunday November 04, 2018 @09:42AM (#18376059)
> It's not like you stumble over UB in your daily work in C/C++...

I don't know. I often help colleagues out with things like preprocessor nonsense and violations of the One Definition Rule.

A lot of it has to do with difficult build and packaging tools. Actually standardizing a module system will be an important step in the right direction.

Re:A Guide to Undefined Behavior in C and C++ (2010) by beefhash on Sunday November 04, 2018 @09:42AM (#18376062)
> This is why if you're currently writing C or C++, please, please look into Rust.

But please, please also look into its platform support table[1], too. If you're the slightest bit opposed to the Windows/macOS/Linux hegemony, you may want to reconsider Rust.

[1] https://forge.rust-lang.org/platform-support.html

Re:A Guide to Undefined Behavior in C and C++ (2010) by ericpauley on Sunday November 04, 2018 @09:53AM (#18376109)
What exactly are you saying? Rust can be built with zero os/kernel dependencies at all. Case in point, people have written entire operating systems on bare metal in rust.
Re:A Guide to Undefined Behavior in C and C++ (2010) by xmiller on Sunday November 04, 2018 @05:17AM (#18375163)
Are there any large programs like BIND written in Rust? If so, what is their actual security record?
Re:A Guide to Undefined Behavior in C and C++ (2010) by jcranmer on Sunday November 04, 2018 @09:03AM (#18375899)
Several bits of Firefox are written in Rust now. Looking through the bug reports, there has yet to be any CVE in any of the Rust-rewritten components.
Re:A Guide to Undefined Behavior in C and C++ (2010) by nickpsecurity on Sunday November 04, 2018 @12:23PM (#18376906)
TrustDNS is one in that space. We won't know for a while what actual, security record is. We do have some evidence from fuzzing where it led to panics vs worse effects in C. I think an objective test would compare effects of fuzzing Rust and C libraries to see how often Rust just panicked. The study should use different types of libraries that use the language in different ways. If Rust's claims are true, most problems will involve 3rd-party libraries that aren't Rust, unsafe Rust, and/or compiler errors that break safety. People doing the study could just grab popular C apps vs similar stuff in Rust on Github that's actually maintained. Also, look at amateurish and unmaintained stuff to see if those still have minimal vulnerabilities.

That's how I'd do it anyway.

Re:A Guide to Undefined Behavior in C and C++ (2010) by pjmlp on Sunday November 04, 2018 @07:05AM (#18375477)
There is a whole OS, good enough?
Re:A Guide to Undefined Behavior in C and C++ (2010) by gbw on Sunday November 04, 2018 @08:39AM (#18375798)
If you mean Redox, that is hardly a production OS (yet).

People don't have infinite time and only search for security issues in software that's actually in use.

Which is one of the reasons that C/C++ appear more often in CVEs.

Re:A Guide to Undefined Behavior in C and C++ (2010) by pjmlp on Sunday November 04, 2018 @09:01AM (#18375893)
The OP asked for an example of a large program like BIND, I gave him/her an OS, which naturally also contains something like BIND, but apparently we now move the goal posts for how much users it has.

I love how people hand have the fact that even projects like the Linux kernel, with their stringent processes aren't able to avoid CVEs, to the point it became the major focus of Linux Kernel Security Summit 2018.

Yet we all know that only newbies do major errors in C. /s

Re:A Guide to Undefined Behavior in C and C++ (2010) by sethammons on Sunday November 04, 2018 @08:10AM (#18375683)
Depends on popularity and use.
Re:A Guide to Undefined Behavior in C and C++ (2010) by pjmlp on Sunday November 04, 2018 @09:19AM (#18375959)
The question was about an example for a large program like BIND.
Re:A Guide to Undefined Behavior in C and C++ (2010) by sureaboutthis on Sunday November 04, 2018 @08:25AM (#18375741)
Which one?
Re:A Guide to Undefined Behavior in C and C++ (2010) by otabdeveloper2 on Sunday November 04, 2018 @03:11AM (#18374833)
"Undefined behavior" in C++ just means stuff that's either CPU-specific or compiler-specific.

Rust has no undefined behavior because it only supports one CPU and has only one compiler implementation.

When Rust supports a dozen architectures and has three different compiler implementations then Rust will be just as full of UB as C++. Except with the added "bonus" of all the UB being brand-new and unspecified, unlike the well-known portability pitfalls of C++.

Re:A Guide to Undefined Behavior in C and C++ (2010) by pcein on Sunday November 04, 2018 @03:52AM (#18374933)
I am happily running Rust generated code on my ARM microcontrollers. Rust uses LLVM for code generation and can support any architecture which LLVM supports.
Re:A Guide to Undefined Behavior in C and C++ (2010) by steveklabnik on Sunday November 04, 2018 @04:47AM (#18375078)
> When Rust supports a dozen architectures

"Rust now available on 14 Debian architectures" https://lists.debian.org/debian-devel-announce/2018/11/msg00...

Re:A Guide to Undefined Behavior in C and C++ (2010) by pjmlp on Sunday November 04, 2018 @04:12AM (#18374984)
There are plenty of system languages with a very tiny percentage of UB vs all the languages that allow for copy-paste of C code.

Because they prefer to loose a couple of ms to paying the price of the ultimate performance might bring.

Like in real life you can speed at 300 KMH with a 1200cc motorbike, or doing it a car with reinforced structure wearing seatbelts and airbags.

Even with diminished chances of survival, I rather be in the car.

Re:A Guide to Undefined Behavior in C and C++ (2010) by otabdeveloper2 on Sunday November 04, 2018 @04:23AM (#18375021)
UB is an implementation issue, not a language issue. UB is a fact of life that cannot be removed by overspecifying a language -- if your language spec has too much implementation-specific stuff in it, then compiler writers will simply ignore it. Many such examples. The best you can do is to document the implementation-specific stuff extensively.

Also, implementation-specific details aren't always about maximizing performance. Compilers implement languages slightly differently because of architecture and OS differences and because it's easier that way for the people writing compilers.

Re:A Guide to Undefined Behavior in C and C++ (2010) by pjmlp on Sunday November 04, 2018 @04:35AM (#18375049)
UB is not implementation specific at all, the ISO documentation is quite clear about which is which.

Other systems languages manage to do it quite well.

It was really a shame that Bell Labs gave UNIX away for a symbolic price during 10 years before being allowed to actually sell it.

We wouldn't be having this kind of C related talks.

Re:A Guide to Undefined Behavior in C and C++ (2010) by dagenix on Sunday November 04, 2018 @03:44AM (#18374913)
> Rust has no undefined behavior because it only supports one CPU ...

That is not true. Rust works on multiple architectures.

Re:A Guide to Undefined Behavior in C and C++ (2010) by otabdeveloper2 on Sunday November 04, 2018 @04:24AM (#18375023)
There's a massive chasm between "works on" and "supports".
Re:A Guide to Undefined Behavior in C and C++ (2010) by Retra on Sunday November 04, 2018 @11:20PM (#18379843)
That doesn't seem relevant -- Rust works on and supports multiple architectures. And there's plenty of UB in unsafe code.