3 ms·
You would think converting floats to and from strings was a solved problem by now... I'm surprised noone ever looked at that code --- even when writing it ---
by userbinator 6y ago
You would think converting floats to and from strings was a solved problem by now...
I'm surprised noone ever looked at that code --- even when writing it --- and thought "how long can a %f get?" I've been writing C for a long time and that's just something which comes naturally, being ingrained into memory since the beginning. If I see a fixed-size buffer I will always question whether it's big enough (and also if it's perhaps even too big.)
The "psychology" around buffer overflows has always seemed strange to me; a real-world analogy is someone who has no idea how big his car is, finds a parking spot that "looks big enough", and just rams it in without a second thought, sometimes crashing into the surroundings. Not many people would do that in the real world. Yet countless programmers seemingly can't get something simple like this right?
Edit: downvoters, care to state your case?
- Someone 6y ago“You would think converting floats to and from strings was a solved problem by now” It is, but it is an extremely difficult problem. I think “How to print floating-point numbers accurately” (https://dl.acm.org/doi/10.1145/93548.93559 https://dl.acm.org/doi/10.1145/93548.93559) was the first correct implementation. That is from 1990 and, according to its authors “was almost 20 years in the making” (http://kurtstephens.com/files/p372-steele.pdf http://kurtstephens.com/files/p372-steele.pdf) (Faster versions have since been published)
- userbinator 6y ago...or at least knowing how long the result would be?
- Someone 6y agoThat, of course, is easier, but you have to realize that %f never uses scientific notation for its output (something I must have known at some time, but if you had asked, I would have had to google it), and that DBL_MAX has a lot of digits (it is about 1.8 × 10^308) I think it is unfortunately that %f, the easiest to remember type field for floating point number, is used for this behavior. Most of the time, you’ll want to use %g, which uses scientific notation if it is shorter. I guess linters should warn about bare %f without length fields.
- yannoninator 6y agoDidn't downvote, but I agree, nobody wants to work on the low level stacks anymore, not as sexy as CRUD apps. I would have expected this issue to be a solved problem too, and now we get extremely insecure Python 3 codebases as a result of this vuln. So much for moving from Python 2 to 3, will now wait for Python 4.