cherry-pick: do not segfault without arguments.
PATCH 1/1 cherry-pick: do not segfault without arguments.
Commit 182d7dc46b (2013-09-05, cherry-pick: allow "-" as abbreviation of '@{-1}') accesses the first argument without checking whether it exists.
--- builtin/revert.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
builtin/revert.c +1 -1
@@ -202,7 +202,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)memset (&opts,0 ,sizeof (opts)); opts.action =REPLAY_PICK ;git_config (git_default_config,NULL ); -if (!strcmp (argv[1 ],"-" )) +if (argc >1 && !strcmp (argv[1 ],"-" )) argv[1 ] ="@{-1}" ;parse_args (argc, argv, &opts); res =sequencer_pick_revisions (&opts);
Signature · 1 line
1.8.4.1.469.gb38b9db
Quoted 2 lines · Stefan Beller
Commit
182d7dc46b(2013-09-05, cherry-pick: allow "-" as abbreviation of '@{-1}') accesses the first argument without checking whether it exists.
I think this is an obviously correct fix for the immediate segfault, but...
builtin/revert.c int cmd_cherry_pick(int argc, const char **argv, const char *prefix) · line 208
Quoted 13 lines
builtin/revert.c +1
-1@@ -202,7 +202,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)memset (&opts,0 ,sizeof (opts)); opts.action =REPLAY_PICK ;git_config (git_default_config,NULL ); -if (!strcmp (argv[1 ],"-" )) +if (argc >1 && !strcmp (argv[1 ],"-" )) argv[1 ] ="@{-1}" ;parse_args (argc, argv, &opts); res =sequencer_pick_revisions (&opts);
Why are we looking at argv/argc at all before calling parse_args? We would want to allow options to come before the "-", no?
In other words, I think the right fix is more like this:
-- >8 --
Subject: [PATCH] cherry-pick: handle "-" after parsing options
Currently, we only try converting argv[1] from "-" into "@{-1}". This means we do not notice "-" when used together with an option. We can fix this by doing the substitution after we know any remaining options must be revisions.
Since we now also come after the check that there is something in argv to cherry-pick, this has the added bonus of avoiding a segfault when "git cherry-pick" is run with no options.
---
I still am unsure if we should be more robust about finding "-" in other options. For example, I think you can cherry-pick multiple commits these days. Should we allow something like:
git cherry-pick foo~2 - bar~5
Certainly the convenience of "-" over "@{-1}" is not as big a deal if you are cherry-picking more than one item, but it seems like we should treat it consistently.
builtin/revert.c | 4 ++-- t/t3501-revert-cherry-pick.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)
builtin/revert.c +2 -2
@@ -168,6 +168,8 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts) opts->revs->no_walk =REVISION_WALK_NO_WALK_UNSORTED ;if (argc <2 )usage_with_options (usage_str, options); +if (!strcmp (argv[1 ],"-" )) + argv[1 ] ="@{-1}" ;memset (&s_r_opt,0 ,sizeof (s_r_opt)); s_r_opt.assume_dashdash =1 ; argc =setup_revisions (argc, argv, opts->revs, &s_r_opt);
@@ -202,8 +204,6 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)memset (&opts,0 ,sizeof (opts)); opts.action =REPLAY_PICK ;git_config (git_default_config,NULL ); -if (!strcmp (argv[1 ],"-" )) - argv[1 ] ="@{-1}" ;parse_args (argc, argv, &opts); res =sequencer_pick_revisions (&opts);if (res <0 )
t/t3501-revert-cherry-pick.sh +12 -0
@@ -129,4 +129,16 @@ test_expect_success 'cherry-pick "-" is meaningless without checkout' ' ) ' +test_expect_success 'cherry-pick "-" works with arguments' ' + git checkout -b side-branch && + test_commit change actual change && + git checkout master && + git cherry-pick -s - && + echo "Signed-off-by: C O Mitter <[email protected]>" >expect && + git cat-file commit HEAD | grep ^Signed-off-by: >signoff && + test_cmp expect signoff && + echo change >expect && + test_cmp expect actual +' + test_done
Signature · 1 line
1.8.4.1.4.gf327177
Quoted 7 lines · Jeff King
Quoted 2 lines · Stefan Beller
Commit
182d7dc46b(2013-09-05, cherry-pick: allow "-" as abbreviation of '@{-1}') accesses the first argument without checking whether it exists.I think this is an obviously correct fix for the immediate segfault, but...
Yes my intention was to fix the obvious, as I assumed the discussion about the exact spec for the '-' has been finished.
Your patch probably makes more sense however.
Quoted 33 lines
Quoted 6 lines
builtin/revert.c +0
-0builtin/revert.c int cmd_cherry_pick(int argc, const char **argv, const char *prefix) · line 208
Quoted 8 lines
builtin/revert.c +1
-1@@ -202,7 +202,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)memset (&opts,0 ,sizeof (opts)); opts.action =REPLAY_PICK ;git_config (git_default_config,NULL ); -if (!strcmp (argv[1 ],"-" )) +if (argc >1 && !strcmp (argv[1 ],"-" )) argv[1 ] ="@{-1}" ;parse_args (argc, argv, &opts); res =sequencer_pick_revisions (&opts);Why are we looking at argv/argc at all before calling
parse_args? We would want to allow options to come before the "-", no?In other words, I think the right fix is more like this:
-- >8 --
Subject: [PATCH] cherry-pick: handle "-" after parsing optionsCurrently, we only try converting argv[1] from "-" into "@{-1}". This means we do not notice "-" when used together with an option. We can fix this by doing the substitution after we know any remaining options must be revisions.
Since we now also come after the check that there is something in argv to cherry-pick, this has the added bonus of avoiding a segfault when "git cherry-pick" is run with no options.
Quoted 5 lines
Noticed-by Stefan Beller <[email protected]>Signed-off-by Jeff King <[email protected]>---
I still am unsure if we should be more robust about finding "-" in other options.
There was a discusssion about it recently. - would generally mean the last thing (kind of an undo), so git checkout - would switch to the last branch. It's similar to 'cd -'.
So I am not sure if this is generally @{-1}, or if we need to have other references for other commands.
For example, I think you can cherry-pick multiple commits these
Quoted 59 lines
days. Should we allow something like:
git cherry-pick foo~2 - bar~5
Certainly the convenience of "-" over "@{-1}" is not as big a deal if you are cherry-picking more than one item, but it seems like we should treat it consistently.
builtin/revert.c | 4 ++-- t/t3501-revert-cherry-pick.sh | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)builtin/revert.c +2
-2@@ -168,6 +168,8 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts) opts->revs->no_walk =REVISION_WALK_NO_WALK_UNSORTED ;if (argc <2 )usage_with_options (usage_str, options); +if (!strcmp (argv[1 ],"-" )) + argv[1 ] ="@{-1}" ;memset (&s_r_opt,0 ,sizeof (s_r_opt)); s_r_opt.assume_dashdash =1 ; argc =setup_revisions (argc, argv, opts->revs, &s_r_opt);@@ -202,8 +204,6 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)memset (&opts,0 ,sizeof (opts)); opts.action =REPLAY_PICK ;git_config (git_default_config,NULL ); -if (!strcmp (argv[1 ],"-" )) - argv[1 ] ="@{-1}" ;parse_args (argc, argv, &opts); res =sequencer_pick_revisions (&opts);if (res <0 )t/t3501-revert-cherry-pick.sh +12
-0@@ -129,4 +129,16 @@ test_expect_success 'cherry-pick "-" is meaningless without checkout' ' ) ' +test_expect_success 'cherry-pick "-" works with arguments' ' + git checkout -b side-branch && + test_commit change actual change && + git checkout master && + git cherry-pick -s - && + echo "Signed-off-by: C O Mitter <[email protected]>" >expect && + git cat-file commit HEAD | grep ^Signed-off-by: >signoff && + test_cmp expect signoff && + echo change >expect && + test_cmp expect actual +' + test_done