git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Allow git_author|committer_info be called in the same expression
@ 2007-07-31 16:57 Alex Riesen
  2007-08-01  9:24 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Riesen @ 2007-07-31 16:57 UTC (permalink / raw)
  To: Kristian Høgsberg; +Cc: Junio C Hamano, git

[-- Attachment #1: Type: text/plain, Size: 1180 bytes --]

Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 ident.c |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

On 7/31/07, Kristian Høgsberg <krh@redhat.com> wrote:
> There's number of buffers that don't get freed: the strbuf, the commit
> message buffer, and the strdup'ed author and committer info.  All the
> leaks are not critical since the process exits immediately.  As for the
> strbuf leak, I was thinking about renaming strbuf_begin to strbuf_reset
> and making it public[1], which will then be used for freeing up strbuf
> memory.  The message buffer leak should be fixed by adding a
> strbuf_read_fd() that just reads it straight into the strbuf.  The
> xstrdup's are necessary because fmt_ident uses a static buffer (thanks,
> test case :).  We could add rotating static buffers for fmt_ident like
> git_path and avoid the strdups, but again, the leaks are not critical.

Ach, that's why... Junio, how about this then? I'd even suggest adding
the buffer argument to fmt_ident and use it everywhere (ATM there is
only one user outside of ident.c: builtin-blame.c).
This one is applicable immediately, though

[-- Attachment #2: 0001-Allow-git_author-committer_info-be-called-in-the-same.txt --]
[-- Type: text/plain, Size: 2862 bytes --]

From 0c86739aa9f5db19ebcd2bc041622c317611c87f Mon Sep 17 00:00:00 2001
From: Alex Riesen <raa.lkml@gmail.com>
Date: Tue, 31 Jul 2007 18:48:23 +0200
Subject: [PATCH] Allow git_author|committer_info be called in the same expression


Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
 ident.c |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/ident.c b/ident.c
index 6612d17..0d59090 100644
--- a/ident.c
+++ b/ident.c
@@ -192,10 +192,10 @@ static const char *env_hint =
 "Add --global to set your account\'s default\n"
 "\n";
 
-const char *fmt_ident(const char *name, const char *email,
-		      const char *date_str, int error_on_no_name)
+#define FMT_IDENT_BUFSIZE 1000
+static char *fmt_ident_buf(char *buffer, const char *name, const char *email,
+			   const char *date_str, int error_on_no_name)
 {
-	static char buffer[1000];
 	char date[50];
 	int i;
 
@@ -227,29 +227,44 @@ const char *fmt_ident(const char *name, const char *email,
 	if (date_str)
 		parse_date(date_str, date, sizeof(date));
 
-	i = copy(buffer, sizeof(buffer), 0, name);
-	i = add_raw(buffer, sizeof(buffer), i, " <");
-	i = copy(buffer, sizeof(buffer), i, email);
-	i = add_raw(buffer, sizeof(buffer), i, "> ");
-	i = copy(buffer, sizeof(buffer), i, date);
-	if (i >= sizeof(buffer))
+	i = copy(buffer, FMT_IDENT_BUFSIZE, 0, name);
+	i = add_raw(buffer, FMT_IDENT_BUFSIZE, i, " <");
+	i = copy(buffer, FMT_IDENT_BUFSIZE, i, email);
+	i = add_raw(buffer, FMT_IDENT_BUFSIZE, i, "> ");
+	i = copy(buffer, FMT_IDENT_BUFSIZE, i, date);
+	if (i >= FMT_IDENT_BUFSIZE)
 		die("Impossibly long personal identifier");
 	buffer[i] = 0;
 	return buffer;
 }
 
+const char *fmt_ident(const char *name, const char *email,
+		      const char *date_str, int error_on_no_name)
+{
+	static char buffer[FMT_IDENT_BUFSIZE];
+	return fmt_ident_buf(buffer, name, email, date_str, error_on_no_name);
+}
+
 const char *git_author_info(int error_on_no_name)
 {
-	return fmt_ident(getenv("GIT_AUTHOR_NAME"),
-			 getenv("GIT_AUTHOR_EMAIL"),
-			 getenv("GIT_AUTHOR_DATE"),
-			 error_on_no_name);
+	static char *buffer;
+	if (!buffer)
+		buffer = xmalloc(FMT_IDENT_BUFSIZE);
+	return fmt_ident_buf(buffer,
+			     getenv("GIT_AUTHOR_NAME"),
+			     getenv("GIT_AUTHOR_EMAIL"),
+			     getenv("GIT_AUTHOR_DATE"),
+			     error_on_no_name);
 }
 
 const char *git_committer_info(int error_on_no_name)
 {
-	return fmt_ident(getenv("GIT_COMMITTER_NAME"),
-			 getenv("GIT_COMMITTER_EMAIL"),
-			 getenv("GIT_COMMITTER_DATE"),
-			 error_on_no_name);
+	static char *buffer;
+	if (!buffer)
+		buffer = xmalloc(FMT_IDENT_BUFSIZE);
+	return fmt_ident_buf(buffer,
+			     getenv("GIT_COMMITTER_NAME"),
+			     getenv("GIT_COMMITTER_EMAIL"),
+			     getenv("GIT_COMMITTER_DATE"),
+			     error_on_no_name);
 }
-- 
1.5.3.rc3.132.g39179


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

end of thread, other threads:[~2007-08-01  9:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-31 16:57 [PATCH] Allow git_author|committer_info be called in the same expression Alex Riesen
2007-08-01  9:24 ` Junio C Hamano
2007-08-01  9:35   ` 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).