Live data from Hacker News

Weekend projects: getting silly with C

lcamtuf.substack.com

71–80 of 118 posts

Re: Weekend projects: getting silly with C

#71

Earlier quoted context omitted.

Duff is relying on the fact you're allowed to intermingle the switch block and the loop in K&R C's syntax, the (common at the time but now generally frowned on or even prohibited in new languages) choice to drop-through cases if you don't explicitly break, and the related fact that C lets your loop jump back inside the switch. Duff is trying to optimise MMIO, you wouldn't do anything close to this today even in C, no…

> Duff is relying on the fact you're allowed to intermingle the switch block and the loop That's just a special case of being able to intermingle switch with arbitrary syntax, which is what TFA does, before it jumps to computed gotos.

The overarching point appears to be getting rid of angle brackets, which is not something that Duff is doing. Further, Duff's device keeps case labels on the left of its control structure; moving ifs to the left is the other "innovation" here.

I think you really have to squint your eyes to see the similarities, beyond the general theme of exploiting the counterintuitive properties of switch statements.

Re: Weekend projects: getting silly with C

#72
post #4

Another source of surprise: 4[arr] // same as arr[4]

By the same principle, these are exactly the same:

  arr[i][j]
  j[i[arr]]
These are the simplifications you'd do. You only need to know that a[x][y] is equivalent to (a[x])[y], and that a[x] is the same as x[a].

  arr[i][j]
  (arr[i])[j]
  (i[arr])[j]
  j[i[arr]]

Re: Weekend projects: getting silly with C

#74
post #7

If only there was a way of using setjmp/longjmp-style contexts instead of goto, un/winding the stack as required. So we could travel around in time... unfortunately you can't work with a setjmp buffer before it's actually created, unlike gotos.

sigaltstack tricks to the rescue! (Although POSIX only, not ISO C)

Re: Weekend projects: getting silly with C

#75
post #48
post #43

Earlier quoted context omitted.

I'm inclined to trust Raymond Chen and John Regehr on these matters, so if you assert that they're incorrect here then a source to back up your assertion would help your argument.

I am a member of WG14. You should check the C standard. I do not see how "time-travel" is a possible reading of the definition of UB in C. We added another footnote to C23 to counter this idea: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf "Any other behavior during execution of a program is only affected as a direct consequence of the concrete behavior that occurs when encountering the erroneous or non…

While interactions with volatile and interactive streams cannot time-travel, anything else is free to - the standard only imposes requirements on a conforming implementation in terms of the contents of files at program termination, and programs with undefined behaviour are not required to terminate, so there are approximately no requirements on a program that invokes undefined behaviour.

Re: Weekend projects: getting silly with C

#76
post #66
post #42

Earlier quoted context omitted.

I don't think I've ever thought of explicitly calling main(). Made me chuckle.

I think it is UB Edit: actually looks like it is UB in C++ but not C

Why would calling main be UB!? How is crt0 supposed to work?

Re: Weekend projects: getting silly with C

#78
post #66

Earlier quoted context omitted.

I think it is UB Edit: actually looks like it is UB in C++ but not C

Why would calling main be UB!? How is crt0 supposed to work?

crt0 generally isn't C and isn't subject to C's rules

Re: Weekend projects: getting silly with C

#79
post #53
post #49

Why did I not know that this: case 1 ... 10: Is valid C? I have been programming in C for years, what standard is this from?

It appears to be a GNU C extension: https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html but I couldn't find the history of the extension. I believe it is not in standard C (not sure about clang).

Clang supports almost all GNU C extensions. Maybe not nested functions because they need executable stacks.

Re: Weekend projects: getting silly with C

#80

Earlier quoted context omitted.

Why would calling main be UB!? How is crt0 supposed to work?

crt0 generally isn't C and isn't subject to C's rules

malloc() can't be implemented in C either because it's defined as doing things (creating new memory objects) there are no lower level mechanisms in C to do.
Post reply on HN