From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Cc: Netfilter Development Mailing list <netfilter-devel@vger.kernel.org>
Subject: Re: [libnftnl PATCH] utils: fix buffer reallocation of nft_fprinft()
Date: Tue, 13 May 2014 14:49:10 +0200 [thread overview]
Message-ID: <20140513124910.GA3784@localhost> (raw)
In-Reply-To: <CAOkSjBjpMa4uipdXx8wzgLEDf8PcH_nXXZUNRJrjBkzSJQD-MQ@mail.gmail.com>
On Tue, May 13, 2014 at 11:11:24AM +0200, Arturo Borrero Gonzalez wrote:
> On 12 May 2014 17:54, Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> > You can use malloc instead. Just make sure that the string is always
> > nul-terminated before printing, something like:
> >
> > bufsiz = ret + 1;
> > buf = malloc(1, bufsiz);
> > if (buf == NULL)
> > return -1;
> >
> > ret = snprintf(...
> > if (ret < 0)
> > ...
> > }
> >
> > buf[ret] = '\0';
> > ... = fprintf(...
> >
>
> From my man pages, I understand that snprintf() null-terminate the
> string. This seems a bit redundant.
If the amount of written bytes is smaller than the buffer size,
snprintf always nul-terminate it. But if the amount of bytes returned
equals the buffer size, then you have to explicitly nul-terminate the
string.
> I think it's safe to don't include calloc() neither buf[ret] = '\0'.
Right, if you use a buffer size of ret + 1, then you can guarantee
that snprintf always have room to append the nul-termination, as the
returned value will be ret at maximum.
> I'm resending this patch with this assumption and your other request.
OK, thanks.
prev parent reply other threads:[~2014-05-13 13:03 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-09 16:45 [libnftnl PATCH] utils: fix buffer reallocation of nft_fprinft() Arturo Borrero Gonzalez
2014-05-12 15:54 ` Pablo Neira Ayuso
2014-05-13 9:11 ` Arturo Borrero Gonzalez
2014-05-13 12:49 ` 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=20140513124910.GA3784@localhost \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.