mingw: avoid using strbuf in syslog

strbuf can call die, which again can call syslog from git-daemon. Endless recursion is no fun; fix it by hand-rolling the logic. As a side-effect malloc/realloc errors are changed into non-fatal warnings; this is probably an improvement anyway. Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com> Noticed-by: Johannes Sixt <j.sixt@viscovery.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Faye-Lund committed Oct 6, 2011 at 19:52 UTC 2a6b149c64f6ba8cc3eef70c6901ee0c67a137a7
1 file changed +18 -12
compat/win32/syslog.c
+18 -12
@@ -1,5 +1,4 @@
1 #include "../../git-compat-util.h"
2 -#include "../../strbuf.h"
2
3 static HANDLE ms_eventlog;
4
@@ -16,13 +15,8 @@ void openlog(const char *ident, int logopt, int facility)
15
16 void syslog(int priority, const char *fmt, ...)
17 {
19 - struct strbuf sb = STRBUF_INIT;
20 - struct strbuf_expand_dict_entry dict[] = {
21 - {"1", "% 1"},
22 - {NULL, NULL}
23 - };
18 WORD logtype;
25 - char *str;
19 + char *str, *pos;
20 int str_len;
21 va_list ap;
22
@@ -39,11 +33,24 @@ void syslog(int priority, const char *fmt, ...)
33 }
34
35 str = malloc(str_len + 1);
36 + if (!str) {
37 + warning("malloc failed: '%s'", strerror(errno));
38 + return;
39 + }
40 +
41 va_start(ap, fmt);
42 vsnprintf(str, str_len + 1, fmt, ap);
43 va_end(ap);
45 - strbuf_expand(&sb, str, strbuf_expand_dict_cb, &dict);
46 - free(str);
44 +
45 + while ((pos = strstr(str, "%1")) != NULL) {
46 + str = realloc(str, ++str_len + 1);
47 + if (!str) {
48 + warning("realloc failed: '%s'", strerror(errno));
49 + return;
50 + }
51 + memmove(pos + 2, pos + 1, strlen(pos));
52 + pos[1] = ' ';
53 + }
54
55 switch (priority) {
56 case LOG_EMERG:
@@ -66,7 +73,6 @@ void syslog(int priority, const char *fmt, ...)
73 }
74
75 ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
69 - (const char **)&sb.buf, NULL);
70 -
71 - strbuf_release(&sb);
76 + (const char **)&str, NULL);
77 + free(str);
78 }