News for hackers. Stuff that matters.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by SavageBeast on Thursday August 04, 2022 @12:31PM (#32345078)
I haven't thought about C in years - that one where author passes in a printf as a char array element and de-references it to execute it ... gives me chills. Y'all kids have fun - Im going to stick with my VM over here and call it a day. This makes even modern JS look sane by comparison. Excellent write up too.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by nneonneo on Thursday August 04, 2022 @12:55PM (#32345372)
Nit: the char array doesn’t get dereferenced at all. The entire computation happens as a side effect of computing the length of the array. This is more of a case where there’s an unexpected expression context that can be abused for fun.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by SavageBeast on Thursday August 04, 2022 @01:03PM (#32345484)
The fact the array element isn't in quotes bothers me to some degree - with or without its crazy but the fact that the compiler just accepts it as a language construct as opposed to a value makes me want to drink another beer. But again Im thinking dereferencing here - and as you said thats not the case.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by nneonneo on Thursday August 04, 2022 @01:09PM (#32345550)
It’s not an array element :) The printf statement is part of the length expression, i.e. it’s setting the length of the array to the result of the printf call. So it is indeed a “value”.

This isn’t much different from writing something like this in JS:

    var a = [];
    a[console.log("Hello"), 1] = 42;
except that this indexes the array as opposed to setting its length.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by SavageBeast on Thursday August 04, 2022 @01:46PM (#32345978)
I spent the past 10 minutes figuring out what to search for (C is very rusty here) regarding C arrays and initialization. Now that you point it out, it seems obvious. It certainly wasn't obvious when I read it though. This is some really obtuse use of a language here - hilariously so really. The code in the array is executed as a function that determines the array size and I see that now - thanks. If someone on my team did this for any reason I'm not sure If Id shoot them or put them in charge of something more important. If no other thing - THIS is why code reviews exist. Still it's an impressive use of a compiler. Thanks for nit picking - this has been very entertaining!
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by shawnz on Thursday August 04, 2022 @08:25PM (#32350053)
The feature is called variable length arrays (VLAs)

See https://www.ibm.com/docs/en/i/7.4?topic=arrays-variable-leng...

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by asguy on Thursday August 04, 2022 @02:38PM (#32346668)
> I haven't thought about C in years ... Im going to stick with my VM over here and call it a day.

What's your VM written in?

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by Banana699 on Thursday August 04, 2022 @04:25PM (#32347870)
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by asguy on Thursday August 04, 2022 @04:50PM (#32348158)
Except for most of deno that's written in Unsafe C++.

https://v8.dev/

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by Banana699 on Thursday August 04, 2022 @05:34PM (#32348583)
A JS runtime is a lot more than the core engine, or else nodejs is just a thin wrapper over V8 as well, which is obviously absurd. So without a count of the source lines for each language's portion of the implementation (very rough but acceptable first approximation of how much is implemented in each), you can't claim C++ is doing most of the heavy lifting and be taken seriously.

PS. the worst C++ code is still a hell of a lot more safe than the best C code. This is easy to convince yourself of by noting that C++ mostly supersets C and only deviates to add more safety and static checking, not less. So it's a strict improvment over C, not by a lot, but an improvment nonetheless.

Here are other examples for VMs not written in any of those 2 braindead languages though:

[1] Bun : JS runtime in Zig. https://bun.sh/

[2] Squeak : Smalltalk VM written in Smalltalk. http://www.vpri.org/pdf/tr1997001_backto.pdf

[3] PyPy : Not actually a single VM but an entire framework\toolchain to write VMs, most famous of which is one for Python. The language used to write VMs for the framework is a subset of python called Rpython. https://doc.pypy.org/en/latest/

[4] Maxine Virtual Machine : a JVM written entirely in Java. It's not the only one. https://en.wikipedia.org/wiki/Maxine_Virtual_Machine ; https://news.ycombinator.com/item?id=15733645

Modern VM research is far beyond the 50 year old assembler that thinks itself a programming language. The future is here, just not very evenly distributed.

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by astrange on Friday August 05, 2022 @01:14AM (#32352011)
> PS. the worst C++ code is still a hell of a lot more safe than the best C code.

What? For safe code you need to be able to audit it for logic errors, which you can't do if you can't understand what it does by reading it.

That's why especially fancy code is banned in projects like Linux.

[dead]
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by Banana699 on Friday August 05, 2022 @04:17AM (#32353179)
The only one coping here is that who doesn't understand VMs and language research enough and retreats into childish "it was just a joke lol" remarks when out of their depth.

Incidentally, I write all 3 of C, C++ and x86 assembly, which is why I understand how braindead and unfit for human usage they are.

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by asguy on Friday August 05, 2022 @09:57AM (#32356181)
I didn't say I was "just joking", but calling C++ "unsafe" was definitely something I think was funny to say.

> Incidentally, I write all 3 of C, C++ and x86 assembly, which is why I understand how braindead and unfit for human usage they are.

I can't take you seriously when you make statements like this. You have to be trolling.

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by kllrnohj on Thursday August 04, 2022 @06:31PM (#32349147)
Probably not C regardless. How many widely used VMs are in C? Python is about it, right?
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by Kamq on Thursday August 04, 2022 @08:39PM (#32350140)
Java's? Or at least it used to be.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by kllrnohj on Thursday August 04, 2022 @09:36PM (#32350444)
Openjdk seems to be more c++ than c according to GitHub anyway. In particular the "interesting" stuff (so hotspot) looks to be all c++.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by asguy on Thursday August 04, 2022 @09:44PM (#32350503)
It started as C. It’s interesting to watch people post comments, as though C vs C++ makes the distinction in “safety” in a code base.

The take away I’m getting is most have programmed in neither.

Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by legalcorrection on Thursday August 04, 2022 @10:21PM (#32350759)
These people are just regurgitating HN memes and then flailing when someone prods a bit deeper. C is basically the Donald Trump of HN. All rational thought goes out the door at the mere mention. In fact, you’re socially rewarded for joining the idiot mob.
Re:C99 doesn't need function bodies, or 'VLAs are Turing com... by lsof on Thursday August 04, 2022 @02:49PM (#32346807)
That printf exploit is quite impressive I think.