News for hackers. Stuff that matters.
Re:Formatting Text in C++: The Old and the New Ways by guitarbill on Saturday September 16, 2023 @03:59PM (#37538132)
It seems so trite, especially in 2023, but please don't use sprintf. It isn't safe in general. (Even snprintf is tricky.)
Re:Formatting Text in C++: The Old and the New Ways by 38 on Saturday September 16, 2023 @04:32PM (#37538425)
saying "dont use" something isn't really actionable, unless you give a safe alternative.
Re:Formatting Text in C++: The Old and the New Ways by thiht on Saturday September 16, 2023 @05:30PM (#37538905)
And explain why it’s not safe too
Re:Formatting Text in C++: The Old and the New Ways by jjnoakes on Saturday September 16, 2023 @05:54PM (#37539120)
The safe alternatives are in the article.
Re:Formatting Text in C++: The Old and the New Ways by cozzyd on Saturday September 16, 2023 @09:43PM (#37540655)
asprintf is often a better choice if you're heap allocating anyway. For a static or stack buffer, obviously snprintf, unless you know the maximum possible length won't exceed (which you often do....).
Re:Formatting Text in C++: The Old and the New Ways by consoomer on Saturday September 16, 2023 @04:18PM (#37538315)
YOLO
Re:Formatting Text in C++: The Old and the New Ways by mkoubaa on Saturday September 16, 2023 @06:10PM (#37539264)
There are a lot of use cases where "isn't safe" is absolutely irrelevant
Re:Formatting Text in C++: The Old and the New Ways by i-use-nixos-btw on Saturday September 16, 2023 @07:58PM (#37540070)
OTOH, in many of the cases where “isn’t safe” IS relevant, the developer believes it isn’t.
Re:Formatting Text in C++: The Old and the New Ways by kevin_thibedeau on Sunday September 17, 2023 @09:37AM (#37544946)
Buffer overflows are never irrelevant. You might get away with it until the day it blows up or someone manages to exploit it. Or you could code it correctly the first time.
Re:Formatting Text in C++: The Old and the New Ways by fsloth on Sunday September 17, 2023 @06:12AM (#37543410)
"It isn't safe in general"

Disagree. Use whatever is the most simple and boring option for the problem's solution.

Also, the standard library has so much stuff that will give you pain in runtime, that avoiding sprintf really is not relevant.

I don't know what "Safe" means in general in the scope of C++. If there is a memory corruption, my program will crash. Then I will compile it with C++ debug runtime, which will pinpoint the exact location that caused the memory leak. Then I fix the leak.

Not using sprintf will not result in code that would not have memory leaks. C++ in total is unsafe. You need to write code and have production system that foremost takes this into account. You can't make C++ safe by following dogma of not using functions with possible side-effects. There is a very hight chance your fall-back algorithms themselves will leak memroy anyway.

The only way to write 'as non-leaky' C++ as possible is to make the code as simple, and easy to reason about as possible, and to have tooling to assist in program validation. This requirement is much more important than avoiding some parts of standard library.

Use static checkers, use Microsoft's debug C runtime, use address sanitizers, etc.

If you know some parts of you standard library are broken then ofc avoid them. But what can be considered "broken" really depends what one is trying to achiece, and which platforms one is targeting.