git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin/mv.c: fix possible segfault in add_slash()
@ 2022-09-08 23:02 Shaoxuan Yuan
  2022-09-09  2:21 ` Jeff King
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Shaoxuan Yuan @ 2022-09-08 23:02 UTC (permalink / raw)
  To: git, peff; +Cc: gitster, vdye, derrickstolee, Shaoxuan Yuan

A possible segfault was introduced in c08830de41 (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 segfaults when dest_path[0] is an empty string,
because it was accessing a null value in such case.

Change add_slash() to check the path argument is a non-empty string
before accessing its value.

The purpose of add_slash() is adding a slash to the end of a string to
construct a directory path. And, because adding a slash to an empty
string is of no use here, and checking the string value without checking
it is non-empty leads to segfault, we should make sure the length of the
string is positive to solve both problems.

Reported-by: Jeff King <peff@peff.net>
Helped-by: Jeff King <peff@peff.net>
Signed-off-by: Shaoxuan Yuan <shaoxuan.yuan02@gmail.com>
---
Reference: https://lore.kernel.org/git/YwdJRRuST2SP8ZT7@coredump.intra.peff.net/

 builtin/mv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/mv.c b/builtin/mv.c
index 2d64c1e80f..3413ad1c9b 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -71,7 +71,7 @@ static const char **internal_prefix_pathspec(const char *prefix,
 static const char *add_slash(const char *path)
 {
 	size_t len = strlen(path);
-	if (path[len - 1] != '/') {
+	if (len && path[len - 1] != '/') {
 		char *with_slash = xmalloc(st_add(len, 2));
 		memcpy(with_slash, path, len);
 		with_slash[len++] = '/';

base-commit: e71f9b1de6941c8b449d0c0e17e457f999664bc9
-- 
2.37.0


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-09-09 22:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-08 23:02 [PATCH] builtin/mv.c: fix possible segfault in add_slash() Shaoxuan Yuan
2022-09-09  2:21 ` Jeff King
2022-09-09 14:14 ` Derrick Stolee
2022-09-09 16:37   ` Junio C Hamano
2022-09-09 19:44 ` [PATCH v2] " Shaoxuan Yuan
2022-09-09 20:04   ` Junio C Hamano
2022-09-09 22:40     ` Shaoxuan Yuan
2022-09-09 22:27 ` [PATCH v3] " Shaoxuan Yuan
2022-09-09 22:52   ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).