[lug] An unusual C construct

Bear Giles bgiles at coyotesong.com
Tue Sep 21 20:54:26 MDT 2010


On 9/21/2010 8:39 PM, Anthony Foiani wrote:
> I've used it all the time with things like
>>        sprintf(sizeof(tbl->name), "%s", tbl->name);
>>      
> And just in case someone runs across this in the archive, that looks
> like an odd call to 'sprintf'.  Maybe just:
>
>      printf( "sizeof( %s ) = %u\n", "tblp->name", sizeof( tblp->name ) );
>
> Anyway.
>
> Happy hacking,
> t.
>    

Hmm, serious brainfart there.  The point is to use snprintf(), not 
sprintf(), where the second argument is the size of the buffer you're 
writing into.  That prevents buffer overflows.  You should still 
explicitly set the last char to \0 since the function doesn't.



More information about the LUG mailing list