aboutsummaryrefslogtreecommitdiff
path: root/refs.c
diff options
context:
space:
mode:
authorJunio C Hamano <junkio@cox.net>2007-01-26 02:26:04 -0800
committerJunio C Hamano <junkio@cox.net>2007-01-26 02:26:04 -0800
commit8ac65937d032ad3e4bda1d7d6a8b924a65a01fb4 (patch)
tree19cbfff3bee291287b79c004e3779083c822b4ab /refs.c
parent8a8169c0398ff9996216381d7688c2ba2d007050 (diff)
downloadgit-8ac65937d032ad3e4bda1d7d6a8b924a65a01fb4.tar.gz
git-8ac65937d032ad3e4bda1d7d6a8b924a65a01fb4.tar.xz
Make sure we do not write bogus reflog entries.
The file format dictates that entries are LF terminated so the message cannot have one in it. Chomp the message to make sure it only has a single line if necessary, while removing the leading whitespace. Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'refs.c')
-rw-r--r--refs.c39
1 files changed, 23 insertions, 16 deletions
diff --git a/refs.c b/refs.c
index 4323e9a41..0840b3bab 100644
--- a/refs.c
+++ b/refs.c
@@ -925,6 +925,7 @@ static int log_ref_write(struct ref_lock *lock,
{
int logfd, written, oflags = O_APPEND | O_WRONLY;
unsigned maxlen, len;
+ int msglen;
char *logrec;
const char *committer;
@@ -958,24 +959,30 @@ static int log_ref_write(struct ref_lock *lock,
lock->log_file, strerror(errno));
}
- committer = git_committer_info(-1);
+ msglen = 0;
if (msg) {
- maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
- logrec = xmalloc(maxlen);
- len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
- sha1_to_hex(lock->old_sha1),
- sha1_to_hex(sha1),
- committer,
- msg);
- }
- else {
- maxlen = strlen(committer) + 2*40 + 4;
- logrec = xmalloc(maxlen);
- len = snprintf(logrec, maxlen, "%s %s %s\n",
- sha1_to_hex(lock->old_sha1),
- sha1_to_hex(sha1),
- committer);
+ /* clean up the message and make sure it is a single line */
+ for ( ; *msg; msg++)
+ if (!isspace(*msg))
+ break;
+ if (*msg) {
+ const char *ep = strchr(msg, '\n');
+ if (ep)
+ msglen = ep - msg;
+ else
+ msglen = strlen(msg);
+ }
}
+
+ committer = git_committer_info(-1);
+ maxlen = strlen(committer) + msglen + 100;
+ logrec = xmalloc(maxlen);
+ len = sprintf(logrec, "%s %s %s\n",
+ sha1_to_hex(lock->old_sha1),
+ sha1_to_hex(sha1),
+ committer);
+ if (msglen)
+ len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
free(logrec);
close(logfd);