3 ms·
>It also happens to nearly always be the wrong way to read a file It's the wrong way in many cases, but it's the right way in a very large number of cases, if
by mistercow 13y ago
>It also happens to nearly always be the wrong way to read a file
It's the wrong way in many cases, but it's the right way in a very large number of cases, if not in the majority of cases.
Often you need to read in a relatively small file, then do something trivial with it, or toss it through a couple of library-provided string processing functions. Or maybe the files are a bit bigger, and you're writing a script to automate some grunt work. You expect to run this script once. Or, more generally, maybe you're writing some code that gets called once a month and takes less than a second to complete.
In any of those situations, it would be silly to do anything other than slurping. String manipulation is easy to reason about. Stream processing is not.
Also note that most OSes cache files in memory, so if you are reading the same file often, the slowdown from reading the data into memory is drastically reduced.
- rsobers 13y agoYup. There are literally millions of instances of code that slurps and Just Works and the users don't care and the investors don't care and the servers don't care and the programmer just moved on with life and knocked out the next feature. And nobody cared and it never mattered that it was "wrong."
- pyre 13y agoIt could be used as a known vector to crash the program. Turn the file that it reads into something that will fill up RAM unless Python has some size checks before slurping.
- mistercow 13y agoAn attacker with that access can also DoS a program that doesn't slurp using the same technique. Also, on a 64-bit system, a DoS is all you can realistically get from this vector anyway. I don't know what happens if you actually fill Python's available address space (it seems pretty difficult), but I'd be shocked if it were a crash, and not an exception.