@matheusbsbs / matheus-bagliano / commits / f8b5a8e13c
Fork 0

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>

Junio C Hamano committed Jun 27, 2009 at 17:58 UTC f8b5a8e13cb4d60c8b630f92a8f07590ef218ec5
1 file changed +18 -2
usage.c
+18 -2
@@ -64,8 +64,24 @@ void die_errno(const char *fmt, ...)
64 {
65 va_list params;
66 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);
85
86 va_start(params, fmt);
87 die_routine(fmt_with_err, params);