Skip to content

Commit 2a6b149

Browse files
kusmagitster
authored andcommitted
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 <[email protected]> Noticed-by: Johannes Sixt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 703f05a commit 2a6b149

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

compat/win32/syslog.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "../../git-compat-util.h"
2-
#include "../../strbuf.h"
32

43
static HANDLE ms_eventlog;
54

@@ -16,13 +15,8 @@ void openlog(const char *ident, int logopt, int facility)
1615

1716
void syslog(int priority, const char *fmt, ...)
1817
{
19-
struct strbuf sb = STRBUF_INIT;
20-
struct strbuf_expand_dict_entry dict[] = {
21-
{"1", "% 1"},
22-
{NULL, NULL}
23-
};
2418
WORD logtype;
25-
char *str;
19+
char *str, *pos;
2620
int str_len;
2721
va_list ap;
2822

@@ -39,11 +33,24 @@ void syslog(int priority, const char *fmt, ...)
3933
}
4034

4135
str = malloc(str_len + 1);
36+
if (!str) {
37+
warning("malloc failed: '%s'", strerror(errno));
38+
return;
39+
}
40+
4241
va_start(ap, fmt);
4342
vsnprintf(str, str_len + 1, fmt, ap);
4443
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+
}
4754

4855
switch (priority) {
4956
case LOG_EMERG:
@@ -66,7 +73,6 @@ void syslog(int priority, const char *fmt, ...)
6673
}
6774

6875
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);
7278
}

0 commit comments

Comments
 (0)