Re: [libnftnl PATCH] utils: fix buffer reallocation of nft_fprinft()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, May 13, 2014 at 11:11:24AM +0200, Arturo Borrero Gonzalez wrote:
> On 12 May 2014 17:54, Pablo Neira Ayuso <pablo@xxxxxxxxxxxxx> 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.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux