From dcc1234511cb51901184e31b6d914d634176a8ef Mon Sep 17 00:00:00 2001 From: koalaman Date: Sun, 25 Jan 2015 12:17:29 -0800 Subject: [PATCH] Created SC2115 (markdown) --- SC2115.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 SC2115.md diff --git a/SC2115.md b/SC2115.md new file mode 100644 index 0000000..6c11d44 --- /dev/null +++ b/SC2115.md @@ -0,0 +1,21 @@ +## Use "${var:?}" to ensure this never expands to /* . + +### Problematic code: + + rm -rf "$STEAMROOT/"* + +### Correct code: + + rm -rf "${STEAMROOT:?}/"* + +### Rationale: + +If `STEAMROOT` is empty, this will [end up deleting everything](https://github.com/ValveSoftware/steam-for-linux/issues/3671) in the system's root directory. + +Using `:?` will cause the command to fail if the variable is null or unset. Similarly, you can use `:-` to set a default value if applicable. + +In the case command substitution, assign to a variable first and then use `:?`. This is relevant even if the command seems simple and obviously correct, since forks and execs can fail due to external system limits and conditions, resulting in a blank substitution. + +### Exceptions: + +None. \ No newline at end of file