Skip to content

Commit f8b5a8e

Browse files
committed
die_errno(): double % in strerror() output just in case
[tr: handle border case where % is placed at end of buffer] Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b875036 commit f8b5a8e

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

usage.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,24 @@ void die_errno(const char *fmt, ...)
6464
{
6565
va_list params;
6666
char fmt_with_err[1024];
67-
68-
snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, strerror(errno));
67+
char str_error[256], *err;
68+
int i, j;
69+
70+
err = strerror(errno);
71+
for (i = j = 0; err[i] && j < sizeof(str_error) - 1; ) {
72+
if ((str_error[j++] = err[i++]) != '%')
73+
continue;
74+
if (j < sizeof(str_error) - 1) {
75+
str_error[j++] = '%';
76+
} else {
77+
/* No room to double the '%', so we overwrite it with
78+
* '\0' below */
79+
j--;
80+
break;
81+
}
82+
}
83+
str_error[j] = 0;
84+
snprintf(fmt_with_err, sizeof(fmt_with_err), "%s: %s", fmt, str_error);
6985

7086
va_start(params, fmt);
7187
die_routine(fmt_with_err, params);

0 commit comments

Comments
 (0)