From 3a731dae438f5b00e4f71a6de697f4bd9a0b2ca6 Mon Sep 17 00:00:00 2001 From: Karl Skomski Date: Thu, 3 Sep 2015 10:10:30 +0200 Subject: [PATCH] src: use standard conform snprintf on windows The version used before returned -1 on truncation which does not conform to the standard. PR-URL: https://github.com/nodejs/node/pull/2404 Reviewed-By: Trevor Norris Reviewed-By: Fedor Indutny --- src/node_internals.h | 18 ++++++++++-------- test/fixtures/throws_error6.js | 1 + test/parallel/test-error-reporting.js | 9 +++++++-- 3 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 test/fixtures/throws_error6.js diff --git a/src/node_internals.h b/src/node_internals.h index 1fb58c9274e074..f4d8c1b78d1e81 100644 --- a/src/node_internals.h +++ b/src/node_internals.h @@ -95,17 +95,19 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo& args) { #ifdef _WIN32 // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer // on overflow... +// VS 2015 added a standard conform snprintf +#if defined( _MSC_VER ) && (_MSC_VER < 1900) #include -inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) { - va_list ap; - va_start(ap, fmt); - int n = _vsprintf_p(buf, len, fmt, ap); - if (len) - buf[len - 1] = '\0'; - va_end(ap); - return n; +inline static int snprintf(char *buffer, size_t n, const char *format, ...) { + va_list argp; + va_start(argp, format); + int ret = _vscprintf(format, argp); + vsnprintf_s(buffer, n, _TRUNCATE, format, argp); + va_end(argp); + return ret; } #endif +#endif #if defined(__x86_64__) # define BITS_PER_LONG 64 diff --git a/test/fixtures/throws_error6.js b/test/fixtures/throws_error6.js new file mode 100644 index 00000000000000..8e650d4bf4f413 --- /dev/null +++ b/test/fixtures/throws_error6.js @@ -0,0 +1 @@ +00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000k000000000 diff --git a/test/parallel/test-error-reporting.js b/test/parallel/test-error-reporting.js index cea60cf90f907e..88ef5d306e3e37 100644 --- a/test/parallel/test-error-reporting.js +++ b/test/parallel/test-error-reporting.js @@ -54,11 +54,16 @@ errExec('throws_error4.js', function(err, stdout, stderr) { assert.ok(/SyntaxError/.test(stderr)); }); -// Long exception line doesn't result in stack overflow +// Specific long exception line doesn't result in stack overflow errExec('throws_error5.js', function(err, stdout, stderr) { assert.ok(/SyntaxError/.test(stderr)); }); +// Long exception line with length > errorBuffer doesn't result in assertion +errExec('throws_error6.js', function(err, stdout, stderr) { + assert.ok(/SyntaxError/.test(stderr)); +}); + process.on('exit', function() { - assert.equal(5, exits); + assert.equal(6, exits); });