git-commit-vandalism/compat/win32/syslog.c
Derrick Stolee b4f52f09ae compat/win32: correct for incorrect compiler warning
The 'win build' job of our CI build is failing with the following error:

compat/win32/syslog.c: In function 'syslog':
compat/win32/syslog.c:53:17: error: pointer 'pos' may be used after \
				    'realloc' [-Werror=use-after-free]
   53 |                 memmove(pos + 2, pos + 1, strlen(pos));
    CC compat/poll/poll.o
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compat/win32/syslog.c:47:23: note: call to 'realloc' here
   47 |                 str = realloc(str, st_add(++str_len, 1));
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

However, between this realloc() and the use we have a line that resets
the value of 'pos'. Thus, this error is incorrect. It is likely due to a
new version of the compiler on the CI machines.

Instead of waiting for a new compiler, create a new variable to avoid
this error.

Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-07-19 12:51:34 -07:00

84 lines
1.5 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 *new_pos;
char *oldstr = str;
str = realloc(str, st_add(++str_len, 1));
if (!str) {
free(oldstr);
warning_errno("realloc failed");
return;
}
new_pos = str + offset;
memmove(new_pos + 2, new_pos + 1, strlen(new_pos));
new_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);
}