Fix completion log saving in current directory when program name contains certain characters #45
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Circumstances triggering the bug
While generating bash completion with dashdash through cmdln, I noticed that debugging logs for the completion were being written to my current directory. My program is called
validana-cli
, and the minus sign in that name causes my issue. This will happen for any program with a minus sign in the name; which is not uncommon for command line programs.Analysis of the bug
Specifically, in dashdash debugging of completion is enabled by uncommenting a variable. This name of the variable contains the name of the program, I presume so that enabling completion debugging for a specific program won't cause many other programs using dashdash to start spewing out logfiles as well.
The problem is that if the program name contains a character that is not a valid character in a shell variable name (as in my case, the minus sign), the check the script uses will always succeed, and the filename it will use will be a combination of the last part of the program name and and a part of the broken variable name. Since this filename will also never contains any slashes, it will write this logfile to the current directory, spewing log files wherever the user goes and tries to use completion.
Proposed resolution
This pull request resolves this by using a function instead of a variable to store the filename for the debug log. Since functions names in bash are allowed a larger set of characters, this resolves the problem. It does so for all possible characters dashdash supports (if the program name would contain a character which is also not valid in a function name, most of the completion script would break anyway). It also keeps it just as simple as before to start debugging by just removing a single character.
Impact of changes
Since this change is only touching the
bash_completion.in
template file this change should not have any impact outside of the completion functionality. Since the changed code is specifically for debugging purposes and should never be triggered during normal use, impact on consumers of this package should be negligible.Additional comments
@trentm If you decide to merge and publish this fix, could you also update your cmdln package to depend on the new version? That way users of that package (such as myself :) ) may also benefit from this fix.