[PATCH 8/8] Enable GIT_DEBUG_MEMCHECK on git_pathname()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Make git_pathname() use xmalloc_mmap/xfree_mmap to catch invalid access
to old buffer when it's already overwritten.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx>
---
 cache.h |   11 +++++++----
 path.c  |   28 +++++++++++++++++++---------
 2 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/cache.h b/cache.h
index feb44a5..66365fb 100644
--- a/cache.h
+++ b/cache.h
@@ -661,10 +661,13 @@ extern char *git_pathdup(const char *fmt, ...)
 	__attribute__((format (printf, 1, 2)));
 
 /* Return a statically allocated filename matching the sha1 signature */
-extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
-extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
-extern char *git_path_submodule(const char *path, const char *fmt, ...)
-	__attribute__((format (printf, 2, 3)));
+#define mkpath(...) mkpath_real(__FUNCTION__, __LINE__, __VA_ARGS__)
+extern char *mkpath_real(const char *file, int line, const char *fmt, ...) __attribute__((format (printf, 3, 4)));
+#define git_path( ...) git_path_real(__FUNCTION__, __LINE__, __VA_ARGS__)
+extern char *git_path_real(const char *file, int line, const char *fmt, ...) __attribute__((format (printf, 3, 4)));
+#define git_path_submodule(path, ...) git_path_submodule_real(__FUNCTION__, __LINE__, path, __VA_ARGS__)
+extern char *git_path_submodule_real(const char *file, int line, const char *path, const char *fmt, ...)
+	__attribute__((format (printf, 4, 5)));
 
 extern char *sha1_file_name(const unsigned char *sha1);
 extern char *sha1_pack_name(const unsigned char *sha1);
diff --git a/path.c b/path.c
index b6f71d1..d2aa941 100644
--- a/path.c
+++ b/path.c
@@ -15,11 +15,21 @@
 
 static char bad_path[] = "/bad-path/";
 
-static char *get_pathname(void)
+static char *get_pathname(const char *file, int line)
 {
-	static char pathname_array[4][PATH_MAX];
+	static char real_pathname_array[4][PATH_MAX];
+	static char *pathname_array[4];
 	static int index;
-	return pathname_array[3 & ++index];
+	int idx = 3 & ++index;
+
+	if (!pathname_array[idx] && !getenv("GIT_DEBUG_MEMCHECK"))
+		pathname_array[idx] = real_pathname_array[idx];
+
+	if (pathname_array[idx] != real_pathname_array[idx]) {
+		xfree_mmap(pathname_array[idx]);
+		pathname_array[idx] = xmalloc_mmap(PATH_MAX, file, line);
+	}
+	return pathname_array[idx];
 }
 
 static char *cleanup_path(char *path)
@@ -87,11 +97,11 @@ char *git_pathdup(const char *fmt, ...)
 	return xstrdup(path);
 }
 
-char *mkpath(const char *fmt, ...)
+char *mkpath_real(const char *file, int line, const char *fmt, ...)
 {
 	va_list args;
 	unsigned len;
-	char *pathname = get_pathname();
+	char *pathname = get_pathname(file, line);
 
 	va_start(args, fmt);
 	len = vsnprintf(pathname, PATH_MAX, fmt, args);
@@ -101,10 +111,10 @@ char *mkpath(const char *fmt, ...)
 	return cleanup_path(pathname);
 }
 
-char *git_path(const char *fmt, ...)
+char *git_path_real(const char *file, int line, const char *fmt, ...)
 {
 	const char *git_dir = get_git_dir();
-	char *pathname = get_pathname();
+	char *pathname = get_pathname(file, line);
 	va_list args;
 	unsigned len;
 
@@ -122,9 +132,9 @@ char *git_path(const char *fmt, ...)
 	return cleanup_path(pathname);
 }
 
-char *git_path_submodule(const char *path, const char *fmt, ...)
+char *git_path_submodule_real(const char *file, int line, const char *path, const char *fmt, ...)
 {
-	char *pathname = get_pathname();
+	char *pathname = get_pathname(file, line);
 	struct strbuf buf = STRBUF_INIT;
 	const char *git_dir;
 	va_list args;
-- 
1.7.4.74.g639db

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]