a6a243e94a
Git for Windows' SDK recently upgraded to GCC v12.x which points out
that the `pos` variable might be used even after the corresponding
memory was `realloc()`ed and therefore potentially no longer valid.
Since a subset of this SDK is used in Git's CI/PR builds, we need to fix
this to continue to be able to benefit from the CI/PR runs.
Note: This bug has been with us since 2a6b149c64
(mingw: avoid using
strbuf in syslog, 2011-10-06), and while it looks tempting to replace
the hand-rolled string manipulation with a `strbuf`-based one, that
commit's message explains why we cannot do that: The `syslog()` function
is called as part of the function in `daemon.c` which is set as the
`die()` routine, and since `strbuf_grow()` can call that function if it
runs out of memory, this would cause a nasty infinite loop that we do
not want to re-introduce.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
83 lines
1.4 KiB
C
83 lines
1.4 KiB
C
#include "../../git-compat-util.h"
|
|
|
|
static HANDLE ms_eventlog;
|
|
|
|
void openlog(const char *ident, int logopt, int facility)
|
|
{
|
|
if (ms_eventlog)
|
|
return;
|
|
|
|
ms_eventlog = RegisterEventSourceA(NULL, ident);
|
|
|
|
if (!ms_eventlog)
|
|
warning("RegisterEventSource() failed: %lu", GetLastError());
|
|
}
|
|
|
|
void syslog(int priority, const char *fmt, ...)
|
|
{
|
|
WORD logtype;
|
|
char *str, *pos;
|
|
int str_len;
|
|
va_list ap;
|
|
|
|
if (!ms_eventlog)
|
|
return;
|
|
|
|
va_start(ap, fmt);
|
|
str_len = vsnprintf(NULL, 0, fmt, ap);
|
|
va_end(ap);
|
|
|
|
if (str_len < 0) {
|
|
warning_errno("vsnprintf failed");
|
|
return;
|
|
}
|
|
|
|
str = malloc(st_add(str_len, 1));
|
|
if (!str) {
|
|
warning_errno("malloc failed");
|
|
return;
|
|
}
|
|
|
|
va_start(ap, fmt);
|
|
vsnprintf(str, str_len + 1, fmt, ap);
|
|
va_end(ap);
|
|
|
|
while ((pos = strstr(str, "%1")) != NULL) {
|
|
size_t offset = pos - str;
|
|
char *oldstr = str;
|
|
str = realloc(str, st_add(++str_len, 1));
|
|
if (!str) {
|
|
free(oldstr);
|
|
warning_errno("realloc failed");
|
|
return;
|
|
}
|
|
pos = str + offset;
|
|
memmove(pos + 2, pos + 1, strlen(pos));
|
|
pos[1] = ' ';
|
|
}
|
|
|
|
switch (priority) {
|
|
case LOG_EMERG:
|
|
case LOG_ALERT:
|
|
case LOG_CRIT:
|
|
case LOG_ERR:
|
|
logtype = EVENTLOG_ERROR_TYPE;
|
|
break;
|
|
|
|
case LOG_WARNING:
|
|
logtype = EVENTLOG_WARNING_TYPE;
|
|
break;
|
|
|
|
case LOG_NOTICE:
|
|
case LOG_INFO:
|
|
case LOG_DEBUG:
|
|
default:
|
|
logtype = EVENTLOG_INFORMATION_TYPE;
|
|
break;
|
|
}
|
|
|
|
ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
|
|
(const char **)&str, NULL);
|
|
free(str);
|
|
}
|