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>
Junio C Hamano committed
Jan 26, 2007 at 02:26 UTC
8ac65937d032ad3e4bda1d7d6a8b924a65a01fb4
2 files changed
+23
-18
builtin-update-ref.c
-2
@@ -23,8 +23,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
23
msg = argv[++i];
24
if (!*msg)
25
die("Refusing to perform update with empty message.");
26
- if (strchr(msg, '\n'))
27
- die("Refusing to perform update with \\n in message.");
26
continue;
27
}
28
if (!strcmp("-d", argv[i])) {
refs.c
+23
-16
@@ -925,6 +925,7 @@ static int log_ref_write(struct ref_lock *lock,
925
{
926
int logfd, written, oflags = O_APPEND | O_WRONLY;
927
unsigned maxlen, len;
928
+ int msglen;
929
char *logrec;
930
const char *committer;
931
@@ -958,24 +959,30 @@ static int log_ref_write(struct ref_lock *lock,
959
lock->log_file, strerror(errno));
960
}
961
961
- committer = git_committer_info(-1);
962
+ msglen = 0;
963
if (msg) {
963
- maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
964
- logrec = xmalloc(maxlen);
965
- len = snprintf(logrec, maxlen, "%s %s %s\t%s\n",
966
- sha1_to_hex(lock->old_sha1),
967
- sha1_to_hex(sha1),
968
- committer,
969
- msg);
970
- }
971
- else {
972
- maxlen = strlen(committer) + 2*40 + 4;
973
- logrec = xmalloc(maxlen);
974
- len = snprintf(logrec, maxlen, "%s %s %s\n",
975
- sha1_to_hex(lock->old_sha1),
976
- sha1_to_hex(sha1),
977
- committer);
964
+ /* clean up the message and make sure it is a single line */
965
+ for ( ; *msg; msg++)
966
+ if (!isspace(*msg))
967
+ break;
968
+ if (*msg) {
969
+ const char *ep = strchr(msg, '\n');
970
+ if (ep)
971
+ msglen = ep - msg;
972
+ else
973
+ msglen = strlen(msg);
974
+ }
975
}
976
+
977
+ committer = git_committer_info(-1);
978
+ maxlen = strlen(committer) + msglen + 100;
979
+ logrec = xmalloc(maxlen);
980
+ len = sprintf(logrec, "%s %s %s\n",
981
+ sha1_to_hex(lock->old_sha1),
982
+ sha1_to_hex(sha1),
983
+ committer);
984
+ if (msglen)
985
+ len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
986
written = len <= maxlen ? write_in_full(logfd, logrec, len) : -1;
987
free(logrec);
988
close(logfd);