News for hackers. Stuff that matters.
Re:The Curious Case of the Longevity of C by colechristensen on Tuesday October 03, 2017 @04:10AM (#15390928)
I'm quite bored by the obsession of some for replacing C.

Yes, it does exactly what you tell it to do and that's dangerous. It was a high level language 25 years ago, but today the metaphor should be assembly. Nobody would criticize assembly for letting you shoot yourself in the foot, C is much the same.

The biggest threat to the tech sector is Linus dying and being replaced by some charlatan who insists on replacing C and using Jira.

</offtopic>

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @05:20AM (#15391178)
I would be (mostly) fine with C for the purposes it's designed for if each C compiler were required to have an option that makes the compiler use implementation-defined behavior everywhere. This would make C more like a portable assembler and less like the software engineering version of "do you feel lucky, punk?"

As it is, lots of major C applications already do this on a piecemeal basis and use one or more of:

  -fno-strict-aliasing
  -fno-strict-overflow
  -fno-delete-null-pointer-checks
with gcc to keep a lid on the effects of undefined behavior (and often more or even stricter options, such as -fwrapv).

The problem is that even experienced C programmers can get tripped up by undefined behavior all the time. "Yes, it does exactly what you tell it do" doesn't really cut it when the vast majority of actual humans cannot safely predict the effects because they're so unintuitive. Or, in the words of Douglas Adams:

  "But the plans were on display . . ."
  "On display? I eventually had to go down to the cellar to
  find them."
  "That's the display department."
  "With a torch."
  "Ah, well the lights had probably gone."
  "So had the stairs."
  "But look, you found the notice, didn't you?"
  "Yes," said Arthur, "yes I did. It was on display in the
  bottom of a locked filing cabinet stuck in a disused
  lavatory with a sign on the door saying 'Beware of the
  Leopard'."
Re:The Curious Case of the Longevity of C by josephg on Tuesday October 03, 2017 @10:20AM (#15392571)
> with gcc to keep a lid on the effects of undefined behavior (and often more or even stricter options, such as -fwrapv).

Speaking of which, a killer feature I'd like to see in a C compiler is a flag for debug mode which SIGABRTs whenever it enters an undefined behaviour codepath. For example, sometimes the compiler knows that if pointers alias, or an integer overflows or something, it can do whatever it wants. I want a flag which will automatically add assertions to the generated code, so at least in debug mode it'll crash if thats ever actually the case.

Re:The Curious Case of the Longevity of C by jchb on Tuesday October 03, 2017 @10:56AM (#15392809)
It exists. Clang has UBSan which can detect many undefined behaviour (https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#...). It's integrated into Apple's Xcode 9, which can pause into the debugger when encountering an issue. I was actually using it to debug an issue just as I read your comment.
Re:The Curious Case of the Longevity of C by iainmerrick on Tuesday October 03, 2017 @11:55AM (#15393290)
It exists, but it doesn't do anything in the example that was discussed recently, "undefined behavior may call a never-called function": https://news.ycombinator.com/item?id=15324414

As far as I can tell, -O0 is the only way to avoid that particular mis-optimization in Clang.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @10:50AM (#15392774)
This cannot be done for all undefined behavior (because that would be too expensive, if at all possible), but you can use the `-fsanitize` family of options in both gcc and clang to at least get runtime errors for quite a few important cases of undefined behavior.
Re:The Curious Case of the Longevity of C by colechristensen on Tuesday October 03, 2017 @05:46AM (#15391262)
How possible do you think it is to create a new language which is almost, but not quite, exactly the same as C?

Edge cases that have evolved into it do have plenty of problems, but it's very difficult to create a new backwards-incompatible standard which is just a little bit different.

Then again, the embedded (microcontrollers) world seems to be filled with slightly esoteric implementations of C and everybody gets along just fine.

Re:The Curious Case of the Longevity of C by CJefferson on Tuesday October 03, 2017 @08:02AM (#15391789)
Almost impossible to do efficiently. Let's consider a tiny example:

    void copy(int* in, int* out) {
       for(int i = 0; i < 10; ++i)
       { *out = *in; ++out; ++in; }
    }
Now, in C it is undefined behaviour if either o in or out point into the stack where the variables i, in and out live. Maybe their values will change, maybe they won't. In practice, this code will be optimised to some code that just stores in and out in registers.

Now let's imagine we want to make it defined. Then the statement '* out = * in' would have to become something like:

1) Write the current values of i, in and out to memory (in case * in overlaps with any of their locations.

2) Read * in into a register

3) Write *out from that register

4) Re-read i, in and out from memory, in case they just changed.

Now, you could make that more efficient by adding to the start of the function a check which looked to see if either 'in' or 'out' pointed anywhere near the stack, but there is still going to be a significant cost. If you plan for this from the start with your language, you can drive the costs much lower.

The main thing that separates C from assembler is that you do not have to worry about what values are in registers, and which are in memory. However, that means any code which writes to memory provides a place where this can make a difference.

Re:The Curious Case of the Longevity of C by Asooka on Tuesday October 03, 2017 @11:50AM (#15393247)
Oh, you can fix it pretty simple

"The compiler can always assume writes to memory via one object don't change values in memory accessed via any other object."

Then, if you know your code can overlap, you can use some invalidate function to mark that "memory accessed via this object was changed by some statement prior, make sure to re-read values cached in registers". Because that's the odd case where you're doing something unusual. In general, "The compiler is allowed to optimise things assuming that spooky action at a distance isn't happening". You already have something very similar for atomics, which might be hidden behind a standard library, but in any case are a sequence of "lock the bus, read/write, memory barrier", telling your CPU and your compiler that memory access cannot be reordered around the memory barrier.

Re:The Curious Case of the Longevity of C by Sohcahtoa82 on Tuesday October 03, 2017 @07:00PM (#15396944)
> Now, in C it is undefined behaviour if either o in or out point into the stack where the variables i, in and out live

Seems to me that that could only happen if you had some other bug clobbering the values of in and out, or you're passing something to the function that you shouldn't be. In either case, it's an error on the side of the programmer.

> If you plan for this from the start with your language, you can drive the costs much lower.

It shouldn't be the job of the language or compiler to make up for the shortcomings of the programmer besides maybe code generation optimization.

Re:The Curious Case of the Longevity of C by staticassertion on Tuesday October 03, 2017 @11:00AM (#15392848)
You can express what you're talking about (non aliasing mutable pointers) for free, at compile time.

In fact, you can't express this in C (or, restricted, whatever), so I do wonder if your assumption that because it's UB it'll compile to the optimal code is true.

Re:The Curious Case of the Longevity of C by CJefferson on Tuesday October 03, 2017 @11:25AM (#15393009)
I think you are thinking about the memory that 'in' and 'out' point to aliasing each other. That's handled with the "restricted" keyword, as you say.

I'm talking about something else, the issue of 'in' and 'out' pointing into the stack, so writing to them changing the value of the local variables. That is basically treated as UB by every C compiler, and no compiler I am aware of provides any switches which would make it easier to handle, it's just horrible and your code behaves in different ways depending on the compiler and optimisation level.

Re:The Curious Case of the Longevity of C by InternetOfStuff on Tuesday October 03, 2017 @06:38AM (#15391430)
> Then again, the embedded (microcontrollers) world seems to be filled with slightly esoteric implementations of C and everybody gets along just fine.

But embedded teams often severely limit the language constructs you may use, in order to avoid the foot howitzers and only have to worry about the foot handguns.

There are also many other techniques (such as pre-allocating all variables during initialisation) which can be considered a mitigation techniques for the many traps C offers.

I'm also skeptical of the quality of a lot of embedded software (and I've seen quite a bit of it).

This is not to say that C is bad, or that I disagree with you. But embedded is a bad example.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @06:15AM (#15391350)
As I said, I think that basically doing a global search and replace of "undefined" with "implementation-defined" in the standard would be a good first attempt (this should be possible, because that's how C compilers worked for a long time and – with optimizations turned off – generally still do). This wouldn't solve everything, but it makes the use case for C as a "portable assembler" better, which could then be used as a foundation to build upon.
Re:The Curious Case of the Longevity of C by naasking on Tuesday October 03, 2017 @09:54AM (#15392404)
> How possible do you think it is to create a new language which is almost, but not quite, exactly the same as C?

See Friendly C:

https://blog.regehr.org/archives/1180

https://blog.regehr.org/archives/1287

Re:The Curious Case of the Longevity of C by edmccard on Tuesday October 03, 2017 @04:33AM (#15391003)
>Nobody would criticize assembly for letting you shoot yourself in the foot, C is much the same.

But I bet they would criticize programs for being written in assembly, if they didn't need to be.

If you could have a language with all the performance of C without the footguns, why wouldn't you want that?

Re:The Curious Case of the Longevity of C by chii on Tuesday October 03, 2017 @05:01AM (#15391105)
> a language with all the performance of C without the footguns, why wouldn't you want that?

I've yet to see a language that actually delivered on this claim.

Re:The Curious Case of the Longevity of C by bluejekyll on Tuesday October 03, 2017 @05:07AM (#15391128)
Perhaps you aren’t looking?

Rust delivers on all these claims. And there have been others before it. Rust hits all the sweet spots for me.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @07:33AM (#15391644)
Rust sits in a really weird spot. It's too high-level for a lot of low-level work, and too low-level for a lot of high-level work.

Example for the first case: Writing a garbage collector runtime in Rust has most of the same problems in Rust as in C, because you have to write most of it in unsafe code, where Rust inherits much of C's undefined behavior w.r.t. pointers via LLVM. In short, you have largely the same problems and have added a hard dependency on Rust.

For high-level work, almost all [1] of what Rust gives you is memory safety and that comes at the price of dealing with a LOT of extra language complexity. But aside from dynamic memory management, memory safety isn't hard (we did that back in the 1970s and 1980s), and for dynamic memory management, we can get memory safety with a garbage collector and much less complexity. So Rust is primarily of interest for those use cases where garbage collection is not an option.

While that still gives you plenty of interesting use cases for Rust, there are also plenty of programming niches that it serves poorly.

[1] People will also mention "fearless concurrency", but guaranteeing the absence of data races is not hard. That more languages don't do it is partly because they simply neglected that aspect [2], but also because any mechanism – including Rust's – for doing so inherently constrains your options w.r.t. concurrency [3]. Plus, avoiding data races is the easy part of getting concurrency right.

[3] Concurrent Pascal had guaranteed absence of data races in the absence of pointers in the 1970s, Eiffel had done it with pointers in the 1980s, and there was a plethora of research in the 1990s to do it in various other ways.

[3] For example, there are plenty of use cases, such as certain idempotent operations, where data races are not only perfectly safe, but also desired for performance. There are also use cases where you can prove that no data races occur, but a type system cannot easily capture that.

Re:The Curious Case of the Longevity of C by tdbgamer on Tuesday October 03, 2017 @09:12AM (#15392136)

  It's too high-level for a lot of low-level work, and too low-level for a lot of high-level work.
This is true in the very specific cases that you gave, but I believe that is the minority of use cases, not the majority.

Even the example of writing a GC that requires tons of unsafe code, that is not a good argument for making all the code unsafe. All the unsafe GC code would be abstracted away into a module and would be more obvious to those looking at it that they will need to be watchful for undefined behavior. Now you can proceed writing the rest of the project in safe, simple Rust.

  People will also mention "fearless concurrency", but guaranteeing the absence of data races is not hard
Maybe for developers that are very familiar with the race conditions of parallel code, but definitely not for most people. Even seasoned developers will make mistakes with simple multithreaded code.

Also, the reasoning behind "x is easy so why do I need my language to check it for me" is questionable. The whole point is that you have a guarantee. Have you never had a compiler catch a stupid mistake before it happened and felt relieved? I doubt it. Now imagine if instead of debugging stupid data races in your parallel code you can spend that time optimizing and improving it. I fail to see how this can be viewed as negative.

Sure Rust doesn't cover 100% of use cases, but it definitely covers more than you're implying. It's low-level enough that Redox OS can be written in Rust, but high-level enough that Firefox is now outpacing other browsers and parallelizing everything with Rust.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @09:37AM (#15392284)
> All the unsafe GC code would be abstracted away into a module and would be more obvious to those looking at it that they will need to be watchful for undefined behavior.

That code that could be "abstracted away" would be "virtually all the code" in my example.

> Maybe for developers that are very familiar with the race conditions of parallel code, but definitely not for most people. Even seasoned developers will make mistakes with simple multithreaded code.

I'm not talking about manually guaranteeing absence of data races. I mean absence of data races as a language feature.

> Also, the reasoning behind "x is easy so why do I need my language to check it for me" is questionable.

This is not at all what I was talking about. You completely misunderstood me.

Re:The Curious Case of the Longevity of C by steveklabnik on Tuesday October 03, 2017 @09:49AM (#15392371)
I think you'd be surprised, even operating systems, the canonical unsafe activity, has a relatively low percentage of unsafe code. For example, https://doc.redox-os.org/book/introduction/unsafes.html says

> A quick grep gives us some stats: the kernel has about 70 invocations of unsafe in about 4500 lines of code overall.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @10:06AM (#15392489)
> I think you'd be surprised, even operating systems, the canonical unsafe activity, has a relatively low percentage of unsafe code.

My example was a GC runtime, not an OS kernel. If I have only very little unsafe code, then I could just do that in C and the rest in whatever other high-level language suits my project and not see any difference.

The bigger problem – where Rust failed to pick some low-hanging fruit, IMO – is that "unsafe" is too much like the bad parts of C. There is no medium position between "everything is defined and memory-safe" and "everything may explode at a moment's notice".

My most practical need for a low-level language is a language that is in that in-between position: semantics that remain easy to comprehend and predictable even if there are no static guarantees, and where I have to use a different strategy for software assurance. The point here is that for such a language I can resort to alternate validation tools (think Ada and SPARK for an example). Rust's unsafe mode does not handle that situation well because (like C) it does not provide a foundation for alternate validation strategies.

It's perhaps also worth pointing out that I have a formal methods background. In short, I've done formal specifications/proofs for software before. In this context, safe Rust has a fairly high cost for only providing memory safety (and few other guarantees), and unsafe Rust is not a good foundation (or at least, not much better than C) for bringing advanced tools to bear.

Re:The Curious Case of the Longevity of C by jononor on Tuesday October 03, 2017 @11:12AM (#15392928)
There are several things that make unsafe Rust better than C wrt to ensuring correctness. For example the stronger, more expressive type-system and fewer instances of undefined behavior for common operations. The standardized, modern tools for unit-testing and fuzzing are also nicer in Rust.
Re:The Curious Case of the Longevity of C by adwhit on Tuesday October 03, 2017 @10:18AM (#15392559)
You might be interested in this paper [0] wherein the authors implement a high-performance GC in Rust. Quoting the abstract:

We find that Rust’s safety features do not create significant barriers to implementing a high performance collector. Though memory managers are usually considered low-level, our high performance implementation relies on very little unsafe code, with the vast majority of the implementation benefiting from Rust’s safety. We see our experience as a compelling proof-of-concept of Rust as an implementation language for high performance garbage collection.

[0] http://users.cecs.anu.edu.au/%7Esteveb/downloads/pdf/rust-is...

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @10:26AM (#15392606)
That seems to be a bit misleading. What they seem to do, inter alia, is expose memory addresses as a safe type in Rust, with pointer arithmetic and dereferencing simply declared safe without it actually being so. There is no check that the underlying address actually points to valid memory, satisfies aliasing rules, etc.
Re:The Curious Case of the Longevity of C by burntsushi on Tuesday October 03, 2017 @10:51AM (#15392778)
Did you read the paper? Dereferencing is not "simply declared safe." There's an entire section of the paper that goes over the API of the Address type, and explicitly points out that dereferencing is considered unsafe. Their conclusion runs directly contrary to your stated claims:

> We found that the Rust programming model is quite restrictive, but not needlessly so. In practice we were able to use Rust to implement Immix. We found that the vast majority of the collector could be implemented naturally, without difficulty, and without violating Rust’s restrictive static safety guarantees. In this paper we have discussed each of the cases where we ran into difficulties and how we overcame those challenges. Our experience was very positive: we enjoyed programming in Rust, we found its restrictive programming model helpful in the context of a garbage collector implementation, we appreciated access to its standard libraries (something missing when using a restricted language such as restricted Java), and we found that it was not difficult to achieve excellent performance. Our experience leads us to the view that Rust is very well suited to garbage collection implementation.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @11:07AM (#15392905)
Oops, I meant to write pointer conversion, not dereferencing. Yes, I read the paper, as I was taking that directly from the Rust code shown in the paper.

The underlying problem is that Rust's borrow checker cannot possibly capture at compile time the arbitrary relations between objects managed by a garbage collector and arbitrary object layouts. Whatever guarantees it provides, memory safety isn't one of them (and cannot be), even if nominally only part of it is unsafe code. Pointer arithmetic and memory safety do not mix.

What you get out of that is basically an alternative approach to information hiding, not memory safety.

Re:The Curious Case of the Longevity of C by burntsushi on Tuesday October 03, 2017 @11:41AM (#15393151)
It sounds like you're shifting the goal posts pretty dramatically to me. Compare what you said above (which presumably prompted the reference to this paper)

> Writing a garbage collector runtime in Rust has most of the same problems in Rust as in C, because you have to write most of it in unsafe code, where Rust inherits much of C's undefined behavior w.r.t. pointers via LLVM. In short, you have largely the same problems and have added a hard dependency on Rust.

to the conclusions drawn in the cited paper. They sit in pretty stark contrast from where I'm standing.

Re:The Curious Case of the Longevity of C by rbehrends on Tuesday October 03, 2017 @12:08PM (#15393425)
I'm not sure where you're getting the "goal post shifting" from. Writing "p.plus(n)" in their Rust code is not materially different from what "p + n" would be in C/C++ code. That the former is nominally safe code doesn't change the fact that the same unsafe Rust code gets inlined at every callsite. You could do the same in C++ by creating an Address class (similar to smart pointers) with restricted operations; these operations would not magically become safer just because they're inlined by the compiler rather than spelled out in the code.
Re:The Curious Case of the Longevity of C by nine_k on Tuesday October 03, 2017 @08:52AM (#15392030)
Your critique of Rust can be largely applied to C++. Maybe the latter is a niche language, but that niche was not served by many offerings up until recently, and C++ is still going strong, despite being less safe than Rust.
Re:The Curious Case of the Longevity of C by bitwize on Tuesday October 03, 2017 @07:58PM (#15397268)
> But aside from dynamic memory management, memory safety isn't hard (we did that back in the 1970s and 1980s), and for dynamic memory management, we can get memory safety with a garbage collector and much less complexity.

At a severe cost in performance. Static object lifetimes cover 99.9% of a garbage collector's use cases, without the performance cost of GC, nor the nondeterministic runtimes. We first saw static object lifetimes come into their own with C++'s value semantics; Rust refines and clarifies the idea and makes memory safety an inherent part of the language itself.

Static object lifetime is to GC what static types are to dynamic types. Lisp is 1960s tech. It has failed, and been replaced with something much better.

Re:The Curious Case of the Longevity of C by rbehrends on Wednesday October 04, 2017 @05:18AM (#15399188)
I think your terminology is off. Static lifetime means that objects exist for the entire duration of the program and do not affect memory management at all, automated or manual. If you're talking about automatic variables, then things already become more complicated.

For starters, we have to assume that we don't deal with value types (which will end up on the stack, one way or the other), but with local variables that reference heap objects. Second, we have to distinguish between tracing and reference-counting GCs.

A modern tracing garbage collector will have cost for such temporary allocations comparable to `alloca()` and those allocations will typically be inlined. The cost of deallocating a short-lived stack object is zero (yes, zero). This is possible because GCs (unlike manual memory management schemes) are compacting. Whether one approach or the other comes out on top is very situational.

More importantly, I dispute the 99.9% as a vast exaggeration. There are plenty of important use cases (such as persistent data structures, shared caches, etc.) where unique ownership is insufficient; Rust requires you to use either copying or reference counting when you run into shared ownership scenarios, both of which are more expensive than tracing GC (naive reference counting is already one of the more expensive memory management methods known, and atomic reference counting is especially expensive).

If you use reference-counting GC, then for any program that satisfies Rust's borrow checker, the optimizer can eliminate reference counts that satisfy the same conditions (assuming that the optimizer knows about them because they're part of the language semantics). This is largely what Swift does, for example.

Finally, there is deferred reference counting, which incurs only trivial overhead for objects with automatic lifetime (on the order of a fraction of a percent). This is because this algorithm incurs real cost only when pointers are written to global or heap locations; this is also why it's seen limited use in practice: it's excellent for objects with automatic lifetimes and does not rely on the generational hypothesis, but those do not constitute 99.9% of all use cases. If they were, deferred reference counting would have a far more prominent role.

This does not even account for the fact that when there is overhead, that overhead is generally trivial in an imperative language with value types.

There are use cases, of course, where a tracing GC is an inappropriate choice, but that is not because of throughput. Tracing GCs make interoperability with other GCs different, for example, and have implicit memory overhead that may be prohibitive in large applications such as a web browser (that can easily consume gigabytes of memory on a laptop or desktop machine). That said, there are alternative approaches to garbage collection that do not have those problems.

Re:The Curious Case of the Longevity of C by beefhash on Tuesday October 03, 2017 @07:34AM (#15391651)
Rust delivers on all these claims on a very limited set of platforms. The support for BSDs is abysmal, with only x86_64 NetBSD and i686/x86_64 FreeBSD even being "guaranteed to build", while OpenBSD, Windows XP and Solaris sit in some kind of state of hopelessness[1].

Rust is not a replacement for C in the sense of portability. People love simplifying the world into Windows/macOS/Linux, but that is not all you may want to target.

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

Re:The Curious Case of the Longevity of C by staticassertion on Tuesday October 03, 2017 @11:05AM (#15392888)
"All of the performance of C without the footguns"

Nowhere in this sentence do I see "for every platform C targets".

Given no other requirement other than C without the footguns (memory unsafe) is there a good reason not to use the safe version? I'd say there are some, but they aren't crazy compelling (ie: developers have to learn rust, maybe harder to hire for, etc).

Re:The Curious Case of the Longevity of C by boomlinde on Wednesday October 04, 2017 @03:10AM (#15398785)
It's still a very realistic reason that someone would prefer C over Rust. Having to learn Rust, it being harder to hire for, etc. are really just as irrelevant to the named requirements, but important concerns nonetheless.
Re:The Curious Case of the Longevity of C by bluejekyll on Tuesday October 03, 2017 @10:27AM (#15392616)
What you point out can be fixed with time and resources. C has had 40 years to be ported to all of these other platforms. Rust has been realeased and stable for a little over 2.
Re:The Curious Case of the Longevity of C by zik on Tuesday October 03, 2017 @06:32AM (#15391408)
Rusty has a significantly higher cognitive load for programmers than C. I find it quicker to write reliable code in C than in Rust because I can write the C code and a comprehensive set of tests quicker than I could write just the Rust code with no tests.
Re:The Curious Case of the Longevity of C by naasking on Tuesday October 03, 2017 @10:03AM (#15392465)
> I find it quicker to write reliable code in C than in Rust because I can write the C code and a comprehensive set of tests quicker than I could write just the Rust code with no tests.

Tests can't establish the absence of bugs the way Rust can. You only think your C code is reliable, you don't actually know that it's reliable. Rust only appears harder because of the latent bugs in your C program that you're not aware of.

Re:The Curious Case of the Longevity of C by mannykannot on Tuesday October 03, 2017 @07:35AM (#15391658)
Testing does not perfectly substitute for verification, and vice-versa. In particular, comprehensive testing does not scale: at some point in the growth of your code, your testing will no longer be comprehensive, even if it started that way. On the other hand, no amount of static analysis will eliminate semantic errors (but neither will testing.)
Re:The Curious Case of the Longevity of C by dragonwriter on Tuesday October 03, 2017 @10:41AM (#15392707)
> Rusty has a significantly higher cognitive load for programmers than C.

For simple cases, this may be true, for complex cases it but shifts the cognitive load up front. Which may be more frustrating, but also may prevent a large class of hard to identify, intermittent in manifestation, bugs from getting into production. Which also saves programmer frustration.

Re:The Curious Case of the Longevity of C by steveklabnik on Tuesday October 03, 2017 @06:36AM (#15391424)
How much time have you spent with Rust? This kind of thing will obviously vary with the individual.
Re:The Curious Case of the Longevity of C by flukus on Wednesday October 04, 2017 @03:37AM (#15398871)
I've been doing the same lately, I don't know how it would scale to a bigger project but I've been enjoying it so far. I've been running the tests under valgrind as well which has found 1 or 2 issues a lot faster than debugging would. The best part of that is that valgrind shows me actual errors in actual running code whereas the rust compiler shows potential errors.
Re:The Curious Case of the Longevity of C by steveklabnik on Wednesday October 04, 2017 @04:17AM (#15398998)
Valgrind works with Rust too, to be clear. Most similar tools, like AFL do too.
Re:The Curious Case of the Longevity of C by z3phyr on Tuesday October 03, 2017 @05:38AM (#15391236)
Rust is very sweet, but it lacks the simplicity of C. I get that ML style programming is all the rage today, and ML itself is a simple language, but programming languages today carry a lot of baggage and styles, maybe to cater to wide range of programmers. But in the end, it complicates the language. I have noticed that a lot of people find Rust or Scala to be very hard because of these reasons.
Re:The Curious Case of the Longevity of C by hsivonen on Tuesday October 03, 2017 @06:00AM (#15391301)
The simplicity of C relative to Rust is an illusion in the sense that the main thing that makes Rust feel difficult at first ("fighting the borrow checker") relates to a concern that's relevant to C (allocation lifetimes), but in the case of C the burden of dealing with the issue is on the programmer and not on the compiler.
Re:The Curious Case of the Longevity of C by Ygg2 on Tuesday October 03, 2017 @06:37AM (#15391429)
> Rust is very sweet, but it lacks the simplicity of C.

That's like saying a SawStop (http://www.sawstop.com/) lacks a simplicity of a sawblade. I mean - Yeah, sure, but sawblade also won't stop you from turning yourself into amputee.

I understand Rust can be overly verbose, but main complexity comes from the borrow checker and the effect adding another kind of type, to track lifetimes. The lifetime system is the main selling point.

There are other sources of complexity in Rust, but I am glad to say both Rust/Scala seem to be looking for a way to simplify things.

Re:The Curious Case of the Longevity of C by blub on Tuesday October 03, 2017 @06:57AM (#15391504)
Perhaps if the SawStop would complain about using certain types of wood after another, forced you to pick the wood from the pile in a certain order, etc. I bet woodworkers would be quite annoyed by that.
Re:The Curious Case of the Longevity of C by junkcollector on Tuesday October 03, 2017 @07:50AM (#15391726)
This is in fact what the Sawstop does because if you use the wrong material it will engage and destroy your very expensive saw blades. This is one of the (many) reasons why SawStops aren't more popular and why they are in fact removable. So really, the sawstop is like Rust in that you can do what you want when it considers it dangerous by embedding c code, but it requires you to put in an annoying level of effort.
Re:The Curious Case of the Longevity of C by Const-me on Tuesday October 03, 2017 @09:17PM (#15397603)
> Rust delivers on all these claims

No it doesn’t.

One reason is rust doesn’t have SSE/AVX/Neon intrinsics.

Without them, you can’t get anywhere near all the performance of C, nor anywhere near advertised performance of any modern CPU.

Re:The Curious Case of the Longevity of C by steveklabnik on Wednesday October 04, 2017 @03:02AM (#15398758)
Sometimes the compiler will autovectorize, but you're right that these are important. That's why it's actively being designed; it's a temporary limitation, not an inherent property.
Re:The Curious Case of the Longevity of C by throwme211345 on Tuesday October 03, 2017 @09:23AM (#15392193)
Rust is the ugliest, least palatable, alternative to C for me. Seems like they said "Let's chain function calls together like java(script) as the preferred expression and throw in lispy looking declarations and logic constructs. When people get frustrated with our safety paradigm there will always be unsafe..for experts."
Re:The Curious Case of the Longevity of C by luckydude on Tuesday October 03, 2017 @10:38AM (#15392685)
Yeah, I feel pretty much the same way. I get that there are a lot of sharp edges in C but I don't see why every attempt to do a new language wants to throw the baby out with the bathwater. Why not do a C dialect that fixes whatever it is that bugs people? There is value in the Rust language, why couldn't that value have been added to a C like language?
Re:The Curious Case of the Longevity of C by zurn on Wednesday October 04, 2017 @04:08AM (#15398975)
There have been attempts. Safe-C and Cyclone come to mind.
Re:The Curious Case of the Longevity of C by pjmlp on Tuesday October 03, 2017 @10:30AM (#15392631)
Pascal compilers up to the mid 90's, were as fast as C compilers.
Re:The Curious Case of the Longevity of C by mokus on Tuesday October 03, 2017 @02:57PM (#15395046)
> a language with all the performance of C without the footguns

IMO, this isn't even the right goal. The problem is that in many cases using C is itself a premature optimization. Consider two languages:

* C, which gives you performance by default at the cost of an entire arsenal of footguns with esoteric nonlocal trigger conditions

* Hypothetical language X, which has all the same footguns but engineered for safer triggering and locked up in a safe that you have to choose to open when you want C-like performance

I'd rather have hypothetical language X (which is an accurate portrayal of many real existing languages), because it's got better failure modes. Performance issues are less impactful, in general, and more importantly they are more obvious. It is usually easy to tell when code is not fast enough. The endless parade of CVEs ultimately deriving from memory safety issues, often decades old, is living proof that misuse of the footguns is frequently far from obvious.

Re:The Curious Case of the Longevity of C by colechristensen on Tuesday October 03, 2017 @05:34AM (#15391221)
I have serious doubts that anybody would actually create such a language. The basic problem with C is the ease in which you're allowed to shoot yourself in the foot or in other words the great pedantic lengths you have to go to in order to not do so.

A language that avoided it would have to be very close to C while making it only mildly more difficult to foot shoot. Everyone who is trying is overshooting and therefore not really writing an adequate replacement low level systems language. Even if they did, it would be so close to C that adoption pickup would be low, awkward, and the language would fail outright. (Think Python 3 but worse)

>But I bet they would criticize programs for being written in assembly, if they didn't need to be.

Certainly. If you're writing your web app in assembly you are very likely a crazy person unless your goal is to do something ridiculous. If you're writing some assembly in a critical path in a web server to precisely control the network stack, you might not be a crazy person (see Netflix tech blogs).

Re:The Curious Case of the Longevity of C by edmccard on Tuesday October 03, 2017 @06:20AM (#15391367)
>A language that avoided it would have to be very close to C while making it only mildly more difficult to foot shoot.

Says who? A language where every single memory access did NOT have the potential to be a buffer overflow would be much more difficult to foot-shoot with, even if it allowed you to sometimes access memory with raw pointers. Just having the ability, as in Rust, to mark code as either safe or unsafe goes a long way towards preventing footguns.

>Everyone who is trying is overshooting and therefore not really writing an adequate replacement low level systems language.

Using Rust as an example again, I don't think there's anything about being able to say "this code right here cannot cause memory unsafety" that precludes "low-level systems" programming.

Re:The Curious Case of the Longevity of C by cyphar on Tuesday October 03, 2017 @06:26AM (#15391388)
Rust delivers on many of the use-cases, so much so that there is a full operating system (kernel and userland) implemented in Rust called Redox[1]. I understand being critical of hype, but Rust is legitimately a very interesting and exciting language.

[1]: https://www.redox-os.org/

Re:The Curious Case of the Longevity of C by pjmlp on Tuesday October 03, 2017 @10:32AM (#15392647)
Ada, Modula-2, Mesa, PL/8, ESPOL, Object Pascal and many others come to mind.
Re:The Curious Case of the Longevity of C by moosingin3space on Tuesday October 03, 2017 @11:57AM (#15393310)
> I'm quite bored by the obsession of some for replacing C.

Well, I'm quite bored of the endless stream of memory safety vulnerabilities that can be systematically eliminated with memory-safe languages like Rust. Note that every single one of the CVEs disclosed yesterday in dnsmasq are memory safety violations, and the fact that buffer overflows, data races, and other related errors are not only pervasive, but also tend to sit around for years in codebases[1].

> Nobody would criticize assembly for letting you shoot yourself in the foot, C is much the same.

It's completely acceptable to criticize the choice of any tool. I can and do criticize the use of C for any code that touches a network and in some cases consider it willful negligence, as using a non-memory-safe language to parse untrusted data is asking for trouble.

[1]: https://twitter.com/johnregehr/status/914663997647069184

Re:The Curious Case of the Longevity of C by tempodox on Tuesday October 03, 2017 @12:56PM (#15393880)
When you speak of “parsing untrusted data”, do you mean to imply that “parsing trusted data” even exists? I for one would view any and all input as untrusted.
Re:The Curious Case of the Longevity of C by moosingin3space on Tuesday October 03, 2017 @01:58PM (#15394496)
"Trusted data" can exist in theory -- on a closed network where you control all endpoints -- but does not exist in practice (as many devices eventually connect to the Internet or exchange data with Internet-connected computers).

Therefore, it follows that re-writing parsers in memory-safe languages would provide a nice bang-for-buck.

Re:The Curious Case of the Longevity of C by TheCoelacanth on Tuesday October 03, 2017 @08:30PM (#15397424)
In some cases, say the parser for an interpreter for a general purpose programming language, I would consider the data "trusted". You are already allowing execution of arbitrary code, so there is nothing to be gained by exploiting the parser.
Re:The Curious Case of the Longevity of C by zokier on Tuesday October 03, 2017 @06:10AM (#15391329)
As others have already alluded, the problem with C is not its low level nature or that it allows you to shoot yourself in the way asm does. The real problem are the arcane unintuitive rules of ISO C standard that in comparison to asm footgun is more like carrying a critical lump of plutoniun in your pocket that might go off at any point.
Re:The Curious Case of the Longevity of C by staticassertion on Tuesday October 03, 2017 @11:02AM (#15392870)
> I'm quite bored by the obsession of some for replacing C.

That's fine... but the billions of dollars spent on security and the massive trove of user data that's been exfiltrated due to C is kinda a problem regardless of whether you are bored or not.

> Nobody would criticize assembly for letting you shoot yourself in the foot, C is much the same.

I wouldn't criticize a developer for writing bad code. I'd criticize the language for letting them, or the developer for choosing such a language. Same with C and Assembly.

> The biggest threat to the tech sector is Linus dying and being replaced by some charlatan who insists on replacing C and using Jira.

That is... not the biggest threat. It seems extremely unlikely that that is where linux kernel development would go.

Re:The Curious Case of the Longevity of C by sidlls on Tuesday October 03, 2017 @11:48AM (#15393222)
It isn't "due to C." This is a popular meme on HN, but it's incorrect in its focus.

If anything replaces C I'm skeptical that more than a shift in the character of the attack surface (rather than the area) will occur. That is, there will be just as much to attack, it's just that the attacks will require alternate methods. Some attacks will be harder to employ, some less, but on average probably about the same quantity and seriousness will occur.

Re:The Curious Case of the Longevity of C by staticassertion on Tuesday October 03, 2017 @12:17PM (#15393516)
> It isn't "due to C." This is a popular meme on HN, but it's incorrect in its focus.

You can call it a meme but that doesn't change my opinion.

> Some attacks will be harder to employ, some less, but on average probably about the same quantity and seriousness will occur.

This is unfounded, I don't really know how to respond to it other than that nothing has ever really worked this way in security. You don't get less secure in one area because you were more secure in another.

Re:The Curious Case of the Longevity of C by sidlls on Tuesday October 03, 2017 @02:00PM (#15394515)
> You don't get less secure in one area because you were more secure in another.

I agree with you, however that doesn't address what I actually wrote.

Re:The Curious Case of the Longevity of C by staticassertion on Tuesday October 03, 2017 @02:52PM (#15394988)
"Some attacks will be harder to employ, some less" - I'm interpreting this sentence that way.
Re:The Curious Case of the Longevity of C by bluejekyll on Tuesday October 03, 2017 @04:25AM (#15390975)
> it does exactly what you tell it to do and that's dangerous.

gcc: “hold my beer while I optimize out that block of code you clearly didn’t mean to be in your program”

Re:The Curious Case of the Longevity of C by DougBTX on Tuesday October 03, 2017 @04:48AM (#15391054)
The example of optimizing in a block of code that is never explicitly called is even more fun: https://kristerw.blogspot.co.uk/2017/09/why-undefined-behavi...
Re:The Curious Case of the Longevity of C by jwilk on Tuesday October 03, 2017 @06:59AM (#15391514)
Re:The Curious Case of the Longevity of C by hashmal on Tuesday October 03, 2017 @04:31AM (#15390996)
That's not because of C, that's because of GCC.
Re:The Curious Case of the Longevity of C by int_19h on Tuesday October 03, 2017 @04:37AM (#15391020)
gcc does it precisely because the C standard says that it can do so, and any program that's relying on not getting broken that way is not valid C (well, "undefined behavior", but it's the same thing in practice).
Re:The Curious Case of the Longevity of C by userbinator on Tuesday October 03, 2017 @05:28AM (#15391201)
gcc does it precisely because the C standard says that it can do so

Or rather, because the standard "imposes no requirements". A compiler that does the obvious/sensible thing for UB can also claim conformance.

From C99 3.4.3 paragraph 3 (emphasis mine): "Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message)."

Re:The Curious Case of the Longevity of C by pkolaczk on Tuesday October 03, 2017 @05:19AM (#15391176)
Where exactly does C standard explicitly say it can do so?

UB was introduced to allow skipping checks that otherwise might have added overhead, like array bound checks or null checks or to give more flexibility to how correct programs are optimized (e.g. freedom to evaluate arguments in any order). Therefore each compiler/library/os vendor could implement these cases differently.

However, it was never meant to allow compiler to assume "ok, I can see your code is totally broken, so let's break it more, cause you don't care anyway".

Re:The Curious Case of the Longevity of C by loup-vaillant on Tuesday October 03, 2017 @08:11AM (#15391832)
> However, it was never meant to allow compiler to assume "ok, I can see your code is totally broken, so let's break it more, cause you don't care anyway".

That's nevertheless how compiler writers are interpreting the standard right now. Take integer overflow for instance. Stuff like this:

  unsigned u = some_computation();
  int x = u;   // because of reasons
  x += 42;     // hmm this may overflow, let's check that
  if (x < 0) { // It's a 2's complement machine, this'll work
      handle_overflow(x); // now, let's deal with the overflow
  }
Now here's how your optimising compiler see that stuff:

  unsigned u = some_computation();
  int x = u;   // x is always positive.
  x += 42;     // signed integer never overflow
  if (x < 0) { // x was positive, no overflow... always false
      handle_overflow(x); // Let's delete this dead code.
  }
Then, what actually happens:

  unsigned u = some_computation();
  int x = u;
  x += 42;
This is insane.
Re:The Curious Case of the Longevity of C by smitherfield on Tuesday October 03, 2017 @11:01AM (#15392860)
A correction: a compiler would not be able to assume that `x` is always positive because casts between signed and unsigned integer types are based on their binary representation (what computers see), not their value (what humans see).[1]

[1] https://wandbox.org/permlink/dDDAJjOlm50GD1PW

It would be able to (correctly) make that assumption if `x` were a long, assuming 64-bit longs and 32-bit int/unsigneds.

Re:The Curious Case of the Longevity of C by int_19h on Tuesday October 03, 2017 @03:39PM (#15395422)
In the C standard, a cast from unsigned to signed integer is undefined behavior if it overflows. So the compiler can, indeed, assume that "x" is always positive in the code snippet above - if the value of "u" is such that it would require signed wraparound when initializing "x", the rest of the program is U.B., and so that possibility doesn't even have to be considered.
Re:The Curious Case of the Longevity of C by smitherfield on Tuesday October 03, 2017 @04:52PM (#15396080)
This is untrue; see section 6.3.1.3:

When a value with integer type is converted to another integer type ... [if] the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1570.pdf

So casts from unsigned to signed must have a well-defined result, but the implementation can choose how to define it. You could in theory have an implementation that defines that casts from unsigned to signed are always positive,[1] but since that's not how GCC and every other mainstream (existing?) compiler defines it, they are not allowed to assume the result of a cast from an arbitrary unsigned integer to a signed integer of equal width is positive.

[1] In which case the compiler would be allowed to assume that unsigned-to-signed casts are never negative. This assumption would however be both safe and correct, and would have nothing to do with UB.

Re:The Curious Case of the Longevity of C by Avernar on Tuesday October 03, 2017 @02:27PM (#15394753)
I always wished C gave access to the overflow and carry flags. Would have made a lot of things simpler.
Re:The Curious Case of the Longevity of C by Bromskloss on Tuesday October 03, 2017 @07:08PM (#15396989)
> // It's a 2's complement machine, this'll work

Let's see, that's not an assumption you can make in C, or is it? I seem to remember that a compiler is allowed to present to the programmer at least one or two representations other than two's complement.

Re:The Curious Case of the Longevity of C by int_19h on Wednesday October 04, 2017 @05:15PM (#15404765)
From C99 on, it's either one of: two's complement, ones' complement, or sign + magnitude.
Re:The Curious Case of the Longevity of C by Bromskloss on Wednesday October 04, 2017 @06:57PM (#15405469)
That's sounds familiar, so it's probably what I've read. What did previous versions say?
Re:The Curious Case of the Longevity of C by int_19h on Wednesday October 04, 2017 @09:12PM (#15406121)
"The type char, the signed and unsigned integer types. and the enumerated types are collectively called integral types. The representations of integral types shall define values by use of a pure binary numeration system."

And a footnote on that:

"A positional representation of integers that uses the binary digits 0 and 1, in which the values represented by successive bits are additive, begin with 1, and are multiplied by successive integral powers of 2, except perhaps the bit with the highest position."

Re:The Curious Case of the Longevity of C by steveklabnik on Tuesday October 03, 2017 @05:32AM (#15391217)
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf is the draft of the C11 spec,

3.4.3 undefined behavior

behavior, upon use of a nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

-------------

Even if you want to argue over what "imposes no requirements" means, I'd argue that "ignoring the situation completely with unpredictable results" is very clear.

The note also says (and elsewhere in the standard it reinforces this) that since there are no requirements, implementations could absolutely do things like perform runtime checks, etc. Compilers do not do this though. This isn't for no reason, but that's a separate issue.

Re:The Curious Case of the Longevity of C by reacweb on Tuesday October 03, 2017 @05:20AM (#15391177)
Before the standard, C compilers applied a principle of least surprise. The standardisation has created a huge mess by leaving so many undefined behaviours were compilers could do as they want.
Re:The Curious Case of the Longevity of C by adrianratnapala on Tuesday October 03, 2017 @04:37AM (#15391018)
It's because of C as it has been redefined in recent times, but the kinds of people who write standards and compilers.

The galling thing is that they did this to win a benchmark war against Fortran.

Re:The Curious Case of the Longevity of C by stinos on Tuesday October 03, 2017 @06:10AM (#15391330)
I'm equally bored with the apparent obsession of some to hate on Jira. It's a dev tool like any other meaning for some it works wonderfully well with some quirks here and there. Hmm, like C, I'd say. And I use and like both.
Re:The Curious Case of the Longevity of C by jsight on Tuesday October 03, 2017 @12:55PM (#15393870)
Well, in fairness, JIRA has tried to evolve well beyond the role of a mere bugtracker. Between the complex workflows of its large existing userbase, the huge complexity of the code, and the desire to use it for project management, it has become really slow and painful both to use and to develop.

I've heard some horror stories and apparently the development team is pretty heavily pressured to fix a lot of things.

Re:The Curious Case of the Longevity of C by tonyedgecombe on Tuesday October 03, 2017 @04:40AM (#15391033)
Yes, it does exactly what you tell it to do and that's dangerous.

Exactly, if you could get all the benefits without the danger why wouldn't you?

Re:The Curious Case of the Longevity of C by danieltillett on Tuesday October 03, 2017 @04:41AM (#15391036)
I don't worry about Jira, but I do worry about someone replacing C.
Re:The Curious Case of the Longevity of C by pjmlp on Tuesday October 03, 2017 @10:27AM (#15392612)
25 years ago it was a middle-level language.

I hardly saw any reason to use it instead of Pascal dialects, besides being "the language" on UNIX systems.

Thankfully Bjarne created C++.

Re:The Curious Case of the Longevity of C by booleandilemma on Tuesday October 03, 2017 @07:47AM (#15391716)
Has Linus ever talked about appointing a successor?
Re:The Curious Case of the Longevity of C by throwme211345 on Tuesday October 03, 2017 @09:03AM (#15392084)
I love you man.