From d1db74bf57999ac336fddadf2f9ee7c24961268f Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 29 Oct 2011 17:40:04 +0200 Subject: [PATCH 1/3] status: Prevent segfaulting when determining the status of a repository Fixes #465 --- src/status.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/status.c b/src/status.c index 1fc3794ea..a3d6ee897 100644 --- a/src/status.c +++ b/src/status.c @@ -183,26 +183,28 @@ static int process_folder(struct status_st *st, const git_tree_entry *tree_entry git_object *subtree = NULL; git_tree *pushed_tree = NULL; int error, pushed_tree_position = 0; - git_otype tree_entry_type; + git_otype tree_entry_type = GIT_OBJ_BAD; - tree_entry_type = git_tree_entry_type(tree_entry); + if (tree_entry != NULL) { + tree_entry_type = git_tree_entry_type(tree_entry); - switch (tree_entry_type) { - case GIT_OBJ_TREE: - error = git_tree_entry_2object(&subtree, ((git_object *)(st->tree))->repo, tree_entry); - pushed_tree = st->tree; - pushed_tree_position = st->tree_position; - st->tree = (git_tree *)subtree; - st->tree_position = 0; - st->head_tree_relative_path_len += 1 + tree_entry->filename_len; /* path + '/' + name */ - break; + switch (tree_entry_type) { + case GIT_OBJ_TREE: + error = git_tree_entry_2object(&subtree, ((git_object *)(st->tree))->repo, tree_entry); + pushed_tree = st->tree; + pushed_tree_position = st->tree_position; + st->tree = (git_tree *)subtree; + st->tree_position = 0; + st->head_tree_relative_path_len += 1 + tree_entry->filename_len; /* path + '/' + name */ + break; - case GIT_OBJ_BLOB: - /* No op */ - break; + case GIT_OBJ_BLOB: + /* No op */ + break; - default: - error = git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type"); /* TODO: How should we deal with submodules? */ + default: + error = git__throw(GIT_EINVALIDTYPE, "Unexpected tree entry type"); /* TODO: How should we deal with submodules? */ + } } if (full_path != NULL && path_type == GIT_STATUS_PATH_FOLDER) From a1bd78ea29caa05cf44e57f525d5b11fd9e2dc3a Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 29 Oct 2011 21:29:31 +0200 Subject: [PATCH 2/3] status: Add a file in the test repository to cover the correct sorting of entries when the working folder is being read In this case, "subdir.txt" should be listed before the "subdir" directory. --- tests/resources/status/.gitted/COMMIT_EDITMSG | Bin 11 -> 105 bytes tests/resources/status/.gitted/ORIG_HEAD | Bin 41 -> 41 bytes tests/resources/status/.gitted/index | Bin 1080 -> 1160 bytes tests/resources/status/.gitted/logs/HEAD | Bin 315 -> 563 bytes .../status/.gitted/logs/refs/heads/master | Bin 315 -> 563 bytes .../26/a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f | Bin 0 -> 225 bytes .../37/fcb02ccc1a85d1941e7f106d52dc3702dcf0d0 | Bin 0 -> 331 bytes .../e8/ee89e15bbe9b20137715232387b3de5b28972e | Bin 0 -> 38 bytes .../status/.gitted/refs/heads/master | Bin 41 -> 41 bytes tests/resources/status/subdir.txt | Bin 0 -> 29 bytes 10 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/resources/status/.gitted/objects/26/a125ee1bfc5df1e1b2e9441bbe63c8a7ae989f create mode 100644 tests/resources/status/.gitted/objects/37/fcb02ccc1a85d1941e7f106d52dc3702dcf0d0 create mode 100644 tests/resources/status/.gitted/objects/e8/ee89e15bbe9b20137715232387b3de5b28972e create mode 100644 tests/resources/status/subdir.txt diff --git a/tests/resources/status/.gitted/COMMIT_EDITMSG b/tests/resources/status/.gitted/COMMIT_EDITMSG index ff887ba1367b9d5f6ddd8e58e8aa696d648ffcd0..1a25cd4a63320cce047eda4076d93a7af168e0f4 100644 GIT binary patch literal 105 zcmW;D%MHUY3Vp`PZ<>Ol;QnxWc+p p5{0!T(T$k4y{PAVLT#bU8(p-}mmic89mBh!9M9g991?%#>jbE+O-jiu;sO8|Cm}F-c4^Oiea6FflPm0`e164b3bJlGD-*jes(yX@+TLTmb8<3pM}% diff --git a/tests/resources/status/.gitted/index b/tests/resources/status/.gitted/index index 5c4b18841c44de70476e5789a71db949eefbe01b..d793791c9ecac95e4e7eae12dac6c301b0167b75 100644 GIT binary patch delta 289 zcmdnN(ZOlp;u+-3z`(!+#JqlM?0*7j2EP@a5+E7@8kfM>6AjKP6QOLf0^?idH4gJZ z+VB}S*?{S7{Th2$u(~z&?J#Q~G{RaSU-rej&WF+aW-ADniz+L(Z@w3;FwI#3g;Q7#JFtFfcHF1xkrbG&rk_U0E)& zvdIdJZ*izAKv8GF1X5QEwH8c6t*t^)H-Y(yNanZ4-wz4BUUzZT$5Y-06?`R6l}_% p;y}xOP{!xyCD{}V@?SNz3cfp$0f$1PFOJmb|Gi{j`R6l}_% p;y}xOP{!xyCD{}V@?SNz3cfp$0f$1PFOJmb|Gi{j4HDNF`FfcPQQAjQ=DoV{OiBHSSNo81Yua4^w!;#J-GLiRJ z3W$0YsQKkV)q&*WQ&MwMOHxx9LV_;q%U-p3&BFR(h0Vd%@AFh&@I%$*=BH$)Wu~S; z40P3Va@l&J`R)fduJdwdN^Z|RzfcOQu(%{K9jGihBQY;MwV1&uz`LlpDMWuh$2^t4 zw~jtwFl!Z?=MPxu~*o`{sMm8q@Vm41hqP7_5k4 dmHIMA*9;E@{@e96Gk2N&STI$i902&UwRWHzr-=Xn literal 0 HcmV?d00001 diff --git a/tests/resources/status/.gitted/objects/e8/ee89e15bbe9b20137715232387b3de5b28972e b/tests/resources/status/.gitted/objects/e8/ee89e15bbe9b20137715232387b3de5b28972e new file mode 100644 index 0000000000000000000000000000000000000000..cfc2413d5ee7b0e6bbe13ce5dcfdb3c5767e8176 GIT binary patch literal 38 ucmb`p8uz^f~D5PKHbOxRn4)!V!A_ literal 0 HcmV?d00001 diff --git a/tests/resources/status/.gitted/refs/heads/master b/tests/resources/status/.gitted/refs/heads/master index b46871fd6ec98de0a7c9fd80a4f255b7fcfc8b65..3e2e2a07a98b4978335419a066b924bed2676d04 100644 GIT binary patch literal 41 ucmXpqOEfexO-(gSN=r6PNizg8j8ZL4ObnBfQq7E$EfUQWQ!Onl)3^ZnZ415t literal 41 vcmV~$Nf7`r2n4Wy)f6a~99Rbb5}LSZTd@X)(kj-GkK&+-NK{D5EK0HG@`UmVauV}W?YRJm1PJ&5 literal 0 HcmV?d00001 From e3baa3ccf3b606517b3819913228c030854b8d77 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Sat, 29 Oct 2011 17:45:01 +0200 Subject: [PATCH 3/3] status: Fix a sorting issue in the treewalker This ensures that entries from the working directory are retrieved according to the following rules: - The file "subdir" should appear before the file "subdir.txt" - The folder "subdir" should appear after the file "subdir.txt" --- src/status.c | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/status.c b/src/status.c index a3d6ee897..d50199d9a 100644 --- a/src/status.c +++ b/src/status.c @@ -291,7 +291,7 @@ static int path_type_from(char *full_path, int is_dir) if (!is_dir) return GIT_STATUS_PATH_FILE; - if (!git__suffixcmp(full_path, "/" DOT_GIT)) + if (!git__suffixcmp(full_path, "/" DOT_GIT "/")) return GIT_STATUS_PATH_IGNORE; return GIT_STATUS_PATH_FOLDER; @@ -360,7 +360,13 @@ static int dirent_cb(void *state, char *a) if (m != NULL) { st->head_tree_relative_path[st->head_tree_relative_path_len] = '\0'; - git_path_join(st->head_tree_relative_path, st->head_tree_relative_path, m->filename); + + /* When the tree entry is a folder, append a forward slash to its name */ + if (git_tree_entry_type(m) == GIT_OBJ_TREE) + git_path_join_n(st->head_tree_relative_path, 3, st->head_tree_relative_path, m->filename, ""); + else + git_path_join(st->head_tree_relative_path, st->head_tree_relative_path, m->filename); + m_name = st->head_tree_relative_path; } else m_name = NULL; @@ -378,7 +384,7 @@ static int dirent_cb(void *state, char *a) if((error = determine_status(st, pm != NULL, pi != NULL, pa != NULL, m, entry, a, status_path(pm, pi, pa), path_type)) < GIT_SUCCESS) return git__rethrow(error, "An error occured while determining the status of '%s'", a); - if (pa != NULL) + if ((pa != NULL) || (path_type == GIT_STATUS_PATH_FOLDER)) return GIT_SUCCESS; } } @@ -571,19 +577,32 @@ struct alphasorted_dirent_info { static struct alphasorted_dirent_info *alphasorted_dirent_info_new(const char *path) { - int is_dir; + int is_dir, size; struct alphasorted_dirent_info *di; is_dir = git_futils_isdir(path) == GIT_SUCCESS ? 1 : 0; + size = sizeof(*di) + (is_dir ? GIT_PATH_MAX : strlen(path)) + 2; - di = git__malloc(sizeof(*di) + (is_dir ? GIT_PATH_MAX : strlen(path)) + 1); + di = git__malloc(size); if (di == NULL) return NULL; - memset(di, 0x0, sizeof(*di)); + memset(di, 0x0, size); strcpy(di->path, path); - di->is_dir = is_dir; + + if (is_dir) { + di->is_dir = 1; + + /* + * Append a forward slash to the name to force folders + * to be ordered in a similar way than in a tree + * + * The file "subdir" should appear before the file "subdir.txt" + * The folder "subdir" should appear after the file "subdir.txt" + */ + di->path[strlen(path)] = '/'; + } return di; }