Skip to content
Browse files

wrapper: account for -l when checking argc

If "-l" is the first argument, record the fact in a variable and
adjust argv and argc as if had not been present. This lets the
existing "minimum arguments" check to handle the case where "-l" is
the only argument.

Previously, if "-l" was the only argument, we would end up with
a segmentation fault from accidentally doing strlen(argv[argc])
(i.e. strlen(NULL)) in the dash-prefixing code.

Reported as item #2 here:

    #13
  • Loading branch information...
1 parent d5f6ae9 commit 66ffbee221cdbc3e461b8976a8c061e223c73ddc @ChrisJohnsen committed Jul 29, 2012
Showing with 11 additions and 2 deletions.
  1. +11 −2 reattach-to-user-namespace.c
View
13 reattach-to-user-namespace.c
@@ -51,6 +51,16 @@ static const char usage_msg[] = "\n"
" rewrite the program's argv[0] so that it starts with a '-'.\n";
int main(int argc, char *argv[]) {
+ unsigned int login = 0;
+
+ if (argc > 1) {
+ if (!strcmp(argv[1], "-l")) {
+ login = 1;
+ argv[1] = argv[0];
+ argv++;
+ argc--;
+ }
+ }
if (argc < 2)
die(1, "usage: %s [-l] <program> [args...]\n%s", argv[0], usage_msg);
@@ -140,8 +150,7 @@ int main(int argc, char *argv[]) {
int arg = 1;
char **newargs = NULL;
const char *file = argv[arg];
- if (!strcmp(file, "-l")) {
- file = argv[++arg];
+ if (login) {
/*
* For their argv[0], take the bit of file after the
* last slash (the whole thing if there is no slash

0 comments on commit 66ffbee

Please sign in to comment.
Something went wrong with that request. Please try again.