{"thread":{"id":"34643","subject":"[PATCH] rm: remove unneeded null pointer check","startedAt":"2013-08-08T17:52:41Z","lastAt":"2013-08-08T17:52:41Z","messageCount":1,"participants":["Stefan Beller"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"224836","messageId":"1375984361-2494-1-git-send-email-stefanbeller@googlemail.com","threadId":"34643","inReplyTo":null,"subject":"[PATCH] rm: remove unneeded null pointer check","fromName":"Stefan Beller","fromEmail":"stefanbeller@googlemail.com","sentAt":"2013-08-08T17:52:41Z","receivedAt":"2013-08-08T17:52:41Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"As of 7612a1efdb (2006-06-09 git-rm: honor -n flag.) the variable\n'pathspec' seems to be assumed to be never NULL after calling get_pathspec\nThere was a NULL pointer check after the seen = NULL assignment, which\nwas removed by that commit. So if pathspec would be NULL now, we'd segfault\nin the line accessing the pathspec:\n\tfor (i = 0; pathspec[i] ; i++)\n\nA few lines later, 'pathspec' still cannot be NULL, but that check was\noverlooked, hence removing it now.\n\nAs the null pointer check was removed, it makes no sense to assign NULL\nto seen and 3 lines later another value as there are no conditions in\nbetween.\n\nSigned-off-by: Stefan Beller <stefanbeller@googlemail.com>\n---\n builtin/rm.c | 38 +++++++++++++++++---------------------\n 1 file changed, 17 insertions(+), 21 deletions(-)\n\ndiff --git a/builtin/rm.c b/builtin/rm.c\nindex 0df0b4d..d00eaf8 100644\n--- a/builtin/rm.c\n+++ b/builtin/rm.c\n@@ -277,8 +277,8 @@ static struct option builtin_rm_options[] = {\n \n int cmd_rm(int argc, const char **argv, const char *prefix)\n {\n-\tint i, newfd;\n-\tconst char **pathspec;\n+\tint i, newfd, seen_any;\n+\tconst char **pathspec, *match;\n \tchar *seen;\n \n \tgit_config(git_default_config, NULL);\n@@ -314,7 +314,6 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \tpathspec = get_pathspec(prefix, argv);\n \trefresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);\n \n-\tseen = NULL;\n \tfor (i = 0; pathspec[i] ; i++)\n \t\t/* nothing */;\n \tseen = xcalloc(i, 1);\n@@ -328,27 +327,24 @@ int cmd_rm(int argc, const char **argv, const char *prefix)\n \t\tlist.entry[list.nr++].is_submodule = S_ISGITLINK(ce->ce_mode);\n \t}\n \n-\tif (pathspec) {\n-\t\tconst char *match;\n-\t\tint seen_any = 0;\n-\t\tfor (i = 0; (match = pathspec[i]) != NULL ; i++) {\n-\t\t\tif (!seen[i]) {\n-\t\t\t\tif (!ignore_unmatch) {\n-\t\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n-\t\t\t\t\t    match);\n-\t\t\t\t}\n-\t\t\t}\n-\t\t\telse {\n-\t\t\t\tseen_any = 1;\n+\n+\tseen_any = 0;\n+\tfor (i = 0; (match = pathspec[i]) != NULL ; i++) {\n+\t\tif (!seen[i]) {\n+\t\t\tif (!ignore_unmatch) {\n+\t\t\t\tdie(_(\"pathspec '%s' did not match any files\"),\n+\t\t\t\t    match);\n \t\t\t}\n-\t\t\tif (!recursive && seen[i] == MATCHED_RECURSIVELY)\n-\t\t\t\tdie(_(\"not removing '%s' recursively without -r\"),\n-\t\t\t\t    *match ? match : \".\");\n \t\t}\n-\n-\t\tif (! seen_any)\n-\t\t\texit(0);\n+\t\telse {\n+\t\t\tseen_any = 1;\n+\t\t}\n+\t\tif (!recursive && seen[i] == MATCHED_RECURSIVELY)\n+\t\t\tdie(_(\"not removing '%s' recursively without -r\"),\n+\t\t\t    *match ? match : \".\");\n \t}\n+\tif (!seen_any)\n+\t\texit(0);\n \n \t/*\n \t * If not forced, the file, the index and the HEAD (if exists)\n-- \n1.8.4.rc1.25.gd121ba2\n"}]}