ngx_snprintf question

Mike Gagnon mikegagnon at gmail.com
Tue Apr 10 15:00:39 UTC 2012


Good to know; thanks!

Mike Gagnon
On Apr 10, 2012 7:54 AM, "Piotr Sikora" <piotr.sikora at frickle.com> wrote:

> Hi,
>
>  I am getting unexpected results from ngx_snprintf in the following
>> statement:
>>
>>        ngx_snprintf((u_char *) buf, sizeof(buf), "%s\n",
>> peer->peer_config->name.data);
>>
>> with peer->peer_config->name.data == "127.0.0.1:9000\0".
>>
>> I expect ngx_snprintf to yield buf == "127.0.0.1:9000\n\0", but
>> instead it yields buf == "127.0.0.1:9000\n\bf\0" (I was not expecting
>> that '\bf' character to be inserted there.)
>>
>> When I change ngx_snprintf to just snprintf, it works as expected.
>>
>
> ngx_snprintf isn't snprintf-equivalent (I was burned by this once myself),
> it doesn't add trailing '\0' and the "\bf\0" you're seeing is just the data
> that was in the buffer before.
>
> You should either add "%Z" ('\0') to the format string or explicitly set
> '\0' yourself after ngx_snprintf() call.
>
> Best regards,
> Piotr Sikora < piotr.sikora at frickle.com >
>
> ______________________________**_________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/**mailman/listinfo/nginx-devel<http://mailman.nginx.org/mailman/listinfo/nginx-devel>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20120410/eb7e71cd/attachment.html>


More information about the nginx-devel mailing list