Skip to content
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

Quoted parameter short flag handling with quotes is wonky #1174

Open
SpencerMalone opened this issue Oct 1, 2024 · 7 comments
Open

Quoted parameter short flag handling with quotes is wonky #1174

SpencerMalone opened this issue Oct 1, 2024 · 7 comments

Comments

@SpencerMalone
Copy link

Describe the bug
Maybe I'm crazy and this is how it works for other CLIs and I've just never noticed, but it feels very surprising:

  • memcached -l localhost:11211 - This works
  • memcached --listen=localhost:11211 - This works
  • memcached "-l localhost:11211" - This fails
  • memcached "-llocalhost:11211" - This works

That third one is especially impactful and surprising in kubernetes environments, as you may do:

command:
  - memcached
  - l localhost:11211

which will be translated into...
memcached "-l localhost:11211" and cause an error.

This behavior is not exclusive to the l flag, but is present on many other short flags (notably, -o is also effected in an odd way, simply saying Illegal suboption "(null)")

To Reproduce
memcached "-l localhost:11211"

System Information

  • OS/Distro: Alpine
  • Version of OS/distro: 3.20.3
  • Version of memcached: memcached 1.6.31
  • Hardware detail: cannot provide
@dormando
Copy link
Member

dormando commented Oct 1, 2024

I'm just using standard C getopt-long stuff. Perhaps most of the stuff you work with are using meatier/extended CLI parsing code?

I'm not sure there's anything I could even do to make that work without writing the whole parser myself. I can check.

@dormando
Copy link
Member

dormando commented Oct 1, 2024

I've not heard of anyone else using kubies having this problem btw. is this cli quoting an unusual form of providing the arguments or something?

@SpencerMalone
Copy link
Author

I don't think so? I'm just doing like...

          command:
            - memcached
            - -o proxy_config=routelib.lua,proxy_arg=proxy.lua # This one errors
            - -u appuser
            - -c 10000 # max connections
            - -l *:11200 # This one errors
            - -l tag_e2e_:*:11201 # This one errors
            - --memory-limit=90

If this is all just standard stuff and it's what we get without a larger investment, I'm fine to close, it just really caught me off guard.

@SpencerMalone
Copy link
Author

Yeah, looks like this is just a getopt_long side effect for optional string args (so like "-c 10000" works as expected)
https://cfengine.com/blog/2021/optional-arguments-with-getopt-long/ talks about a solution for this a bit, but it seems hacky. https://superuser.com/a/1652973 talks about this in mysql world with the somewhat infamous -p flag. I'm fine with this being around, but maybe it's worth trying to change some of the docs to mention this, really surprised me earlier today.

@dormando
Copy link
Member

dormando commented Oct 2, 2024

it seems super bizarre to me to quote commandline arguments like that. I'll still take a look before considering this closed though.

@dormando dormando reopened this Oct 2, 2024
dormando added a commit that referenced this issue Oct 3, 2024
From #1174: "-l localhost:11211" will fail. internally we end up with a
suboption string of " localhost:11211" instead of "localhost:11211". The
same happens with `-o` suboptions.

A simple fix to to ensure we skip leading whitespace in optarg.
@dormando
Copy link
Member

dormando commented Oct 3, 2024

with "-l localhost:11211":

(gdb) p argv[0]
$8 = 0x7fffffffe1bc "/home/dormando/p/code/memcached/memcached-debug"
(gdb) p argv[1]
$9 = 0x7fffffffe1ec "-l localhost:11211"
(gdb) p argv[2]
$10 = 0x0

with -l localhost:11211

(gdb) p argv[0]
$11 = 0x7fffffffe1bc "/home/dormando/p/code/memcached/memcached-debug"
(gdb) p argv[1]
$12 = 0x7fffffffe1ec "-l"
(gdb) p argv[2]
$13 = 0x7fffffffe1ef "localhost:11211"

So when you quote it getopt is dealing with a longer substring and actually parses it slightly differently.

When I get the suboption in the quoted case, it looks like localhost:11211 which then fails to parse later on because we don't skip leading whitespace as we aren't expecting it.

Just pushed a fix for this to next since it seemed simple. Millions of installs and over decades and you're the first to point it out :) Thanks!

@dormando
Copy link
Member

dormando commented Oct 3, 2024

Pushed to next means you can try the next branch now or wait for the next tarball release (roughly monthly)

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

No branches or pull requests

2 participants