by senozhatsky · 6y ago · 0 comments
> 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. double foo = 1.2;
printf(foo);
puts(foo);
won't compile, while double foo = 1.2;
display(foo);
works fine.Incidentally I actually think display is the only thing on this list that is probably worth using, you could also probably extend it to accept multiple arguments relatively simply as well.
puts(“Hello, World!”)
which has been in C since the dawn of time?Specifically, printf is an oddball function because it uses the varargs mechanism, and the whole format strings mechanism is inherently risky because it effectively bypasses the type system and says "trust me." Back when I was learning C, on a Mac with THINK C, misusing printf was a sure-fire way to crash the computer very quickly, especially since misaligned accesses of 16-bit or 32-bit words caused crashes. Compilers now go to a great deal of trouble to try to do additional safety and consistency checks.
Don't get my wrong, I grew up using printf, and it is massively useful. But it was designed when computers were much smaller and simpler, and design tradeoffs were made back then that probably wouldn't be chosen today. So printf, along with a whole family of related functions, has been a seething mess of a security and safety hole longer than most programmers have been alive.
[0] https://gist.github.com/shakna-israel/4fd31ee469274aa49f8f97...
printf("%p\n", v);
the Generic call must look like this displayln((const void *)v);
is it really better?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 vulnerability.
I'm pathologically lazy.
Also as others point out typically the literal string will be in a ro segment so tampering with it won't be easy unless the code runs in a rather exotic environment.
This is not the case for RO strings, is it?
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
In order to modify that string, even in RW pages, the attacker already has to have access, at which point the point is moot. It's like saying "if you can change memory, then you can change memory"....
Nothing.
Neither is more secure, all modern compilers put both “%s” and “Hello, world!” in rodata sections.
Your understanding of practical format string attacks is misguided.