* [PATCH] mingw: avoid using strbuf in syslog
@ 2011-10-06 16:57 Erik Faye-Lund
2011-10-06 17:27 ` Brandon Casey
0 siblings, 1 reply; 3+ messages in thread
From: Erik Faye-Lund @ 2011-10-06 16:57 UTC (permalink / raw)
To: git; +Cc: drafnel, j.sixt, gitster
strbuf can call die, which again can call syslog from git-daemon.
Endless recursion is no fun; fix it by hand-rolling the logic.
Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
---
Fixes the problem Brandon pointed out...
compat/win32/syslog.c | 26 ++++++++++++++------------
1 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
index 42b95a9..3b8e2b7 100644
--- a/compat/win32/syslog.c
+++ b/compat/win32/syslog.c
@@ -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);
}
--
1.7.6.msysgit.0.579.ga3d6f
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] mingw: avoid using strbuf in syslog
2011-10-06 16:57 [PATCH] mingw: avoid using strbuf in syslog Erik Faye-Lund
@ 2011-10-06 17:27 ` Brandon Casey
2011-10-06 17:32 ` Erik Faye-Lund
0 siblings, 1 reply; 3+ messages in thread
From: Brandon Casey @ 2011-10-06 17:27 UTC (permalink / raw)
To: Erik Faye-Lund; +Cc: git, j.sixt, gitster
On Thu, Oct 6, 2011 at 11:57 AM, Erik Faye-Lund <kusmabite@gmail.com> wrote:
> strbuf can call die, which again can call syslog from git-daemon.
>
> Endless recursion is no fun; fix it by hand-rolling the logic.
>
> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
> ---
> Fixes the problem Brandon pointed out...
Actually, Johannes should get that credit. He brought up the whole
recursion issue.
> compat/win32/syslog.c | 26 ++++++++++++++------------
> 1 files changed, 14 insertions(+), 12 deletions(-)
>
> diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
> index 42b95a9..3b8e2b7 100644
> --- a/compat/win32/syslog.c
> +++ b/compat/win32/syslog.c
> @@ -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 */
> +
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.
> 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:
-Brandon
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH] mingw: avoid using strbuf in syslog
2011-10-06 17:27 ` Brandon Casey
@ 2011-10-06 17:32 ` Erik Faye-Lund
0 siblings, 0 replies; 3+ messages in thread
From: Erik Faye-Lund @ 2011-10-06 17:32 UTC (permalink / raw)
To: Brandon Casey; +Cc: git, j.sixt, gitster
On Thu, Oct 6, 2011 at 7:27 PM, Brandon Casey <drafnel@gmail.com> wrote:
> On Thu, Oct 6, 2011 at 11:57 AM, Erik Faye-Lund <kusmabite@gmail.com> wrote:
>> strbuf can call die, which again can call syslog from git-daemon.
>>
>> Endless recursion is no fun; fix it by hand-rolling the logic.
>>
>> Signed-off-by: Erik Faye-Lund <kusmabite@gmail.com>
>> ---
>> 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?
>> compat/win32/syslog.c | 26 ++++++++++++++------------
>> 1 files changed, 14 insertions(+), 12 deletions(-)
>>
>> diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
>> index 42b95a9..3b8e2b7 100644
>> --- a/compat/win32/syslog.c
>> +++ b/compat/win32/syslog.c
>> @@ -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 */
>> +
>
> 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:
---
diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
index 3b8e2b7..d015e43 100644
--- a/compat/win32/syslog.c
+++ b/compat/win32/syslog.c
@@ -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] = ' ';
}
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-10-06 17:33 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-06 16:57 [PATCH] mingw: avoid using strbuf in syslog Erik Faye-Lund
2011-10-06 17:27 ` Brandon Casey
2011-10-06 17:32 ` Erik Faye-Lund
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox