netfilter-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [libnftnl PATCH] src: improve printing of XML/JSON event wrapper header/footer
Date: Thu, 24 Jul 2014 13:18:07 +0200	[thread overview]
Message-ID: <20140724111807.GA20917@salvia> (raw)
In-Reply-To: <20140718110806.29103.18161.stgit@nfdev.cica.es>

Hi Arturo,

On Fri, Jul 18, 2014 at 01:08:06PM +0200, Arturo Borrero Gonzalez wrote:
> Every snprintf() call is warranteed to put the trailing \0 character, unless
> the output was truncated, ie ret == bufsiz. In case of output truncated, the
> caller must reallocate a buffer and start again.
> This \0 character is used by other functions like fputs() and fprintf() to
> know where a string ends, so they avoid adding garbage to the output.
> 
> The XML/JSON event wrapper header/footer is printed for each object, meaning
> that the header is the first thing to be written to the buffer and the footer
> the last; all objects share this code path.
> 
> A new helper function nft_fprintf2() is added because:
>  * we need to handle the needed buffer resizing.
>  * so, we can ignore about what string the header/footer are composed of.
>  * nft_fprint() requires an object; the signature of the function differs.
> 
> There were two cases we weren't considering in the header/footer fprintf
> functions: if the nft_event_[head|foot]er_snprintf() call returned 0 and/or if
> returned < 0. Previous to this patch, we unconditionally do a fprintf().
> 
> Now with this patch, we avoid doing fprintf() if the inner snprintf() call:
>  * failed (ret < 0)
>  * didn't print nothing (ret == 0) to the buffer (even a \0 character)
> 
> Also, we were unconditionally adding a \0 character at buf[sizeof(buf) - 1],
> meaning that if the inner snprintf() returned 0, we get sizeof(buf) - 2
> characters of unknown value.
> 
> A side effect of this patch is that we avoid 2 syscall (2 fprintf() calls) per
> nft object to be printed if the header/footer is empty (ie, no events flags
> set).
> 
> Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
> ---
>  src/common.c   |   14 ++------------
>  src/internal.h |    3 +++
>  src/utils.c    |   33 +++++++++++++++++++++++++++++++++
>  3 files changed, 38 insertions(+), 12 deletions(-)
> 
> diff --git a/src/common.c b/src/common.c
> index 1b600f1..2ebb7d1 100644
> --- a/src/common.c
> +++ b/src/common.c
> @@ -113,12 +113,7 @@ int nft_event_header_snprintf(char *buf, size_t size, uint32_t type,
>  
>  int nft_event_header_fprintf(FILE *fp, uint32_t type, uint32_t flags)
>  {
> -	char buf[64]; /* enough for the maximum string length above */
> -
> -	nft_event_header_snprintf(buf, sizeof(buf), type, flags);
> -	buf[sizeof(buf) - 1] = '\0';
> -
> -	return fprintf(fp, "%s", buf);
> +	return nft_fprintf2(fp, type, flags, nft_event_header_snprintf);
>  }
>  
>  int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type,
> @@ -139,10 +134,5 @@ int nft_event_footer_snprintf(char *buf, size_t size, uint32_t type,
>  
>  int nft_event_footer_fprintf(FILE *fp, uint32_t type, uint32_t flags)
>  {
> -	char buf[32]; /* enough for the maximum string length above */
> -
> -	nft_event_footer_snprintf(buf, sizeof(buf), type, flags);
> -	buf[sizeof(buf) - 1] = '\0';
> -
> -	return fprintf(fp, "%s", buf);
> +	return nft_fprintf2(fp, type, flags, nft_event_footer_snprintf);
>  }
> diff --git a/src/internal.h b/src/internal.h
> index b8ed616..04c3ef9 100644
> --- a/src/internal.h
> +++ b/src/internal.h
> @@ -136,6 +136,9 @@ int nft_get_value(enum nft_type type, void *val, void *out);
>  
>  #include <stdio.h>
>  int nft_fprintf(FILE *fp, void *obj, uint32_t type, uint32_t flags, int (*snprintf_cb)(char *buf, size_t bufsiz, void *obj, uint32_t type, uint32_t flags));
> +int nft_fprintf2(FILE *fp, uint32_t type, uint32_t flags,
> +		 int (*snprintf_cb)(char *buf, size_t bufsiz, uint32_t type,
> +				    uint32_t flags));
>  int nft_event_header_snprintf(char *buf, size_t bufsize,
>  			      uint32_t format, uint32_t flags);
>  int nft_event_header_fprintf(FILE *fp, uint32_t format, uint32_t flags);
> diff --git a/src/utils.c b/src/utils.c
> index 20a2fa3..94268c3 100644
> --- a/src/utils.c
> +++ b/src/utils.c
> @@ -219,6 +219,39 @@ out:
>  	return ret;
>  }
>  
> +int nft_fprintf2(FILE *fp, uint32_t type, uint32_t flags,
> +		 int (*snprintf_cb)(char *buf, size_t bufsiz, uint32_t type,
> +				    uint32_t flags))
> +{
> +	char _buf[NFT_SNPRINTF_BUFSIZ];
> +	char *buf = _buf;
> +	size_t bufsiz = sizeof(_buf);
> +	int ret;
> +
> +	ret = snprintf_cb(buf, bufsiz, type, flags);
> +	if (ret <= 0)
> +		goto out;
> +
> +	if (ret >= NFT_SNPRINTF_BUFSIZ) {
> +		bufsiz = ret + 1;
> +
> +		buf = malloc(bufsiz);
> +		if (buf == NULL)
> +			return -1;
> +
> +		ret = snprintf_cb(buf, bufsiz, type, flags);
> +		if (ret <= 0)
> +			goto out;
> +	}
> +
> +	ret = fprintf(fp, "%s", buf);
> +out:
> +	if (buf != _buf)
> +		xfree(buf);
> +
> +	return ret;
> +}
> +

This function looks almost exactly the same like nft_fprintf. We can
save line of code with some initial code refactorization.

So please, let us have one single nft_fprintf function. Hint: You
can generalise the snprintf_cb callback, ie.

        int (*snprintf_cb)(char *buf, size_t bufsiz, uint32_t type,
                           uint32_t flags, void *data);

The nft_fprintf should look like:

int nft_fprintf(FILE *fp, uint32_t type, uint32_t flags, void *data, ...)

The data pointer is generic and it is interpreted by the callback
function.

      reply	other threads:[~2014-07-24 11:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-18 11:08 [libnftnl PATCH] src: improve printing of XML/JSON event wrapper header/footer Arturo Borrero Gonzalez
2014-07-24 11:18 ` Pablo Neira Ayuso [this message]

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=20140724111807.GA20917@salvia \
    --to=pablo@netfilter.org \
    --cc=arturo.borrero.glez@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    /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).