@matheusbsbs / matheus-bagliano / commits / 6490a3383f
Fork 0

Fix work-tree related breakages

In set_work_tree(), variable rel needs to be reinitialized to NULL on every call (it should not be static). Make sure the incoming dir variable is not too long before copying to the temporary buffer, and make sure chdir to the resulting directory succeeds. This was spotted and fixed by Alex and Johannes in a handful patch exchanges. Here is the final version. Signed-off-by: Junio C Hamano <gitster@pobox.com> Acked-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

Junio C Hamano committed Aug 2, 2007 at 15:10 UTC 6490a3383f1d0d96c122069e510ef1af1d019fbb
1 file changed +14 -8
setup.c
+14 -8
@@ -201,26 +201,32 @@ int is_inside_work_tree(void)
201 */
202 const char *set_work_tree(const char *dir)
203 {
204 - char dir_buffer[PATH_MAX];
205 - static char buffer[PATH_MAX + 1], *rel = NULL;
206 - int len, postfix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
204 + char dir_buffer[PATH_MAX], *rel = NULL;
205 + static char buffer[PATH_MAX + 1];
206 + int len, suffix_len = strlen(DEFAULT_GIT_DIR_ENVIRONMENT) + 1;
207
208 /* strip the variable 'dir' of the postfix "/.git" if it has it */
209 len = strlen(dir);
210 - if (len > postfix_len && !strcmp(dir + len - postfix_len,
211 - "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
212 - strncpy(dir_buffer, dir, len - postfix_len);
210 + if (len > suffix_len &&
211 + !strcmp(dir + len - suffix_len, "/" DEFAULT_GIT_DIR_ENVIRONMENT)) {
212 + if ((len - suffix_len) >= sizeof(dir_buffer))
213 + die("directory name too long");
214 + memcpy(dir_buffer, dir, len - suffix_len);
215 + dir_buffer[len - suffix_len] = '\0';
216
217 /* are we inside the default work tree? */
218 rel = get_relative_cwd(buffer, sizeof(buffer), dir_buffer);
219 }
220 +
221 /* if rel is set, the cwd is _not_ the current working tree */
222 if (rel && *rel) {
223 if (!is_absolute_path(dir))
224 set_git_dir(make_absolute_path(dir));
225 dir = dir_buffer;
222 - chdir(dir);
223 - strcat(rel, "/");
226 + if (chdir(dir))
227 + die("cannot chdir to %s: %s", dir, strerror(errno));
228 + else
229 + strcat(rel, "/");
230 inside_git_dir = 0;
231 } else {
232 rel = NULL;