jaraco / cmdix

Fork of https://launchpad.net/pycoreutils
MIT License
27 stars 1 forks source link

argv.pop causes crash in Python's smtpd #3

Closed rhbvkleef closed 3 years ago

rhbvkleef commented 3 years ago

I have identified this line to cause problems when running the shell as an entrypoint of a docker container, without passing parameters.

This is the stacktrace that is created:

Traceback (most recent call last):
  File "/bin/sh", line 8, in <module>
    sys.exit(run())
  File "/lib/python3.10/site-packages/cmdix/__init__.py", line 102, in run
    epilog="Available Commands: " + ", ".join(listcommands()),
  File "/lib/python3.10/site-packages/cmdix/__init__.py", line 39, in _is_available
    cmd = _get_command(name)
  File "/lib/python3.10/site-packages/cmdix/__init__.py", line 51, in _get_command
    return __import__(importstring, fromlist=1).parseargs
  File "/lib/python3.10/site-packages/cmdix/command/smtpd.py", line 2, in <module>
    import smtpd as _smtpd
  File "/lib/python3.10/smtpd.py", line 97, in <module>
    program = sys.argv[0]
IndexError: list index out of range

The better option is probably to replace this line with the following two:

commandname = os.path.basename(argv[0])
argv = argv[1:]

That way, sys.argv is not mutated.

P.S.: Thanks for writing this. Now I can finally properly embark on running an almost pure python userland 😄 P.P.S.: Yes, I am running Python straight from mainline, but I've tested with older versions, and the problem persists.

jaraco commented 3 years ago

How can I replicate the failure? I can apply the change, and tests still pass, but how do I know the test is having the intended effect?

jaraco commented 3 years ago

Nevermind. I found simply running uname was sufficient to trigger the error... and applying the recommended patch fixed the issue.