git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Erik Faye-Lund <kusmabite@gmail.com>
To: Brandon Casey <drafnel@gmail.com>
Cc: Johannes Sixt <j.sixt@viscovery.net>,
	"peff@peff.net" <peff@peff.net>,
	"git@vger.kernel.org" <git@vger.kernel.org>,
	"gitster@pobox.com" <gitster@pobox.com>,
	"sunshine@sunshineco.com" <sunshine@sunshineco.com>,
	"bharrosh@panasas.com" <bharrosh@panasas.com>,
	"trast@student.ethz.ch" <trast@student.ethz.ch>
Subject: Re: [PATCH 2/4] cleanup: use internal memory allocation wrapper functions everywhere
Date: Thu, 6 Oct 2011 18:52:43 +0200	[thread overview]
Message-ID: <CABPQNSaQFFj0vP1+wuoL2=GyHZPRgCXLxXqD+5WX3Le_PyuH4w@mail.gmail.com> (raw)
In-Reply-To: <CABPQNSak_jDbNQhzMoSN=NdWmyby3xJRwED54Ck5H1Y12HoGCQ@mail.gmail.com>

On Thu, Oct 6, 2011 at 6:50 PM, Erik Faye-Lund <kusmabite@gmail.com> wrote:
> On Thu, Oct 6, 2011 at 6:14 PM, Brandon Casey <drafnel@gmail.com> wrote:
>> Ah.  Yes, you're right.  x-wrappers should not be used in syslog.c and
>> the use of strbuf's should be replaced.
>
> Good point. The patch for this looks something like this:
>
> diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
> index 42b95a9..243538c 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,5 @@ 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);
>  }
>

...aaand this on top to avoid a leak, of course:

diff --git a/compat/win32/syslog.c b/compat/win32/syslog.c
index 243538c..3b8e2b7 100644
--- a/compat/win32/syslog.c
+++ b/compat/win32/syslog.c
@@ -70,4 +70,5 @@ void syslog(int priority, const char *fmt, ...)

 	ReportEventA(ms_eventlog, logtype, 0, 0, NULL, 1, 0,
 	    (const char **)&str, NULL);
+	free(str);
 }

  reply	other threads:[~2011-10-06 16:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <5XXEFw0WjtXKd9dpXSxpkskCcgVyG9Db1_zzVSEBNey-kpXSBbmQfYaxZ2Szg6Pbck6hZZTQ5hHzBwG4rhKYXshrdmveEFLPZ9W0V8P_lw@cipher.nrlssc.navy.mil>
2011-09-15  1:59 ` [PATCH 0/4] Honor core.ignorecase for attribute patterns Brandon Casey
2011-09-15  1:59   ` [PATCH 1/4] attr.c: avoid inappropriate access to strbuf "buf" member Brandon Casey
2011-09-15  1:59   ` [PATCH 2/4] cleanup: use internal memory allocation wrapper functions everywhere Brandon Casey
2011-09-15  6:52     ` Johannes Sixt
2011-09-15 15:39       ` Brandon Casey
     [not found]         ` <CA+sFfMf73K3yv_5K633DKOsVufMV6rTjd+SSunq4sBikt4jCsg@mail.gmail.com>
2011-10-06  2:00           ` Brandon Casey
2011-10-06  6:17             ` Johannes Sixt
2011-10-06  7:01               ` Alexey Shumkin
2011-10-06 16:14               ` Brandon Casey
2011-10-06 16:50                 ` Erik Faye-Lund
2011-10-06 16:52                   ` Erik Faye-Lund [this message]
2011-10-06 17:17                   ` Brandon Casey
2011-09-15  1:59   ` [PATCH 3/4] builtin/mv.c: plug miniscule memory leak Brandon Casey
2011-09-15  1:59   ` [PATCH 4/4] attr.c: respect core.ignorecase when matching attribute patterns Brandon Casey
2011-09-15  4:01     ` Junio C Hamano
2011-09-15  4:06       ` Junio C Hamano
2011-09-15 15:38         ` Brandon Casey
2011-09-15 18:12   ` [PATCH 0/4] Honor core.ignorecase for " Jeff King
2011-09-15 20:28     ` Brandon Casey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABPQNSaQFFj0vP1+wuoL2=GyHZPRgCXLxXqD+5WX3Le_PyuH4w@mail.gmail.com' \
    --to=kusmabite@gmail.com \
    --cc=bharrosh@panasas.com \
    --cc=drafnel@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=j.sixt@viscovery.net \
    --cc=peff@peff.net \
    --cc=sunshine@sunshineco.com \
    --cc=trast@student.ethz.ch \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).