Skip to content

fix(crontab): avoid panic on env line with empty value#233

Open
devteamaegis wants to merge 1 commit into
aptible:mainfrom
devteamaegis:fix/empty-env-value-panic
Open

fix(crontab): avoid panic on env line with empty value#233
devteamaegis wants to merge 1 commit into
aptible:mainfrom
devteamaegis:fix/empty-env-value-panic

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

ParseCrontab panics on a crontab that contains an environment line with an empty value, e.g. FOO= — a valid Vixie-cron construct (clearing/setting a variable to empty). Instead of parsing it, supercronic crashes at startup:

panic: runtime error: index out of range [0] with length 0
  github.com/aptible/supercronic/crontab.ParseCrontab
      crontab/crontab.go:88

Why it happens

The env regex ^([^\s=]+)\s*=\s*(.*)$ lets the value group match the empty string, so envVal == "". The quote-stripping block then indexes envVal[0] unconditionally. The existing FOO='' test doesn't hit this because there envVal is "''" (length 2) at that point; only a bare FOO= produces a zero-length value.

Fix

Guard the quote check with len(envVal) > 0. Empty values parse to an empty string, consistent with the existing FOO='' behaviour.

Test

Added a FOO= case to TestParseCrontab asserting it parses to {"FOO": ""}; panics before, passes after.

A crontab env line with an empty value (e.g. "FOO=") matched the env
regex with an empty value group, and the quote-stripping check indexed
envVal[0] unconditionally, panicking with index out of range on the
empty string. Guard the quote check on len(envVal) > 0 so an empty
assignment parses to an empty value, matching Vixie cron behavior.
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