git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add mksnpath and git_snpath which allow to specify the output buffer
@ 2008-10-26 21:59 Alex Riesen
  2008-10-26 22:07 ` [PATCH] Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.c Alex Riesen
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Alex Riesen @ 2008-10-26 21:59 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano

Both are actually just vsnprintf's but additionally call cleanup_path
on the result. To be used as alternatives to mkpath and git_path where
the buffer for the created path may not be reused by subsequent calls
of the same formatting function.

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 cache.h |    4 ++++
 path.c  |   38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)

diff --git a/cache.h b/cache.h
index b0edbf9..a9024db 100644
--- a/cache.h
+++ b/cache.h
@@ -495,6 +495,10 @@ extern int check_repository_format(void);
 #define DATA_CHANGED    0x0020
 #define TYPE_CHANGED    0x0040
 
+extern char *mksnpath(char *buf, size_t n, const char *fmt, ...)
+	__attribute__((format (printf, 3, 4)));
+extern char *git_snpath(char *buf, size_t n, const char *fmt, ...)
+	__attribute__((format (printf, 3, 4)));
 /* 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)));
diff --git a/path.c b/path.c
index 76e8872..85ab28a 100644
--- a/path.c
+++ b/path.c
@@ -32,6 +32,44 @@ static char *cleanup_path(char *path)
 	return path;
 }
 
+char *mksnpath(char *buf, size_t n, const char *fmt, ...)
+{
+	va_list args;
+	unsigned len;
+
+	va_start(args, fmt);
+	len = vsnprintf(buf, n, fmt, args);
+	va_end(args);
+	if (len >= n) {
+		snprintf(buf, n, bad_path);
+		return buf;
+	}
+	return cleanup_path(buf);
+}
+
+char *git_snpath(char *buf, size_t n, const char *fmt, ...)
+{
+	const char *git_dir = get_git_dir();
+	va_list args;
+	size_t len;
+
+	len = strlen(git_dir);
+	if (n < len + 1)
+		goto bad;
+	memcpy(buf, git_dir, len);
+	if (len && !is_dir_sep(git_dir[len-1]))
+		buf[len++] = '/';
+	va_start(args, fmt);
+	len += vsnprintf(buf + len, n - len, fmt, args);
+	va_end(args);
+	if (len >= n)
+		goto bad;
+	return cleanup_path(buf);
+bad:
+	snprintf(buf, n, bad_path);
+	return buf;
+}
+
 char *mkpath(const char *fmt, ...)
 {
 	va_list args;
-- 
1.6.0.3.540.g3f8b

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

end of thread, other threads:[~2008-10-28 17:33 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-26 21:59 [PATCH] Add mksnpath and git_snpath which allow to specify the output buffer Alex Riesen
2008-10-26 22:07 ` [PATCH] Fix mkpath abuse in dwim_ref and dwim_log of sha1_name.c Alex Riesen
2008-10-26 22:08 ` [PATCH] Fix potentially dangerous uses of mkpath and git_path Alex Riesen
2008-10-27  7:08   ` Johannes Sixt
2008-10-27  8:30     ` Alex Riesen
2008-10-27  5:07 ` [PATCH] Add mksnpath and git_snpath which allow to specify the output buffer Junio C Hamano
2008-10-27  6:45   ` Alex Riesen
2008-10-28  3:35     ` Junio C Hamano
2008-10-28 12:47       ` Alex Riesen
2008-10-28 17:31         ` Alex Riesen

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).