News for hackers. Stuff that matters.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @06:12PM (#13314164)
No, Rust arrays are not safe by default: https://is.gd/iY5lPQ
Re:Getting Past C by staticassertion on Tuesday January 03, 2017 @06:36PM (#13314347)
Panics do not violate memory safety. In C that code would be undefined behavior.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @07:26PM (#13314601)
You are right, my mistake. I just can't call code failing in runtime "safe" (because I can't rely on it). But in terms of memory safety it's safe.
Re:Getting Past C by moosingin3space on Tuesday January 03, 2017 @07:30PM (#13314625)
Then use the get() function on an array/slice. It returns an Option so it won't perform an access off the end of the array, and you can catch it.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @07:35PM (#13314657)
thanks, I do. Only thing left is explain it to everybody else to avoid panicking in 3-party crates. I really hate when people use panicking just because it's easier than error handling. So it was "surprise" to see such behavior from "[x]" construction.
Re:Getting Past C by cesarb on Wednesday January 04, 2017 @06:44AM (#13317435)
Like in C, you are not supposed to pass out-of-bounds indexes to the [] operator. If anyone does so, it's a bug. Rust converts it to a panic, which safely terminates the thread/program, instead of C's undefined behavior.

You can also catch this panic at the thread boundary, so other threads in your program can keep running, unless the program was compiled to call abort() on panic.

Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @06:54AM (#13317493)
You still think in C limits of possibilities. Rust could just return Option as a result for [x] syntax.
Re:Getting Past C by cesarb on Wednesday January 04, 2017 @07:21AM (#13317638)
And then people would simply call .unwrap() on the return of the [] operator, leading to the same situation. It's extremely common for an array dereference to always be within the bounds, by code construction (I'm iterating over the indexes of the array, or I have an index into the array saved inside some other structure, and so on). The programmer knows it will never be out of bounds. The assert!() within the [] operator is only to protect you when the programmer gets it wrong, and in a correctly written program, will never trigger.

(The alternative instead of .unwrap() would be to propagate the error, polluting the whole program with code to handle errors which never will happen. And since they will never happen, many programmers would simply start ignoring them - and in the process, they would by accident end up ignoring errors that can happen. Not a good situation.)

Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @08:39AM (#13318121)
There is HUGE difference between "unwrap" (which can found by grep) and mutual dangerous behavior. Programmer can only assume, not know, without additional checks. "Polluting with error" is much better than panicking.
Re:Getting Past C by EugeneOZ on Wednesday January 04, 2017 @02:03PM (#13321075)
And for safe iterations Rust has "for .. in".
Re:Getting Past C by steveklabnik on Tuesday January 03, 2017 @06:16PM (#13314203)
Panics are definitely safe. They are significantly different than the segfault you'd get out of the C code.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @07:29PM (#13314617)
Besides of memory safety, what is different in panicking? I'm not trying to argue - I don't know it. For me result is the same - process failed. What is difference in consequences for memory?
Re:Getting Past C by gue5t on Tuesday January 03, 2017 @07:59PM (#13314794)
Not all violations of memory safety result in immediate program termination. There's an intervening period of execution during which your program simply performs unintended operations such as overwriting unrelated memory (which could be a memory-mapped file), terminating "correctly" (and performing whatever operations that entails) with corrupt state, and generally an unbounded set of other potential consequences. An immediate segfault is the absolute best hope for a program without memory safety, since you both see no unintended effects performed on the program's behalf and you learn that your program needs fixing.
Re:Getting Past C by EugeneOZ on Tuesday January 03, 2017 @08:11PM (#13314858)
Got it, thank you.
Re:Getting Past C by gpderetta on Tuesday January 03, 2017 @08:25PM (#13314918)
Panic behaviour is well defined. Segfaults are non-reliable (i.e. it might or might not crash) and often exploitable.
Re:Getting Past C by ue_ on Wednesday January 04, 2017 @07:07AM (#13317560)
There are tools you can use with C compilers that don't access OOB memory on OOB access. What's your point?

I think I'd rather not have it panic at all. They were making a language from scratch and still didn't solve the crash-at-runtime problem that C has. Instead they just made a common C compiler option default and called it 'safe'.

It's worse than the fact that the Go creators ignored years of research after the year 1970 and didn't implement generics, for no good reason.

Rust's safety is a joke, and so is the idea that a lot of it is anything 'new'. They had the chance to fix it and they didn't. Boo.

Re:Getting Past C by the_why_of_y on Wednesday January 04, 2017 @05:41PM (#13322801)
Which common C compiler has a production quality implementation of array bounds checking?

Before you say Address Sanitizer you should be aware that its authors intended it as a debugging tool and recommend against using it in production as it introduces new attack surface, as described in the "Address Sanitizer local root" mail.

http://seclists.org/oss-sec/2016/q1/363

Re:Getting Past C by steveklabnik on Wednesday January 04, 2017 @11:20AM (#13319513)
> What's your point?

My point is that panics are memory safe, contrary to what my parent said.

Re:Getting Past C by outworlder on Tuesday January 03, 2017 @07:09PM (#13314517)
I get: thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'

Looks pretty safe to me.

Re:Getting Past C by ue_ on Wednesday January 04, 2017 @07:15AM (#13317604)
How is such a crash remotely safe, for example, in an aviation system? Or a hospital's main computer?

A panic isn't safe, and if you are going to claim that it is, you can get the same safety in C using gcc and clang features.

Re:Getting Past C by dbaupp on Wednesday January 04, 2017 @11:40AM (#13319716)
Something crashing, so an error can be reported and a watchdog can restart it (etc.) is far better than just forging on with corrupt memory and wrong answers.

People do not use the sanitisers nearly as much as they should, and nor are they designed to be used in production. There's has been CVEs issued for them caused by the testing mindset in which they are written.

In any case, how much a programming language helps the programmer get a correct program (I.e. the most general form of safety) is a thing of degree, not all or nothing. Rust does a lot more than C, pushing many many errors to compile time, meaning they're fixed before the code even runs. Sanitisers only catch these when they actually happen, and will presumably result in the runtime crash you're so concerned about, if used in production. The fact that Rust isn't dependently typed and so can't catch OOB at compile time is unfortunate in this respect, but don't throw the baby out with the bath water.

Re:Getting Past C by cesarb on Wednesday January 04, 2017 @10:44AM (#13319154)
> A panic isn't safe, and if you are going to claim that it is, you can get the same safety in C using gcc and clang features.

There's one important difference. As far as I know, the bounds checking of gcc or clang can either print a warning and continue, or terminate the process.

A panic in Rust, however, will safely unwind the stack (similar to a C++ exception, in fact it's the same mechanism) and terminate only the thread. The rest of the program can continue running, and even start a new thread to replace the terminated one.

You can see this in action when running "cargo test". A panic in a test (the assert!() and assert_eq!() macros, often used in tests, do a panic in case of failure) will not terminate the whole process; the rest of the tests still run.

Re:Getting Past C by steveklabnik on Wednesday January 04, 2017 @11:22AM (#13319528)
(panics are for "non-recoverable" errors, and so don't _have_ to unwind the stack: you can abort on panic as well. The important difference is that you get well-defined behavior in all cases.)
Re:Getting Past C by macintux on Wednesday January 04, 2017 @11:22AM (#13319539)
This sounds like the "fail fast" philosophy in Erlang. Crashing is the standard behavior for any unexpected data, but the process model makes it straightforward to manage those crashes.