News for hackers. Stuff that matters.
Re:Glibc getaddrinfo stack-based buffer overflow by sargun on Tuesday February 16, 2016 @10:26AM (#11110417)
I'm still unsure why people start projects with C, or C++ (or other unsafe languages), that do not need it. I totally understand that there are use cases where C++ and C make sense.

In my mind, these use cases are quite limited though - integration into 3rd party libraries, legacy codebases, and working in embedded environments. I also agree that performance can be a reason, but see Knuth's opinion on premature optimization.

I also think that when writing C, and C++ it makes a lot of sense to restrict oneself as much as possible a la the kinds of standards in the Google Style Guide, or NASA's style guide.

Can people tell me why they start projects in broad C++, and C still? I may be completely missing some piece of the puzzle.

Re:Glibc getaddrinfo stack-based buffer overflow by tangent128 on Tuesday February 16, 2016 @11:09AM (#11110777)
C is still the lingua franca; it's the only toolchain you can rely on a target system having, and the only real usable interface for calling between code in different languages.

So my theory is that it's not so much that people are especially inclined towards starting projects in C, but that projects in C have fewer barriers to distribution and thus adoption.

By natural selection, C code spreads and becomes standard; programs in Java, JS, Ruby, Haskell, Ocaml, etc. don't tend to leave their native ecosystem.

Re:Glibc getaddrinfo stack-based buffer overflow by groovy2shoes on Tuesday February 16, 2016 @12:24PM (#11111474)
This is exactly my reason for starting a couple of recent side-projects in C. You can pretty much count on the target system having an ANSI C compiler (even rather old systems), which can't be said for any other language. Have an ANSI C compiler? Great! You don't need anything else to build and install this program. You don't need to install a hefty runtime like the JVM and you don't need to know what a gem is.

I don't think I'm immune to the hazards inherent in the language. I try my best be aware of them and to write safe code, and still find myself introducing a segfault now and then. Luckily, tools like valgrind and Coverity can really help to catch these kinds of issues early (of course, they can't catch everything, but I've been amazed by just how much they do catch).

Re:Glibc getaddrinfo stack-based buffer overflow by autoreleasepool on Tuesday February 16, 2016 @11:50AM (#11111167)
> Can people tell me why they start projects in broad C++, and C still? I may be completely missing some piece of the puzzle.

Sure.

---

Compelling features:

- Unrivaled portability (yes, contrary to folklore C - and to a lesser extent C++ - is more portable than Java)

- Excellent libraries

- Excellent documentation and learning resources

- Unrivaled tooling

- perfect mapping to how the computer works

- Unrivaled performance (other than Rust)

- exceptional, engaging, and intelligent community (specifically referring to the cpp community)

---

Why start a C++ project today?

- you want to write a web browser

- you want a platform agnostc GUI

- you want to learn OpenGL or Vulkan

- you want to use Unreal and you're not a visual learner

- you're want to write the business logic for a cross platform application and share as much code as possible

- you're implementing you're own programming language

- your implementing an algorithm or data structure you've been studying

- you're bored

- you like programming in C++

- you want to learn template meta-programming

- you want to learn functional programming

- you want to learn how to write a device driver

- you need the performance

- you are not writing mission critical software (aka 99% of all side projects)

- you're a curious person

- you're a contrarian and everyone you know uses Java

- you want to become a more well-rounded engineer

- you want to learn about the ins and outs of language design and features

- you want make a lot of money

- you only know C++

Those are a few reasons. Hope that helps you solve your puzzle.

PS: Do the anti-C people realize that a kernel in Rust/Nim/whatever would be under a big unsafe block anyways? Yeah, you have to program "unsafe" code eventually. Those nice high level abstractions are not omnipresent, you know. They need something to abstract.

Re:Glibc getaddrinfo stack-based buffer overflow by greiskul on Tuesday February 16, 2016 @04:52PM (#11113396)
Just like a kernel written in C is a big block of assembly code, right? You can isolate any unsafe blocks into small functions, with the smallest scope needed. Same as you would do for asm blocks in C. This is good because it calls attention to what you are doing, when reviewing any code with an unsafe block, you remind yourself to check for these kinds of bugs very carefully, unlike when you are reviewing generic C code where almost every line can have a memory bug.
Re:Glibc getaddrinfo stack-based buffer overflow by steveklabnik on Tuesday February 16, 2016 @01:48PM (#11112145)

  > a kernel in Rust/Nim/whatever would be under a big unsafe block anyways?
Only portions of it do. The whole point of unsafe is that you can encapsulate it, and then go from there.
Re:Glibc getaddrinfo stack-based buffer overflow by autoreleasepool on Tuesday February 16, 2016 @02:00PM (#11112249)
Definitely. The ability to encapsulate code that is less strictly checked by the compiler is what makes Rust such a compelling alternative to C. I definitely think Rust is the ideal choice for a hobby kernel from scratch in 2016.

My point is that unsafety is unavoidable. You can encapsulate unsafe Rust or wrap plain old C, it's, for all practical purposes, the same. At that level language features matter a lot less. I agree we should minimize and encapsulate loosely checked code as best we can.

Re:Glibc getaddrinfo stack-based buffer overflow by steveklabnik on Tuesday February 16, 2016 @02:08PM (#11112312)
Yeah, absolutely. Ultimately, the machine is not safe. Such is life.
Re:Glibc getaddrinfo stack-based buffer overflow by bcook on Tuesday February 16, 2016 @08:14PM (#11114438)
Exactly... and users are even worse.
Re:Glibc getaddrinfo stack-based buffer overflow by pjmlp on Tuesday February 16, 2016 @10:45AM (#11110571)
Sometimes there isn't any other choice.

For example, in the mobile OS space if you want to write native apps, without rewriting 100% of the application code per platform and avoid toolchain debugging headaches, C and C++ are the only languages available in iOS, Android and WP SDKs.

In WP case, there are APIs like DirectX that aren't fully exposed via WinRT, so you need to write a C++ wrapper for those type of APIs.

There are other options, but they are either commercial (e.g. Xamarin), or require lots of DYI hacking with the SDKs toolchains.

So if you want to remain productive and not pay for the commercial solutions, there aren't any other alternatives.

Which means, even I with my safety rants, end up using them for my mobile OS hobby coding.

However if I would be doing this commercially, Xamarin would be my first option.

Re:Glibc getaddrinfo stack-based buffer overflow by tosseraccount on Tuesday February 16, 2016 @11:42AM (#11111070)
Isn't slapping C onto objective C trivial for iOS and OSX? Just wrap to the GUI's objective C. You can use this trick for WTL on Window's C++, too.
Re:Glibc getaddrinfo stack-based buffer overflow by jsmthrowaway on Tuesday February 16, 2016 @12:28PM (#11111508)
People seem to forget that Objective C is a superset of C. No slapping on required. You're already writing it. Avoid brackets and a few other things and it's C.

You don't even have to rename .m to .c, but you can.

Re:Glibc getaddrinfo stack-based buffer overflow by creshal on Tuesday February 16, 2016 @11:09AM (#11110778)
> I'm still unsure why people start projects with C, or C++ (or other unsafe languages), that do not need it. I totally understand that there are use cases where C++ and C make sense.

On Linux it's easy: It is (or was, until very recently) the least sucky alternative. All others have (or had) issues with OS support (C#), distribution (Java: OpenJDK vs. Oracle JDK; Python: 2.5 to 3.4 parallel in use by different distributions; Vala: Every compiler update breaks the language; etc. pp.), availability of libraries (Ruby, PHP, Perl… for anything that's not their speciality) or general obscurity (OCaml, …).

Go and Rust (and JS, C# and Vala recently) seem to be changing this, thankfully. But those too will depend on the availability of high-quality bindings for all the C(++) libraries you have to use to get anything substantial done.

Re:Glibc getaddrinfo stack-based buffer overflow by d0lph on Tuesday February 16, 2016 @11:21AM (#11110875)
Written properly, C++ is not unsafe.

Certain language own certain domains, for better or worse, this means that most developers interested in low level programming will know C/C++, and that the will be a substantial amount of tooling already in place.

Re:Glibc getaddrinfo stack-based buffer overflow by cmrx64 on Tuesday February 16, 2016 @11:41AM (#11111062)
> Written properly, C++ is not unsafe.

This is abusing what "unsafe" means. As a language, C++ is inherently unsafe. However, some C++ programs ("written properly") are correct.

If written properly C++ is not unsafe, then written properly C is not unsafe.

Re:Glibc getaddrinfo stack-based buffer overflow by SCHiM on Tuesday February 16, 2016 @12:28PM (#11111504)
No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.

Eg. Don't use C++ as C with vectors, use C++ with the more modern language features:

It's the difference between:

    // where f is of type fstream of a file containging "this is a string\r\n"No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.
Eg. Don't use C++ as C with vectors, use C++ with the more modern language features:

It's the difference between:

    std::string A = "ab" + std::string("c");

    // And:
    char Buffer[512] = {};
    strcat(Buffer, "ab");
    strcat(Buffer, "c");
Or alternatvily:

    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end());

    // and:
    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData;
    OtherData.reserve(3);
    memcpy(...)

In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code.

    std::string A = "ab" + std::string("c");

    // And:
    char Buffer[512] = {};
    strcat(Buffer, "ab");
    strcat(Buffer, "c");
Or alternatvily:

    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData(SomeData.begin(), SomeData.end());

    // and:
    std::vector<uint8_t> SomeData = { 1,2,3 }
    std::vector<uint8_t> OtherData;
    OtherData.reserve(3);
    memcpy(...)

In the first one you're 'protected' by the stl code, which is arguably the same level of protection as afforded by any memory safe language’s library code.
Re:Glibc getaddrinfo stack-based buffer overflow by dbaupp on Tuesday February 16, 2016 @01:39PM (#11112077)
> No I think he meant that if you adhere to certain language constructs the language is just as memory safe as Rust/others.

It's not; as soon as code uses (for instance) references, you're running the memory-unsafety gantlet. E.g. even something super-modern like

  for (auto& thing: vector)
is risky: what if the loop body happens to somehow call `vector.push_back(x)`?
Re:Glibc getaddrinfo stack-based buffer overflow by tomjakubowski on Tuesday February 16, 2016 @02:35PM (#11112513)
> is risky: what if the loop body happens to somehow call `vector.push_back(x)`?

Some people like to say that these bugs are trivial to avoid ("who would write code like that?!"), but they miss the insidious (and common) case: when code several layers down the call stack (in some arbitrary callback assigned at runtime, say) grows the vector/map from under you.

Re:Glibc getaddrinfo stack-based buffer overflow by kccqzy on Wednesday February 17, 2016 @02:44AM (#11116063)
It's still easy to avoid though. Just take a const reference to the vector and use that in the loop and pass that to the callback. It just requires discipline, and in fact less discipline than writing C.
Re:Glibc getaddrinfo stack-based buffer overflow by gpderetta on Tuesday February 16, 2016 @12:36PM (#11111579)
I don't subscribe to the "properly written Modern C++ is safe" meme, but still it must be recognized that there are degrees of unsafety [1] between languages.

[1] not talking about memory safety, but as a general "probability of having a defect".

Re:Glibc getaddrinfo stack-based buffer overflow by cmrx64 on Tuesday February 16, 2016 @01:44PM (#11112119)
I really dislike the "safe"/"unsafe" terminology, there isn't a commonly-agreed upon definition of "safe". But I think it's safe to say that it's a combination of type and memory safety, not "low probability of having a defect".

There certainly is a spectrum.

Re:Glibc getaddrinfo stack-based buffer overflow by tosseraccount on Tuesday February 16, 2016 @11:36AM (#11111014)
Because you can't release a single file statically compiled native binary that relies only on established system calls. People typically can't install packages anymore. They can grab an exe.
Re:Glibc getaddrinfo stack-based buffer overflow by w8rbt on Tuesday February 16, 2016 @12:32PM (#11111534)
This is the reason I ported several Python and Java apps to C++. One exe and it just works.
Re:Glibc getaddrinfo stack-based buffer overflow by J_Darnley on Tuesday February 16, 2016 @03:06PM (#11112752)
> Can people tell me why they start projects in ... C still?

Okay. Based on my current project (Porting AVS out of shit-tier Microsoft C++). I want a region of memory in which I can treat as a bitmap and set pixels at given location to given colours. I know of no other language that gives me this block of memory to draw into. That's probably because I don't know enough. Most languages seem to tout how immutable things are. That's a big word for many people and it just means "does not change".

If other languages want my attention then maybe they need more than a "Hello, World!" and some string processing. Show me your malloc equivalent and see if I can go from there.

Re:Glibc getaddrinfo stack-based buffer overflow by GregBuchholz on Wednesday February 17, 2016 @12:00PM (#11119247)
>I want a region of memory in which I can treat as a bitmap... ...Show me your malloc equivalent and see if I can go from there.

http://www.lispworks.com/documentation/HyperSpec/Body/f_mk_a...

https://wiki.haskell.org/Numeric_Haskell:_A_Vector_Tutorial#...

Re:Glibc getaddrinfo stack-based buffer overflow by J_Darnley on Wednesday February 17, 2016 @02:46PM (#11120669)
Thank you.