Skip to content

Commit 7ead468

Browse files
ffyuandagitster
authored andcommitted
builtin/mv.c: fix possible segfault in add_slash()
A possible segfault was introduced in c08830d (mv: check if <destination> is a SKIP_WORKTREE_DIR, 2022-08-09). When running t7001 with SANITIZE=address, problem appears when running: git mv path1/path2/ . or git mv directory ../ or any <destination> that makes dest_path[0] an empty string. The add_slash() call could segfault when path argument to it is an empty string, because it makes an out-of-bounds read to decide if an extra slash '/' needs to be appended to it. As add_slash() is used to make sure that a valid pathname to a file in the given directory can be made by appending a filename after the value returned from it, if path is an empty string, we want to return it as-is. The path to a file "F" in the top-level of the working tree (i.e. path=="") is formed by appending "F" after "" (i.e. path) without any slash in between. So, just like the case where a non-empty path already ends with a slash, return an empty path as-is. Reported-by: Jeff King <[email protected]> Helped-by: Jeff King <[email protected]> Helped-by: Junio C Hamano <[email protected]> Helped-by: Derrick Stolee <[email protected]> Signed-off-by: Shaoxuan Yuan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent da6fe05 commit 7ead468

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

builtin/mv.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ static const char **internal_prefix_pathspec(const char *prefix,
7171
static const char *add_slash(const char *path)
7272
{
7373
size_t len = strlen(path);
74-
if (path[len - 1] != '/') {
74+
if (len && path[len - 1] != '/') {
7575
char *with_slash = xmalloc(st_add(len, 2));
7676
memcpy(with_slash, path, len);
7777
with_slash[len++] = '/';

0 commit comments

Comments
 (0)