Why Is Escaping Data Considered So Magical?

Kushal Kumaran kushal.kumaran+python at gmail.com
Sun Jun 27 04:15:40 EDT 2010


On Sun, Jun 27, 2010 at 9:47 AM, Lawrence D'Oliveiro
<ldo at geek-central.gen.new_zealand> wrote:
> In message <roy-854954.20435125062010 at news.panix.com>, Roy Smith wrote:
>
>> I recently fixed a bug in some production code.  The programmer was
>> careful to use snprintf() to avoid buffer overflows.  The only problem
>> is, he wrote something along the lines of:
>>
>> snprintf(buf, strlen(foo), foo);
>
> A long while ago I came up with this macro:
>
>    #define Descr(v) &v, sizeof v
>
> making the correct version of the above become
>
>    snprintf(Descr(buf), foo);
>

Not quite right.  If buf is a char array, as suggested by the use of
sizeof, then you're not passing a char* to snprintf.  You need to lose
the & in your macro.

-- 
regards,
kushal



More information about the Python-list mailing list