Skip to content

bug: prevent lightcontrol from crashing without values#90

Open
ALX99 wants to merge 4 commits into
masterfrom
bug/lightcontrol-missing-value
Open

bug: prevent lightcontrol from crashing without values#90
ALX99 wants to merge 4 commits into
masterfrom
bug/lightcontrol-missing-value

Conversation

@ALX99

@ALX99 ALX99 commented Jul 9, 2026

Copy link
Copy Markdown
Owner

Bug

Running .local/bin/lightcontrol -d or .local/bin/lightcontrol -s without the required value crashes under Bash strict mode instead of reporting a usage error.

Severity and impact

This is a reliability bug in a user-facing brightness helper. Omitting a required value is a realistic command-line mistake. The script should fail before touching backlight state and tell the user how to call it; instead it exposes a shell unbound variable crash.

Evidence

Relevant files and lines on master:

  • .local/bin/lightcontrol: line 4 enables Bash strict mode with set -eou pipefail.
  • .local/bin/lightcontrol: lines 44-47 handle -d, run shift, then call delta_change "$1" without verifying that a value remains.
  • .local/bin/lightcontrol: lines 49-52 handle -s, run shift, then call set_brightness "$1" without verifying that a value remains.

Observed incorrect behavior from the baseline pattern:

/tmp/lightcontrol: line 3: $1: unbound variable
exit:1

That is deterministic for both missing-value option paths: after shift, $1 is unset, and set -u aborts before the script can produce an intentional error.

Reproduce

Setup:

git checkout master

Triggering condition: pass -d or -s without its required value.

Exact commands:

.local/bin/lightcontrol -d
.local/bin/lightcontrol -s

Actual result on master:

.local/bin/lightcontrol: line 46: $1: unbound variable
.local/bin/lightcontrol: line 51: $1: unbound variable

Expected result:

usage lightcontrol [OPTIONS]

where:
    -h, --help   show this help text
    -c, --check  show the current brightness
    -d [DELTA]   change the current brightness by DELTA
    -s [VALUE]   set the brightness to VALUE

The commands should exit non-zero without reaching brightness reads or writes.

Root cause

The option handlers assume shift leaves an operand in $1. Under set -u, dereferencing $1 after a missing operand is a runtime error.

Fix

The fix adds a small require_value guard used by both -d and -s after shift. Missing operands now print the existing usage text and exit before any brightness calculation or write is attempted.

Validation

Commands run and results:

bash /tmp/dotlight/tests/lightcontrol-missing-value.test.sh
# passed

bash -n /tmp/dotlight/.local/bin/lightcontrol /tmp/dotlight/tests/lightcontrol-missing-value.test.sh
# passed

shellcheck /tmp/dotlight/.local/bin/lightcontrol /tmp/dotlight/tests/lightcontrol-missing-value.test.sh
# not run: shellcheck is not installed in this runtime

Regression test added:

  • tests/lightcontrol-missing-value.test.sh verifies both -d and -s without values fail, do not print unbound variable, and do print the usage text.

Additional repository-scope check:

GitHub compare API: branch is 2 commits ahead and 0 behind master; only .local/bin/lightcontrol and tests/lightcontrol-missing-value.test.sh changed.

Scope

Intentionally not changed:

  • brightness file paths,
  • current brightness reporting,
  • actual -d VALUE and -s VALUE behavior,
  • sudo usage,
  • option names or documented help text,
  • any unrelated scripts or configs.

@ALX99 ALX99 force-pushed the master branch 4 times, most recently from 5cf571d to dd748db Compare July 10, 2026 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant