← Back to team overview

kicad-developers team mailing list archive

Re: [PATCH] correct some snprintf usage

 


On 2/23/2015 6:07 AM, Brian Sidebotham wrote:
> On 22 February 2015 at 03:00, Mark Roszko <mark.roszko@xxxxxxxxx> wrote:
>> Fixes some cases where n-1 bytes was used as a size but snprintf
>> protects against that internally. Also some hardcoded constant sizes
>> changed to sizeof()
> 
>> And made a case where sprintf was used into snprintf as the path input
>> as aUserPluginsPath could in theory be very stupid long if someone has
>> a very stupid filesystem. Would be best if there was error handling if
>> it exceeds ( snprintf will return greater than sizeof(buf) ) but I
>> have no clue how that section needs cleanup.
> 
> The MinGW compiler uses the Microsoft system version of snprintf which
> is not C99 compliant. I've attached a fix so that we can use snprintf
> as per the C99 spec as per your patch.
> 
> I can't commit it until I can do a test compile to check everything
> works okay with it. I'll have to wait until I'm home tonight. The
> proper use of snprintf is much more sane!
> 
> The MS version is a real pain, it returns -1 if the contents do not
> fit in the buffer and more importantly it does NOT guarantee to NULL
> terminate even if it returns a positive result!!
> 
> Best Regards,
> 
> Brian.
> 

Good catch Brian.  I knew there was a reason I used sizeof() - 1 and set
the last byte to null.  It's been a long time since I've looked at the
MS documentation for printf and friends.  It's hard to believe that bit
of information is still rattling around in my head.  We probably should
just use wxString::Format() and let it take care of the memory
management issues in places where performance isn't an issue.

Cheers,

Wayne


References