Lurker News
Preferably an elegant snippet rather than an entire (well engineered) codebase.

Comments (380) 116 threads

Earlier comments
saagarjha3 replies#
I'd have to think long and hard for the most beautiful code I've ever read, but I think the classic K&R "strcpy" comes pretty close:

  void strcpy(char *s, char *t) {
      while (*s++ = *t++);
  }
It's short, elegant, and quite readable to the trained eye–a bit sharp too, but if you use it right it's quite functional.
jancsika1 reply#
I've got a better code snippet: your exact same code snippet but for a language which will short circuit based on the lvalue of the assignment expression.

Then t would never be able to overflow s (nor even eat its null terminator)

jjnoakes2 replies#
The size of the buffer pointed to by s may be larger than the current string it holds. It may also be uninitialized.
saagarjha1 reply#
Or point to the wrong thing, or point to unmapped memory, or be const, or…there's a lot of things that can go wrong with this function.

Continue this thread — 1 reply

dhsysusbsjsi4 replies#
That’s what strncpy() is for

Continue this thread — 4 replies

WalterGR1 reply#
I dunno. The security ramifications of those few lines of code make me squirm. It's like looking at a very beautifully constructed foot gun.

I wonder how much damage that code has caused.

goodcanadian2 replies#
At the time the code was written, the security ramifications were not quite the same as they are now. Even now, I would suggest that there are times where such a construction would be just fine.
hestefisk#
I would say it’s clever but not beautiful code.
garmaine#
Nit, but an important one: the security ramifications were just as large than as now, they were just largely unknown at the time.
MaxBarraclough#
> quite readable to the trained eye

This kind of code is cute, but awful for readability. The author could easily (more easily!) have written the function to be very readable, but went for the cute ultra-compact style instead.
The only reason it's at all readable is that it's solving such a simple problem. Write more complex functionality in that style, and you quickly get a nightmare.
I believe MISRA C outright bans this kind of thing (three assignments with no sequence point), as it has no place in a codebase of real consequence.
I understand that some C programmers pride themselves on being able to read this kind of code (far more impressive than being able to write it), but I see no reason for it in serious software work.

segmondy2 replies#
Exploit codes are often the most beautiful code I read, they are usually small and take some dazzling brilliance to push the computer and make it do what it wasn't.

I can remember the first code that showed how to exploit IFS, race conditions via symlink, the classic "smashing the stack", RTM's worm.
Beauty of a code to me has nothing to do with the formatting, comments, documentations, but everything to do with the mind that bent it into place. Most of the beautiful code I have ever seen would be classified as ugly, spaghetti, not production worthy.

alasdair_1 reply#
>RTM's worm

I had absolutely no idea until now that RTM co-founded y-combinator. I remember him from mentioned in Bruce Sterling's The Hacker Crackdown and Clifford Stoll's The Cookoo's Egg as well as an occasional Phrack article.
https://en.wikipedia.org/wiki/Robert_Tappan_Morris

the_watcher#
He was one of PG's Viaweb cofounders as well. He also introduced PG to the third cofounder, Trevor Blackwell.
exikyut#
I have similar admiration of hardware hacks.

My current favorite is the one that dumped the SecureROM out of the iPhone 6 via PCI-e: http://ramtin-amin.fr/#nvmedma, http://ramtin-amin.fr/#nvmepcie
I'm partly impressed by the tooling used; most of the coolness (to me) is the author's self-confidence in his hunch that the SoC _didn't_ have its MMU set up properly, and the way he followed his nose in determining that he was probably right.
I still wonder exactly how much was sunk into the project, before it was possible to determine that the MMU was indeed broken. Heh.

symisc_devel2 replies#
The SQLite source tree and DRH code in general are truly piece of art. Almost every line of code is carefully commented. Despite the complexity of the project, you'll learn a lot of practical concepts including expression tree generation, bytecode execution, how to test your code and so forth.
fdsdsa_7777#
Not to mention humor -- there's a reference in SQLite to Golding's Lord of the Flies -- and I'm guessing more.
kizer1 reply#
I love the SQLite guy. His own version control, his own DB engine; you can tell from the site that he’s an individualist!
segf4ult3 replies#
He mentioned on a podcast that he even uses his own text editor.
pnako#
This reminds me of Chuck Moore (inventor of Forth). He designs his own CPUs, using his own CAD package, written in the programming language he invented!
exikyut1 reply#
Oh? What podcast?

Continue this thread — 1 reply

in91 reply#
Wow! Do you remember which podcast? Now I am interested in this interview!

Continue this thread — 1 reply

Load more comments