Skip to contentSkip to conversation

mingw: avoid using strbuf in syslog

PATCH In review Erik Faye-Lund 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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.

---
Fixes the problem Brandon pointed out...

 compat/win32/syslog.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)
compat/win32/syslog.c +14 -12
index 42b95a9..3b8e2b7 100644
@@ -1,5 +1,4 @@
 #include "../../git-compat-util.h"
-#include "../../strbuf.h"
 
 static HANDLE ms_eventlog;
 
@@ -16,13 +15,8 @@ void openlog(const char *ident, int logopt, int facility)
 
 void syslog(int priority, const char *fmt, ...)
 {
-	struct strbuf sb = STRBUF_INIT;
-	struct strbuf_expand_dict_entry dict[] = {
-		{"1", "% 1"},
-		{NULL, NULL}
-	};
 	WORD logtype;
-	char *str;
+	char *str, *pos;
 	int str_len;
 	va_list ap;
 
@@ -39,11 +33,20 @@ void syslog(int priority, const char *fmt, ...)
 	}
 
 	str = malloc(str_len + 1);
+	if (!str)
+		return; /* no chance to report error */
+
 	va_start(ap, fmt);
 	vsnprintf(str, str_len + 1, fmt, ap);
 	va_end(ap);
-	strbuf_expand(&sb, str, strbuf_expand_dict_cb, &dict);
-	free(str);
+
+	while ((pos = strstr(str, "%1")) != NULL) {
+		str = realloc(str, ++str_len + 1);
+		if (!str)
+			return;
+		memmove(pos + 2, pos + 1, strlen(pos));
+		pos[1] = ' ';
+	}
 
 	switch (priority) {
 	case LOG_EMERG:
@@ -66,7 +69,6 @@ void syslog(int priority, const char *fmt, ...)
 	}
 
 	ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
-	    (const char **)&sb.buf, NULL);
-
-	strbuf_release(&sb);
+	    (const char **)&str, NULL);
+	free(str);
 }
Signature · 1 line
1.7.6.msysgit.0.579.ga3d6f
#1 lore raw
Quoted 7 lines · Erik Faye-Lund

strbuf can call die, which again can call syslog from git-daemon.

Endless recursion is no fun; fix it by hand-rolling the logic.

---
Fixes the problem Brandon pointed out...

Actually, Johannes should get that credit. He brought up the whole recursion issue.

Quoted 29 lines

 compat/win32/syslog.c |   26 ++++++++++++++------------

 1 files changed, 14 insertions(+), 12 deletions(-)
compat/win32/syslog.c +1 -7
index 42b95a9..3b8e2b7 100644
@@ -1,5 +1,4 @@
  #include "../../git-compat-util.h"
-#include "../../strbuf.h"
 
  static HANDLE ms_eventlog;
 
@@ -16,13 +15,8 @@ void openlog(const char *ident, int logopt, int facility)
 
  void syslog(int priority, const char *fmt, ...)
  {
-       struct strbuf sb = STRBUF_INIT;
-       struct strbuf_expand_dict_entry dict[] = {
-               {"1", "% 1"},
-               {NULL, NULL}
-       };
        WORD logtype;
-       char *str;
+       char *str, *pos;
        int str_len;
        va_list ap;
 

compat/win32/syslog.c void syslog(int priority, const char *fmt, ...) · line 38

Quoted 6 lines
compat/win32/syslog.c +3 -0
@@ -39,11 +33,20 @@ void syslog(int priority, const char *fmt, ...)
        }
 
        str = malloc(str_len + 1);
+       if (!str)
+               return; /* no chance to report error */
+

Just above this, warning() is used to at least print a message to stderr if vsnprintf() fails. It could be used here, and below when realloc fails.

Quoted 15 lines
compat/win32/syslog.c +8 -2
        va_start(ap, fmt);
        vsnprintf(str, str_len + 1, fmt, ap);
        va_end(ap);
-       strbuf_expand(&sb, str, strbuf_expand_dict_cb, &dict);
-       free(str);
+
+       while ((pos = strstr(str, "%1")) != NULL) {
+               str = realloc(str, ++str_len + 1);
+               if (!str)
+                       return;
+               memmove(pos + 2, pos + 1, strlen(pos));
+               pos[1] = ' ';
+       }
 
        switch (priority) {
Quoted 1 line

       case LOG_EMERG:

-Brandon

#2 lore raw
Quoted 12 lines · Brandon Casey
Quoted 7 lines · Erik Faye-Lund

strbuf can call die, which again can call syslog from git-daemon.

Endless recursion is no fun; fix it by hand-rolling the logic.

---
Fixes the problem Brandon pointed out...

Actually, Johannes should get that credit.  He brought up the whole recursion issue.

OK, I didn't spot that one. But it's not in the commit message, though. Perhaps it should be?

Quoted 31 lines
Quoted 31 lines

 compat/win32/syslog.c |   26 ++++++++++++++------------

 1 files changed, 14 insertions(+), 12 deletions(-)
compat/win32/syslog.c +1 -7
index 42b95a9..3b8e2b7 100644
@@ -1,5 +1,4 @@
  #include "../../git-compat-util.h"
-#include "../../strbuf.h"
 
  static HANDLE ms_eventlog;
 
@@ -16,13 +15,8 @@ void openlog(const char *ident, int logopt, int facility)
 
  void syslog(int priority, const char *fmt, ...)
  {
-       struct strbuf sb = STRBUF_INIT;
-       struct strbuf_expand_dict_entry dict[] = {
-               {"1", "% 1"},
-               {NULL, NULL}
-       };
        WORD logtype;
-       char *str;
+       char *str, *pos;
        int str_len;
        va_list ap;
 
@@ -39,11 +33,20 @@ void syslog(int priority, const char *fmt, ...)
        }
 
Quoted 9 lines

compat/win32/syslog.c void syslog(int priority, const char *fmt, ...) · line 38

Quoted 4 lines
compat/win32/syslog.c +3 -0
        str = malloc(str_len + 1);
+       if (!str)
+               return; /* no chance to report error */
+

Just above this, warning() is used to at least print a message to stderr if vsnprintf() fails.  It could be used here, and below when realloc fails.

Good point, so this should be squashed in:
---

compat/win32/syslog.c +7 -3
index 3b8e2b7..d015e43 100644
@@ -33,8 +33,10 @@ void syslog(int priority, const char *fmt, ...)
 	}
 
 	str = malloc(str_len + 1);
-	if (!str)
-		return; /* no chance to report error */
+	if (!str) {
+		warning("malloc failed: '%s'", strerror(errno));
+		return;
+	}
 
 	va_start(ap, fmt);
 	vsnprintf(str, str_len + 1, fmt, ap);
@@ -42,8 +44,10 @@ void syslog(int priority, const char *fmt, ...)
 
 	while ((pos = strstr(str, "%1")) != NULL) {
 		str = realloc(str, ++str_len + 1);
-		if (!str)
+		if (!str) {
+			warning("realloc failed: '%s'", strerror(errno));
 			return;
+		}
 		memmove(pos + 2, pos + 1, strlen(pos));
 		pos[1] = ' ';
 	}