+ 1

Why "puts"is safer than "printf"?

Why is it safer to use "puts" than using "printf"??

29th Nov 2020, 10:22 AM
Ruby Parker
Ruby Parker - avatar
3 Answers
+ 2
Printf is not dangerous if used properly. If you do: printf("Ciao"); There are no risks. If you do: printf("Ciao %s"); You get an undefined behaviour, because %s expect a pointer. If you always put "%s" before the string you want to output, like that: printf("%s", "Ciao %s"); You are safe, because the second %s is considered as just characters to be printed. The most dangerous thing you can do is printing a string read from user, like that: char ciao[50]; scanf("%s", ciao); printf(ciao); Because you don't know what ciao is. It can be a %s or other dangerous things. But if you print the string read from the user like that: printf("%s", ciao); You are safe. Using puts there's no way to make mistakes. You can safely do: puts("%s"); Check this for more info: https://owasp.org/www-community/attacks/Format_string_attack
29th Nov 2020, 11:37 AM
Davide
Davide - avatar
+ 6
Ruby Parker Hello,Visit this link,this link will helps you and here you will get better answers https://www.google.com/amp/s/www.geeksforgeeks.org/puts-vs-printf-for-printing-a-string/amp/
29th Nov 2020, 10:25 AM
ㅤㅤㅤ
+ 1
I think you are asking this question for ruby language . Yes , printf is used in c language .
29th Nov 2020, 10:24 AM
Sâùtôsh
Sâùtôsh - avatar