-
-
Notifications
You must be signed in to change notification settings - Fork 257
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
programs/cp: Fix pointer cast warning #356
programs/cp: Fix pointer cast warning #356
Conversation
execve() expects the args to be in writable memory, so copy them using strdup(3).
(Note: drive-by comment)
It doesn't, actually: https://pubs.opengroup.org/onlinepubs/9799919799/ (scroll down to the "Rationale" section). I would suggest just marking the variables as |
Thanks, that's helpful. However, the macOS headers declare
This matches the third row in the table of the rationale section of https://pubs.opengroup.org/onlinepubs/9799919799/functions/exec.html. We also don't actually know whether Apple's implementation of So we either need to live with the compiler warning, silence the compiler warning, or add a cast. The allocation fixes the warning regardless of what Apple's implementation does, and the cost of two small allocations compared to the following |
Couldn't you also allocate the static strings on the stack with |
char *cp_path = strdup("/bin/cp"); | ||
char *clone_arg = strdup("-c"); | ||
char **new_argv = malloc(sizeof(char *) * (argc+2)); | ||
if (cp_path && clone_arg && new_argv) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Putting all into one condition will not work well with the perror()
below. The errno
could have been overwritten by the next library function.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
That's the source, for a |
We can use the stack instead. Thanks @raimue for spotting that. See #356 (comment).
Good catch, in 1b43b56. |
execve()
expects the args to be in writable memory, so copy them usingstrdup(3)
.