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

Error due to accessing an entry in argv entry beyond the end of the list #1

Open
antiprism opened this issue May 15, 2016 · 2 comments

Comments

@antiprism
Copy link

Some toolkits, like Glut, might process the argument list before the program calls ultragetopt(), rewriting argc and the entries of argv. This can leave a valid option identifier in argv[argc], which ultragetopt accesses and which leads to an error.

The following demonstration program

ultra_test2.zip

reproduces the situation by reducing argc by two before calling ultragetopt, and then reports on the argument list. It accepts options -a and -b, which take parameters.

Test 1: completes correctly
./ultra_test2 -a 1 bbb ccc

Before processing agument list (argc has been reduced by two):
argc = 3
argv[0] = './ultra_test2'
argv[1] = '-a'
argv[2] = '1'

Processing agument list:
option -a read, argument is '1'

After processing agument list:
ultraoptind = 3

Test 2: shows the issue occurring when the argument beyond the truncation point is a valid option identifier.
./ultra_test2 -a 1 -b ccc

Before processing agument list (argc has been reduced by two):
argc = 3
argv[0] = './ultra_test2'
argv[1] = '-a'
argv[2] = '1'

Processing agument list:
option -a read, argument is '1'
option -b missing required argument

After processing agument list:
ultraoptind = 4
ultraoptind is greater than argc

The fix might be to change ultragetopt.c:568

/* Sanity check (These are specified verbatim in SUS) */
if (ultraoptind > argc

to

/* Sanity check (These are specified verbatim in SUS) */
if (ultraoptind >= argc

Adrian.

@kevinoid
Copy link
Owner

Definitely a bug. I think your fix is correct, but it has been a while since I last worked on this code.

I'm working on adding proper unit and behavior tests as part of the fix, to prevent any accidental regressions. It may take me a little while to complete. If I don't have it done in a few days, feel free to ping this issue.

Thanks again for taking the time to report this issue!

@antiprism
Copy link
Author

Thanks for reviewing this so quickly. I'll look out for the update.

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