News for hackers. Stuff that matters.
Re:Neverflow: Set of C macros that guard against buffer over... by inetknght on Friday June 02, 2023 @08:35AM (#36164260)
Why use C and keep reinventing things that C++ provides?
Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @08:39AM (#36164287)
I keep having that discussion since the C vs C++ Usenet flamewars....
Re:Neverflow: Set of C macros that guard against buffer over... by zffr on Friday June 02, 2023 @08:49AM (#36164390)
There’s lots of software already written in C that needs to be updated and maintained
Re:Neverflow: Set of C macros that guard against buffer over... by epistasis on Friday June 02, 2023 @09:01AM (#36164506)
If one is ready to switch languages, then the clear winner is rust over C++, and I say that as someone who avoided diving into Rust for years because it seemed completely overhyped and with too much cryptic syntax.

C still wins by far when writing libraries that will be used by lots of other people. Doesn't matter what language they are using, they will be able to add in a library written in C very easily. However, C++ or Rust libraries, even with appropriate bindings for the target language, users of the library will need to bring in an entirely new compiler tool chain that may or may not exist on the target architecture. But the C tool chain will exist for that architecture and be robust.

Re:Neverflow: Set of C macros that guard against buffer over... by humanrebar on Friday June 02, 2023 @09:11AM (#36164585)
Availability of C++ tooling is much, much closer to availability of C tooling (often it's the same tool!) compared to Rust. Adopting Rust isn't the same category of conversion at all.

For new side projects, pick what you want to use of course. But for existing codebases and projects that aspire to have maximum impact, I recommend fully considering tradeoffs instead of thinking in terms of "clear winners".

Re:Neverflow: Set of C macros that guard against buffer over... by insanitybit on Friday June 02, 2023 @09:22AM (#36164712)
> Availability of C++ tooling is much, much closer to availability of C tooling (often it's the same tool!) compared to Rust. Adopting Rust isn't the same category of conversion at all.

Which tooling? Just curious, asking entirely in good faith. My recollection is that the majority of tooling I was using with C++ worked with Rust - debuggers, profilers, and sanitizers being the main tools. Although I find that I use them much less frequently since I don't find debuggers as useful for the types of bugs I have these days, and sanitizers are only useful if you have unsafe, and profilers are cool but usually I just write benchmarks using a crate and then iterate from there.

Re:Neverflow: Set of C macros that guard against buffer over... by twic on Friday June 02, 2023 @09:34AM (#36164845)
The parent comment says "compiler tool chain", and i understand "tooling" here as meaning that. So, compiler, linker, assembler, etc.

All the major C compilers are also C++ compilers, and none are (yet) Rust compilers, so out of the gate, C++ has similar availability to C.

Re:Neverflow: Set of C macros that guard against buffer over... by bfrog on Friday June 02, 2023 @09:57AM (#36165149)
If you are going on proprietary tool chains... most of those are moving to llvm which rust is based on. In theory any proprietary toolchain based on llvm could provide rustc given incentives to do so.

If you are speaking to missing a rust compiler built on gcc, that seems to be an ongoing project with some momentum.

Realistically the most widely used architectures are now supported by rustc through llvm... x86, arm, riscv, and even to some extent xtensa now.

Power, arc, mips, sparc, and some others aren't too far away if someone cared enough.

If Linux can support Rust, I'd think that's a good sign most project can use Rust.

Re:Neverflow: Set of C macros that guard against buffer over... by humanrebar on Friday June 02, 2023 @02:29PM (#36168930)
That's just the compilation toolchain. For better or worse, existing C projects have their whole workflows sitting on top of bespoke tools with the assumption that there is a C toolchain. And Rust projects assume cargo, etc. You're more or less doing a parallel rewrite in Rust to adopt Rust in an existing C project.

The Linux kernel already does extensive bespoke tooling and it's low level enough to skip cargo and such. It's rare to see that approach in Rust projects in the wild.

Re:Neverflow: Set of C macros that guard against buffer over... by epistasis on Friday June 02, 2023 @10:44AM (#36165782)
And yet, even with that, Yann Collet credits Google's use of C++ for the compression library as a critical mistake that allowed him, an unknown, to gain traction with his own compression methods. Google later rewrote their library in C:

https://overcast.fm/+LfVPHmBTo

Even if the tool chain exists, it must be adopted, unless you can rely on binaries being available for your end users, which will never be the case for a library which is just starting our. And adding another dependency to your build process, especially one as complex and with as many breaking version changes as C++, is a lot of work to take on.

Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @11:36AM (#36166574)
Unless we are talking about an obscure platform or some PIC CPU, a C++ compiler is available on the same box as the C compiler.

Second, extern "C" exists.

Third, in what concerns clang and MSVC, the C library is actually implemented in C++ with extern "C".

Re:Neverflow: Set of C macros that guard against buffer over... by epistasis on Friday June 02, 2023 @12:22PM (#36167161)
My single sentence may have been too concise, there are two concepts here: 1) the tool chain may or may not exist, and 2) bringing in that tool chain to the build system.

Even if it's the "same" toolchain for compiling C++ as it is C, adding the complexity of an additional language to the build process, and the extra versioning headaches that C++ adds over C, is enough to kill library adoption.

As I said originally, providing bindings is not the challenge, it's all the other stuff.

Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @02:39PM (#36169048)
Re:Neverflow: Set of C macros that guard against buffer over... by insanitybit on Friday June 02, 2023 @11:35AM (#36166554)
Are we just talking about portability then?
Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @11:33AM (#36166530)
Basically all the libraries, IDEs, game engines, game console SDKs, HFT, HPC, OS SDKs, embedded OSes, High Integrity Computing certifications, and plenty more stuff deployed into production since C++ ARM [0] was published in 1990, 33 years ago.

[0] - The Annotated C++ Reference Manual

Re:Neverflow: Set of C macros that guard against buffer over... by junon on Friday June 02, 2023 @04:06PM (#36170072)
That's not C++ tooling. That's tooling written in C++. Two very different things.
Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Saturday June 03, 2023 @11:33AM (#36177461)
Word games, those are domains dominated by C++, take the meaning the way it makes you happier.
Re:Neverflow: Set of C macros that guard against buffer over... by insanitybit on Saturday June 03, 2023 @05:51PM (#36180860)
You're saying word games but you're arguing something that the poster I replied to wasn't saying.
Re:Neverflow: Set of C macros that guard against buffer over... by junon on Friday June 02, 2023 @04:05PM (#36170067)
This is nonsense. Rust is based off of LLVM, which is what Clang is based off of. Name one modern, actually used, non-archaic system that LLVM doesn't run on. Beyond that, Cargo and all associated tooling run pretty much everywhere. So I'm not sure what outdated trope you're on about here.
Re:Neverflow: Set of C macros that guard against buffer over... by kllrnohj on Friday June 02, 2023 @10:01AM (#36165210)
Exporting a stable C ABI/API in no way requires writing the implementation in C. See Android's NDK for a rather widely deployed example. All the APIs are C, yet none of the implementations are C. Same thing works great in Rust, too. You can trivially export C from a Rust implementation.
Re:Neverflow: Set of C macros that guard against buffer over... by epistasis on Friday June 02, 2023 @10:33AM (#36165622)
My comment acknowledged what you state, but then went on to point out that it requires adding a tool chain to compile Rust or C++, neither of which are trivia and which may not exist at all on the target architecture.
Re:Neverflow: Set of C macros that guard against buffer over... by coliveira on Friday June 02, 2023 @10:16AM (#36165396)
People using C will not change to your language-du-jour, please stop.
Re:Neverflow: Set of C macros that guard against buffer over... by epistasis on Friday June 02, 2023 @10:38AM (#36165703)
You seem to be replying to the wrong comment, I am not suggesting that people switch away from C.
Re:Neverflow: Set of C macros that guard against buffer over... by adwn on Friday June 02, 2023 @10:51AM (#36165897)
> People using C will not change to your language-du-jour, please stop.

Two years ago, your argument would have implied that Rust would never be allowed into the Linux kernel, and yet here we are.

Re:Neverflow: Set of C macros that guard against buffer over... by netule on Friday June 02, 2023 @11:12AM (#36166231)
I wonder if Linus it taking the time to teach himself Rust.
Re:Neverflow: Set of C macros that guard against buffer over... by coliveira on Friday June 02, 2023 @03:20PM (#36169568)
There are all kinds of weird stuff in the kernel, many of them will just die.
Re:Neverflow: Set of C macros that guard against buffer over... by mikebenfield on Friday June 02, 2023 @03:40PM (#36169850)
What other programming languages are used in Linux kernel code?
Re:Neverflow: Set of C macros that guard against buffer over... by BearOso on Friday June 02, 2023 @10:37PM (#36173180)
Rust in the kernel is a whole different beast than what most people think--no standard library, no cargo or external crates, some memory safety features removed. It's kind of just an alternative syntax.
Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @11:37AM (#36166586)
They better improve their error free coding skills when liability laws come for them.
Re:Neverflow: Set of C macros that guard against buffer over... by jacquesm on Friday June 02, 2023 @11:43AM (#36166667)
This I can't wait for but the bigger problem will be that the rest of the development process is at least as broken as the languages are.
Re:Neverflow: Set of C macros that guard against buffer over... by JohnFen on Friday June 02, 2023 @02:12PM (#36168762)
Almost every dev I know who uses C (including myself) also uses other languages. Nobody should only have one tool in their toolbox.
Re:Neverflow: Set of C macros that guard against buffer over... by ActorNightly on Friday June 02, 2023 @11:14AM (#36166257)
Rust is by far not mature enough for serious development. Recent shenanigans with crablang are a strong sign of it going down the route of Java, i.e a corporate developed language with offshoots, which will end up with Rust being in the same crappy state.
Re:Neverflow: Set of C macros that guard against buffer over... by duped on Friday June 02, 2023 @11:41AM (#36166644)
> Rust is by far not mature enough for serious development

Except it's being used for serious development today

> going down the route of Java, i.e a corporate developed language with offshoots, which will end up with Rust being in the same crappy state.

So one of the most widely used applications programming languages in the world?

Re:Neverflow: Set of C macros that guard against buffer over... by ActorNightly on Friday June 02, 2023 @10:43PM (#36173218)
>Except it's being used for serious development today

No, its being used for pet projects by people. Serious development = major companies using it in backends.

>So one of the most widely used applications programming languages in the world?

Because of CS programs, and legacy software written in java. Java has a community dedicated to pushing theoretical CS concepts into the language (much like Rust), while allowing things like a logging library to fetch code from anywhere on the internet and execute it, by default (which I would bet on would be the future of Rust given current trajectory)

Re:Neverflow: Set of C macros that guard against buffer over... by c-cube on Friday June 02, 2023 @11:22PM (#36173384)
> No, its being used for pet projects by people. Serious development = major companies using it in backends.

You mean companies like Dropbox, Cloudflare, Amazon, Microsoft...? Are they too small to be relevant?

Re:Neverflow: Set of C macros that guard against buffer over... by ActorNightly on Saturday June 03, 2023 @08:05AM (#36175943)
Very few things in those companies are being written in Rust, and half of those projects chose Rust around ideological reasons rather than technical, with plenty of 'unsafe' thrown in for performance reasons

https://github.com/firecracker-microvm/firecracker/search?q=...

The fact that 'unsafe' even exists in Rust means it's no better than C with some macros.

Don't get me wrong, Rust has it's place, like all the other languages that came about for various reasons, but it's not going to gain wide adoption.

Future of programming consists of 2 languages - something like C that has a small instruction set for adopting to new hardware, and something that is very high level, higher than Python with LLM in the background. Everything in the middle is fodder.

Re:Neverflow: Set of C macros that guard against buffer over... by c-cube on Sunday June 04, 2023 @03:35PM (#36188773)
You're moving the goalposts here. Rust is being used, in significant projects (eg: proxies at Cloudflare, a company where http is somewhat of a big deal: https://blog.cloudflare.com/introducing-oxy/ ; Dropbox's new sync algorithm, a company where file syncing id kind of a big deal : https://dropbox.tech/infrastructure/rewriting-the-heart-of-o...).

Equating the existence of unsafe with C is laughable imho (it'd be barely comparable even if 100% of the rust code was in unsafe blocs, which never happens). Not even then it doesn't matter for the original point : rust is used in production for business critical functions, in large companies.

Re:Neverflow: Set of C macros that guard against buffer over... by ActorNightly on Monday June 05, 2023 @03:01PM (#36201500)
When an OS is written in Rust fully, then we can talk about acceptance.

Parts of systems written in a language doesn't really mean anything for its adoption into mainstream. For example, Amazon uses Ruby heavily for a bunch of deployment stuff, but Ruby (sans Ruby on Rails that is in decline), is not really a mainstream language any more.

>Equating the existence of unsafe with C is laughable imho

Im not comparing it. The point is to demonstrate that unsafe exists for the sole reason of performance. In fast code you often want to directly access x[y] where x and y are variables, without having to run extra code around it. Its a well known computer science thing, as most of the code challenges given in interviews rely on this access pattern for optimal solutions.

And because of Rice theorem, a compiler cannot determine whether x[y] is always safe, as determining all the values y could take would involve running the program.

So as such, for all the advantages that Rust offers, you can have the same advantages with C with macros and LLVM extensions, albeit with less concise syntax.

https://www.microsoft.com/en-us/research/project/checked-c/

Similar arguments were used to justify Haskel about 6-7 years ago, and Haskel is pretty much dead in the water at this point.

The modern way to make a memory safe language is to focus on a high level language that doesn't require programmer to deal with memory directly, and then work on the compiler to make the resultant code optimal.

Re:Neverflow: Set of C macros that guard against buffer over... by c-cube on Thursday June 08, 2023 @11:42AM (#36243787)
Re:Neverflow: Set of C macros that guard against buffer over... by properclass on Friday June 02, 2023 @09:06AM (#36164537)
the obvious answer is that one does not want some things that C++ entails, three examples: - name mangling - larger gap between source code and ISA - impedance mismatch when working with C APIs

that being said, some do not want more macros either

Re:Neverflow: Set of C macros that guard against buffer over... by adwn on Friday June 02, 2023 @10:56AM (#36165983)
> name mangling

Can be turned off on demand for relevant symbols.

> larger gap between source code and ISA

There's already a huge gap between C code and machine code (see: Undefined Behavior). C hasn't been a "portable assembler" for a very long time.

> impedance mismatch when working with C APIs

C++ has no problem working with C APIs.

Re:Neverflow: Set of C macros that guard against buffer over... by ChrisRR on Friday June 02, 2023 @09:55AM (#36165106)
For me the issue is that using C++ brings every single feature in with it. It's very easy to hire developers and they know the entirety of the C language, but using C++ has every feature you could ever want and multiple ways of achieving the same thing.

It makes writing (and hiring) a low-level project in C++ a much more complex task. It may have benefits, it may not. But C++ is so huge that it's difficult to judge whether it would offer an advantage.

And then there's the minefield of tooling in embedded development...

Re:Neverflow: Set of C macros that guard against buffer over... by hgsgm on Friday June 02, 2023 @10:23AM (#36165478)
Knowing every feature of C means they have to learn custom patterns on top of the C to make things work, and that almost always means horrific unhygienic macros.
Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Friday June 02, 2023 @10:56AM (#36165986)
But this isn't something the C++ language provides, which is hilarious.

C++ keeps C's crap array type as its native array type. You need to reach into the C++ standard library to get this awkward library type, std::array<type,N> and then finally you get an array type that remembers how big it is and has some basic features like swap.

Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @11:39AM (#36166615)
True, but it also adds lot of features that help to easily migrate to saner features without rewriting the world and throw away 30 years of tooling.

Microsoft security team is on the record that just because they are adopting Rust, they won't shy away from C++.

Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Friday June 02, 2023 @12:35PM (#36167334)
I'm kind of on board with this, but the problem is that it's 30 years of rotten wood. Rust started from a more secure foundation and has put a lot of effort into stabilising even the trickier ground - whereas in C++ it's too often "Yeah, we don't think about it too hard, when there are strong winds I don't go up into the top floor, the creaking is very loud, I'd rather just never find out".

Example, Rust 1.0 had std::mem::uninitialized::<T>() which gives a T but it's obviously uninitialized. It's marked "unsafe" of course, but is that enough? Turns out they later realised that no, it's strictly never OK to do this, so the unsafe label was insufficiently cautious. Today std::mem::uninitialized is deprecated, Rust never removes stuff from the standard library, but you should not use this library call.

The type MaybeUninit<T> is the fix. Since MaybeUninit<T> might not be initialized, it's OK if it's not initialized, and since it might be T, it's OK for it to occupy the same amount of space as T. So, then we can initialize this memory, and tell the compiler it's initialized now, it's a T not a MaybeUninit<T>.

Can you guess how that works? It's pretty clever, and C++ could do almost the same trick, but it never has and my guess is it never will. If you don't know and are wondering, check that type definition carefully - MaybeUninit<T> is a union

For contrast, in his safety talk Bjarne Stroustrup just says as if it's obviously true, that it's safe to have uninitialized char arrays in C++. And his rationale sounds exactly like how std::mem::uninitialized happened - any possible value of a byte is a valid byte, so that's good enough, right? Nope, ask compiler engineers, there were plenty in the room when Bjarne said that, but he didn't ask them.

Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Friday June 02, 2023 @02:01PM (#36168601)
Sometimes it is better to have rotten wood to build something than nothing at all.

If we want to encourage Rust adoption, it is by having a middle path, not via Rust Advocacy Strike Force.

That only shuts the audience off, specially when Rust has a glass ceiling of depending on C++ infrastructure for its reference compilers.

Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Friday June 02, 2023 @08:12PM (#36172409)
I believe conventionally they're called the Rust Evangelism Strike Force.

And it's true that the rotten wood was better than nothing. Nobody is suggesting that NT or Linux should somehow have been developed in Rust in the 1990s. But likewise we shouldn't resist renewal in newer, better materials.

That applies to compiler internals too. Plenty of trouble down there for C++, it's just that C++ programmers can more often be sent away by assuring them that what they did was UB and so LLVM is entitled to miscompile it whereas the Rust people keep arriving with the receipts, in the form of LLVM IR that is lowered to machine code which makes no sense

e.g. https://github.com/llvm/llvm-project/issues/45725

Re:Neverflow: Set of C macros that guard against buffer over... by pjmlp on Saturday June 03, 2023 @04:23AM (#36174760)
Yet if I want to contribute to Rust backend, or its upcoming GCC implementation, write C++ I must.

Same applies to the runtimes of the languages I use at work, and GPGPU related tooling when not using shaders.

Maybe then do a Go/zig/D, focus on cranelift and fully bootstrap Rust, before trying to rewrite the world.

Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Saturday June 03, 2023 @11:56AM (#36177686)
You certainly can go work on Cranelift or similar projects which have a coherent IR as a central goal rather than eh, it's probably good enough to compile C++.
Re:Neverflow: Set of C macros that guard against buffer over... by FpUser on Friday June 02, 2023 @01:19PM (#36168027)
>"std::array<type,N>"

Unless you mean array of anything like in typeless dynamic languages I do not see anything awkward about STL arrays in C++.

Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Friday June 02, 2023 @07:26PM (#36172127)
It's a standard library feature, rather than a language feature.

And you might say, "Who cares? Even freestanding has the standard library". Nope, std::array wasn't added to freestanding. You can dig into the messy details for yourself if you want, but suffice to say your freestanding C++ doesn't have std::array

So the C++ language has "arrays" but they're garbage, and if your point out that the arrays are garbage you're told to use this library feature, which may not be available.

Re:Neverflow: Set of C macros that guard against buffer over... by inetknght on Friday June 02, 2023 @10:22PM (#36173090)
The only valid complaint about std::array is that it's awkward to declare and takes more characters to type. It is, otherwise, vastly superior in every other way.

That doesn't make them garbage. That makes them annoying.

Re:Neverflow: Set of C macros that guard against buffer over... by tialaramex on Saturday June 03, 2023 @07:44AM (#36175829)
I feel like I already explained it's not even part of the language itself, it's a library feature, you aren't given this feature without the rest of the hosted C++ standard library.

Which is fine if you write Windows desktop apps, but this is an array type, unlike a GUI widget, or an XML parser, it seems like I'd probably want an array type for this $1 per unit micro controller I'm writing firmware for. In Rust the nice array type works just fine, it's a proper first class type, it knows how big it is, mutable arrays coerce into a slice I can sort (only unstably, but hey, we're embedded firmware let's not get fancy), I can iterate over it properly... in C++ only the crappy C-style array is available unless I can butcher the std::array so that it works outside the hosted library. Ugh.

Re:Neverflow: Set of C macros that guard against buffer over... by FpUser on Sunday June 04, 2023 @12:36AM (#36183146)
You do not have to butcher. Standalone allocation free implementations are available if you are so in need.

But I see that you bring Rust in here. If that's your cup of tea then use it. No need to spill venom. And if I am dealing with $1 micros I very much prefer C with some selected libs for embedded. Do not really have problems with it for such small tasks.

Re:Neverflow: Set of C macros that guard against buffer over... by FpUser on Sunday June 04, 2023 @12:42AM (#36183168)
>"takes more characters to type."

I have never perceived it as a problem. I do not think it really slows my programming. Personally I am the guy who would prefer function() vs fn() but without going into extremes of Java culture. Besides you can always alias it to whatever you want if your fingers are so sensitive.

Re:Neverflow: Set of C macros that guard against buffer over... by FpUser on Sunday June 04, 2023 @12:21AM (#36183075)
>"feature, which may not be available"

Wake me up when this happens.

Re:Neverflow: Set of C macros that guard against buffer over... by uecker on Friday June 02, 2023 @11:44AM (#36166686)
These are dependent types which C++ does not have at all. The C support is fairly weak though... But most programming language people I know agree that dependent types are they way to guard against overflow with minimal overhead. So hope we can evolve C in this direction.
Re:Neverflow: Set of C macros that guard against buffer over... by inetknght on Friday June 02, 2023 @10:23PM (#36173096)
> These are dependent types which C++ does not have at all.

As a C++ developer, that sounds strange. Can you point me to some documentation about "dependent types"?