[PATCH 1/3] posix: Remove dynamic memory allocation from execl{e,p}

Paul Eggert eggert@cs.ucla.edu
Fri Feb 26 19:11:00 GMT 2016


On 02/26/2016 05:56 AM, Adhemerval Zanella wrote:
> +  for (i = 1; i < argc; i++)
> +     argv[i] = va_arg (ap, char *);
> +  argv[i] = NULL;

Change "i < argc" to "i <= argc" and remove the "argv[i] = NULL;", as 
that's a bit simpler and faster.

> +  int i;
> +  char *argv[argc + 1];
> +  char **envp;
> +  va_start (ap, arg);
> +  argv[0] = (char *) arg;
> +  for (i = 1; i <= argc; i++)

This sort of thing has undefined behavior on x86-64 if argc == INT_MAX. 
You can fix this by changing the type of argc and of i from int to 
ptrdiff_t.

> +      if (argc == INT_MAX)
>   	{
> +	  errno = E2BIG;
> +	  return -1;
>   	}

Doesn't that have undefined behavior? My impression from C11 is that 
since the function has called va_start it must call va_end before returning.

> +      continue;
>       }

That 'continue;' is redundant and should be removed.



More information about the Libc-alpha mailing list