Live data from Hacker News

Let's Destroy C

gist.github.com

51–60 of 192 posts

Re: Let's Destroy C

#51
post #48
post #44

I guess this article is to be taken lightly as kind of a "joke" ? The first proposed macro displayln is the archetype of malpractice. What if I want to do: if(some_condition) displayln("enjoy debugging that"); etc etc... almost all proposed changes seem to be awful ?

I think I was pretty explicit: > What if, for a moment, we forgot all the rules we know. That we ignore every good idea, and accept all the terrible ones.

Ow alrighty then. Looking at comments here I'm not sure some other people saw that sentence haha

Re: Let's Destroy C

#52
post #45

> printf("%s\n", "Hello, World!"); > > That's an awful lot of symbolic syntax. Well... Because it should have been printf("Hello, World!\n"); in the first place? One can do something like printf("%s,%s%c\n", "Hello", "World", '!'); and claim that C is awful and that displayln("Hello, World!"); is so much better.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

If the string was in a variable, yes, but in this case most implementations will put it in a RO section.

Re: Let's Destroy C

#53
post #50

Earlier quoted context omitted.

Could be. But wouldn't then displayln() require the same string placeholder? Be it "%s", or "{1}" or someting else.

displayln uses the massive _Generic in display_format to supply a large number of formatters automatically. [0] [0] https://gist.github.com/shakna-israel/4fd31ee469274aa49f8f97...

I see. So when for a char pointer one needs to do

        printf("%p\n", v);
the Generic call must look like this

        displayln((const void *)v);
is it really better?

Re: Let's Destroy C

#54
post #45

> printf("%s\n", "Hello, World!"); > > That's an awful lot of symbolic syntax. Well... Because it should have been printf("Hello, World!\n"); in the first place? One can do something like printf("%s,%s%c\n", "Hello", "World", '!'); and claim that C is awful and that displayln("Hello, World!"); is so much better.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

Can you please exemplify how you exploit a printf("Hello, World!\n") ?

Re: Let's Destroy C

#55
post #52
post #45

Earlier quoted context omitted.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

If the string was in a variable, yes, but in this case most implementations will put it in a RO section.

That'll be depending on undefined behaviour, though, correct?

Re: Let's Destroy C

#56
post #45

> printf("%s\n", "Hello, World!"); > > That's an awful lot of symbolic syntax. Well... Because it should have been printf("Hello, World!\n"); in the first place? One can do something like printf("%s,%s%c\n", "Hello", "World", '!'); and claim that C is awful and that displayln("Hello, World!"); is so much better.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

> No. You don't really want to do that. If you're doing that, use puts

This is not the case for RO strings, is it?

Re: Let's Destroy C

#57
post #54
post #45

Earlier quoted context omitted.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

Can you please exemplify how you exploit a printf("Hello, World!\n") ?

That's a format string attack [0].

By modifying the start of that string, you can begin reading and writing to various parts of the stack.

Whilst implementations may inline that string into a RO memory region - that's not defined behaviour, so you shouldn't depend on it.

[0] https://owasp.org/www-community/attacks/Format_string_attack

Re: Let's Destroy C

#58
post #45

Earlier quoted context omitted.

displayln is a _Generic. All of these are valid: displayln("Hello, World!"); displayln(100); displayln(1.8); The point is, for simple things, to not have to specify how they appear. > Well... Because it should have been > printf("Hello, World!\n"); No. You don't really want to do that. If you're doing that, use puts [0] . All this requires is a modification to one string in memory and you have an injection vulnerabil…

> No. You don't really want to do that. If you're doing that, use puts This is not the case for RO strings, is it?

You're not guaranteed to have a RO string by the standard are you?

Re: Let's Destroy C

#59
My friend served as a military officer and told me a story. Every evening before lights out, he had to count soldiers via a roll call, a 10-minute routine. If someone coughed during that (no matter what intent), then suddenly many soldiers began to cough, and that ruined everything. The only solution he figured out to remain legal/moral was to pause and start it all over if someone coughs.

Re: Let's Destroy C

#60
post #50

Earlier quoted context omitted.

displayln uses the massive _Generic in display_format to supply a large number of formatters automatically. [0] [0] https://gist.github.com/shakna-israel/4fd31ee469274aa49f8f97...

I see. So when for a char pointer one needs to do printf("%p\n", v); the Generic call must look like this displayln((const void *)v); is it really better?

Or you could have the pointer be a pointer before you feed it to the function.

    void* v;
    v = ...;

    ...

    displayln(v);
Post reply on HN