ninja-build / ninja

a small build system with a focus on speed
https://ninja-build.org/
Apache License 2.0
11.27k stars 1.6k forks source link

util.h defines _snprintf as snprintf on windows #696

Open nico opened 10 years ago

nico commented 10 years ago

http://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/ :

// Make snprintf available on Windows:
// Don’t ever do this! These two functions are different!
#define snprintf _snprintf

(the former 0-terminates if the printed string is larger than the size, the latter doesn't.)

Audit calls to snprintf, check lack of 0-termination isn't an issue, then either have a portable wrapper around _snprintf on windows (that writes 0 in the last entry always), or something else.

evmar commented 10 years ago

I think I was avoiding making a std::string StringPrintf(...) function just out of laziness. I am pretty sure we're not using format strings in any place where perf critical where we care about extra allocs or whatever.

(My new favorite trick to use, instead of sprintf(buf, "%s:%d blah", x, y), is instead std::string Fmt(const char* fmt, Arg arg1=Arg::None, Arg arg2=Arg::None) which is used like Fmt("$:$ blah", x, y) and Arg has implicit ctors for the various types you care about. Not that Ninja needs this.)

nico commented 9 years ago

A simple fix might be to #define snprintf _snprintf_s instead (?)

evmar commented 9 years ago

We could just remove the one caller too:

https://github.com/martine/ninja/search?utf8=%E2%9C%93&q=strncpy

brevity due to phone On Dec 19, 2014 11:50 AM, "Nico Weber" notifications@github.com wrote:

A simple fix might be to #define snprintf _snprintf_s instead (?)

— Reply to this email directly or view it on GitHub https://github.com/martine/ninja/issues/696#issuecomment-67687893.