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
Don't modify argv[] in user tools #7760
Conversation
cmd/zfs/zfs_main.c
Outdated
| char *fromname = NULL; | ||
| char *toname = NULL; | ||
| char *dataset = NULL; |
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.
In order to simply the error handling I think it would be reasonable to put the dataset and fromname on the stack. Since this code always runs in user space we don't have to worry about the kernel's tiny stacks. You can could do something like.
char fromdup[ZFS_MAX_DATASET_NAME_LEN];
...
(void) strlcpy(fromdup, optarg, sizeof (fromdup));
fromname = &fromdup;
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.
What do you think about using alloca(strlen(fromname)) instead for a dynamic allocation still on the stack?
Edit: on second thought, never mind. Your method is used in other places in this function. Best to be consistent.
cmd/zfs/zfs_main.c
Outdated
| @@ -4145,16 +4149,17 @@ zfs_do_send(int argc, char **argv) | |||
| fromname = frombuf; | |||
| } | |||
| err = zfs_send_one(zhp, fromname, STDOUT_FILENO, flags); | |||
| free(fromname); | |||
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.
This could free frombuf which is on the stack. This might be what caused the ZTS failure.
2a87f6a
to
0322a85
Compare
|
I ended up using something of a mixture of Also replaced various instances of |
This same issue seems to be affecting other |
Codecov Report
@@ Coverage Diff @@
## master #7760 +/- ##
==========================================
- Coverage 78.32% 78.15% -0.17%
==========================================
Files 373 373
Lines 112791 112805 +14
==========================================
- Hits 88341 88168 -173
- Misses 24450 24637 +187
Continue to review full report at Codecov.
|
|
Interesting. I didn't check that because Maybe |
That's not a bad idea. I suspect there's more existing code which makes this assumption and it'd be nice to handle this all in one place. We'd want to update |
0322a85
to
4338c5c
Compare
|
Updated to just duplicate This changes the intent of the commit as a whole while still meeting the same goals. |
921f4da
to
211f842
Compare
|
Last round of buildbot failures seem related to a github outage last night. Pushing a noop rebase to make them try again. |
cmd/zfs/zfs_main.c
Outdated
| */ | ||
| newargv = alloca((argc + 1) * sizeof (newargv[0])); | ||
| for (i = 0; i < argc; i++) { | ||
| newargv[i] = alloca(strlen(argv[i] + 1)); |
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.
Hi there! Thanks for working on this change.
Maybe we could change this:
for (i = 0; i < argc; i++) {
newargv[i] = alloca(strlen(argv[i] + 1));
strcpy(newargv[i], argv[i]);
}
to this:
for (i = 0; i < argc; i++) {
newargv[i] = strdupa(argv[i]);
}
(in both cases where it is used)
To be honest though, I'm not sure how to feel about alloca() being
used as it is both platform and compiler dependent. Do we actually
get any wins from using it?
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.
Stack allocations are extremely fast don't have to be free()'d at the end of a program since they are effectively freed on return from main(). The alternative with regular strdup() is to call free() afterwards just for the sake of cleanliness. Not that big a deal but we have an alternative that, at least in the context of Linux, works equally as well.
Though platform and compiler dependent, it is well supported on gcc and the Linux man page implies it will be just fine on BSD as well. strdupa is noted as strictly a gcc thing whereas it seems alloca has a somewhat wider range of support. Heck I see an equivalent on MSDN. If alloca is not practical I'd go back to normal strdup/free.
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.
I have to admit I'm a bit skittish about using alloca() too. I agree that it should be portable, and it shouldn't cause any issues. But I'd prefer to be conservative and avoid using it unless there's a meaningful win to be had. Let's follow the wise advice in the man page.
The alloca() function is machine- and compiler-dependent. For certain
applications, its use can improve efficiency compared to the use of
malloc(3) plus free(3). In certain cases, it can also simplify memory
deallocation in applications that use longjmp(3) or siglongjmp(3).
Otherwise, its use is discouraged.
Plus this would be the only use of alloca(3) in the entire ZFS code base. Why add another dependency unless we have too.
argv[] gets modified during string parsing for input arguments. This is reflected in the live process listing. Don't do that. Signed-off-by: DHE <git@dehacked.net>
211f842
to
4bdc1ec
Compare
|
|
argv[] gets modified during string parsing for input arguments. This is reflected in the live process listing. Don't do that. Reviewed-by: Serapheim Dimitropoulos <serapheim@delphix.com> Reviewed-by: loli10K <ezomori.nozomu@gmail.com> Reviewed-by: Giuseppe Di Natale <guss80@gmail.com> Reviewed-by: George Melikov <mail@gmelikov.ru> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: DHE <git@dehacked.net> Closes openzfs#7760
argv[] gets modified during string parsing for input arguments. This is reflected in the live process listing. Don't do that. Reviewed-by: Serapheim Dimitropoulos <serapheim@delphix.com> Reviewed-by: loli10K <ezomori.nozomu@gmail.com> Reviewed-by: Giuseppe Di Natale <guss80@gmail.com> Reviewed-by: George Melikov <mail@gmelikov.ru> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: DHE <git@dehacked.net> Closes openzfs#7760
argv[] gets modified during string parsing for input arguments. This is reflected in the live process listing. Don't do that. Reviewed-by: Serapheim Dimitropoulos <serapheim@delphix.com> Reviewed-by: loli10K <ezomori.nozomu@gmail.com> Reviewed-by: Giuseppe Di Natale <guss80@gmail.com> Reviewed-by: George Melikov <mail@gmelikov.ru> Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov> Signed-off-by: DHE <git@dehacked.net> Closes openzfs#7760
argv[]gets modified during string parsing for input arguments. Thisis reflected in the live process listing. Don't do that.
Signed-off-by: DHE git@dehacked.net
Motivation and Context
psoutput ofzfs sendcurrently never includes any@characters while working because the in-memory commandline has been edited. Other commands have similar missing characters.Description
alloca() + strcpy()the strings, edit those instead.How Has This Been Tested?
Various manual runs with
valgrind --leak-check=fullTypes of changes
Checklist:
Signed-off-by.