2 ms·
Yes, mktemp can fail. For example, "TMPDIR=/dev/null mktemp -d" will reliably fail. You shouldn't be validating it starts with "/tmp" though, because on quite
by TheDong 7y ago
Yes, mktemp can fail.
For example, "TMPDIR=/dev/null mktemp -d" will reliably fail.
You shouldn't be validating it starts with "/tmp" though, because on quite a few systems, people set TMPDIR=/var/tmp.
You absolutely should check if mktemp exited nonzero, but if it exited 0, the directory should be safe to use. If you want to be really paranoid, you can check if the output is an empty string too after checking the exit code.
I think you're shying a little too far away from bash's globbing. Yeah, bash has footguns, but using clever workarounds can also be a recipe for creating confusing and error prone code in its own way.
- emj 7y agoPlease tell me it's enough to check the exit code! That's all I ever do and I have hundreds of mktemp scripts out there. In the end I think shelling is mostly about controlling your inputs.
- clarry 7y agoNot just inputs, unfortunately. TOCTOUs (and races in general), for instance, are very common problem in shell scripts.
- arpa 7y agoRaces and TOCTOUs are generally common problems in most concurrent systems that are not considering atomicity by design (e.g. rdbms). It's just that shell can very easily become concurrent...
- TheDong 7y agoYes, it's enough to check the exit code. The parent poster's being overly paranoid. The only thing that checking the exit code won't catch is bugs in mktemp/bash, or bad memory / solar flare bitflips.