diff --git a/builtin-log.c b/builtin-log.c
index f4975cf35f..911fd65990 100644
--- a/builtin-log.c
+++ b/builtin-log.c
@@ -461,7 +461,7 @@ static int extra_cc_alloc;
 static void add_header(const char *value)
 {
 	int len = strlen(value);
-	while (value[len - 1] == '\n')
+	while (len && value[len - 1] == '\n')
 		len--;
 	if (!strncasecmp(value, "to: ", 4)) {
 		ALLOC_GROW(extra_to, extra_to_nr + 1, extra_to_alloc);