Skip to content

Add if statement to avoid UnboundLocalError #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2022
Merged

Conversation

ppaslan
Copy link
Contributor

@ppaslan ppaslan commented May 26, 2022

When there are no challenges installed in ctfd, sync will throw an UnboundLocalError because the variable 'c' at line 209 will be unreferenced.
This adds a simple sanity check to avoid running sync at all if there are no challenges installed.

ppaslan added 2 commits May 26, 2022 14:01
When there are no challenges installed in ctfd, sync will throw an UnboundLocalError because the variable 'c' at line 209 will be unreferenced.
This adds a simple sanity check to avoid running sync at all if there are no challenges installed.
@ColdHeat
Copy link
Member

Can you provide a copy of the exception you get? If there are no challenges then wouldn't it be an empty list and then c would just not go into the for loop?

@ppaslan
Copy link
Contributor Author

ppaslan commented May 27, 2022

Can you provide a copy of the exception you get? If there are no challenges then wouldn't it be an empty list and then c would just not go into the for loop?

Of course, and i checked, it is indeed and empty list, however i think that the variable c gets unreferenced because of this.

ctf@localhost:~/challenges$ ctf challenge sync gift_bag/
Found gift_bag/challenge.yml
Loaded Gift Bag
Traceback (most recent call last):
  File "/usr/local/bin/ctf", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.8/dist-packages/ctfcli/__main__.py", line 107, in main
    fire.Fire(CTFCLI)
  File "/usr/local/lib/python3.8/dist-packages/fire/core.py", line 138, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File "/usr/local/lib/python3.8/dist-packages/fire/core.py", line 466, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File "/usr/local/lib/python3.8/dist-packages/fire/core.py", line 675, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/ctfcli/cli/challenges.py", line 199, in sync
    f'Couldn\'t find existing challenge {c["name"]}. Perhaps you meant install instead of sync?',
UnboundLocalError: local variable 'c' referenced before assignment

@ColdHeat
Copy link
Member

Ah okay I see now. The issue is that instead of c["name"] it should actually be challenge["name"]!

Do you think you can test that out and update your PR if it does fix it?

@ppaslan
Copy link
Contributor Author

ppaslan commented May 27, 2022

Ah okay I see now. The issue is that instead of c["name"] it should actually be challenge["name"]!

Do you think you can test that out and update your PR if it does fix it?

There we go :)

Remove extra whitespace
@ColdHeat ColdHeat merged commit 192b1de into CTFd:master May 27, 2022
@ColdHeat
Copy link
Member

Thanks!

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.

2 participants