I'm Eric Gitangu. I work on backend and platform systems for a mobility/fintech platform. This one is a one-line shell bug that stayed hidden until a safety check got stricter.
The symptom
Our deploy scripts read a config file where each box can override the defaults. One setting is a list of files that live outside the release and get linked into every release: a few per-box files. If the config doesn't mention the list, the script uses a built-in set as defaults. If the config sets it to an empty list, the meaning is "this box has no shared files".
Then I tightened a check. On the branch, a release refuses to activate when a required shared file is missing. Before that, a missing one only left a dangling link.
Right after the change, every test fixture that set the list to empty got refused with a fail-closed message, as if the defaults were still required.
Nothing was wrong with the fixtures. The script had never honoured the empty list.
The cause
The usual way to ask "is this variable set?" in bash is [[ -z "${X+x}" ]]. For a plain string it works. For an array, ${X+x} looks at element zero. An empty array has no element zero, so it reads as unset, and the defaults overwrote the explicit opt-out.
Until then it was harmless. Defaults for files that aren't there only produced dangling links. Once "missing" meant "refuse", the bug started to matter. The new check was right. The default was wrong.
The three states
A list setting like this has three states, and each needs its own answer:
- Never declared: use the defaults.
- Declared and empty: the box said "none". Keep it empty.
- Declared with items: use them.
${X+x} only separates the third state from the first two. It lumps "empty" in with "never declared", which is the one distinction an opt-out depends on.
The decision
Three ways out:
- Treat empty as unset. Simplest, but then there is no way to say "none". A box would need a dummy entry to opt out.
- Add a second switch, something like "use defaults: no". It works, and it is one more knob to keep in step with the list.
- Ask the shell whether the name was declared at all with
declare -p. A declared empty array succeeds. A name nobody declared fails.
I took the third. Another list in the same file already used declare -p for the same reason, so this was the file's own contract: an explicit empty list means none. The fix made the two settings agree.
The fix
# before: an explicit empty list reads as unset
if [[ -z "${ITEMS+x}" ]]; then ITEMS=(a b c); fi
# after: ask whether it was declared at all
if ! declare -p ITEMS >/dev/null 2>&1; then ITEMS=(a b c); fiA comment next to the code says why it isn't the shorter form.
The test
I added a case to the existing test for the missing-file refusal. The config sets the list to empty and the shared directory is empty. The release has to succeed, the output must not contain the fail-closed message, and no link may be made for any of the defaults.
What this costs
declare -p is a bash feature, so the script is tied to bash. These scripts already were. It also counts any declared name as declared, including an empty string, so the code still has to cope with the shape it receives.
The other cost is structural: the default and the check live in different places, and the check changed meaning without the default hearing about it.
If you have the same line
Search your scripts for ${NAME+x} applied to arrays. For each hit, ask what an explicit empty value is supposed to mean. If the answer is "none", the check is wrong. If you can't answer, the setting has two readers who disagree about it, and one of them is the default.
The gist shows it: four test cases, and one of them shows the broken check bringing the defaults back.
Status
Fixed and tested on a release candidate branch. Not live yet.
The gist
A small runnable sketch with generic names: the broken check, the fixed one, and four cases, one of which pins the broken behaviour so you can watch it happen. bash test.sh and you're done. It needs bash 4.4 or newer.
https://gist.github.com/ericgitangu/21d8038284888e54dc5eea1fd51f6500
How do you tell an empty setting from a missing one in your config loaders?
Also on dev.to.