Browse Source

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 <trev.norris@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
v5.x
Karl Skomski 9 years ago
committed by Rod Vagg
parent
commit
98608774a7
  1. 18
      src/node_internals.h
  2. 1
      test/fixtures/throws_error6.js
  3. 9
      test/parallel/test-error-reporting.js

18
src/node_internals.h

@ -95,17 +95,19 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
#ifdef _WIN32 #ifdef _WIN32
// emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer // emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
// on overflow... // on overflow...
// VS 2015 added a standard conform snprintf
#if defined( _MSC_VER ) && (_MSC_VER < 1900)
#include <stdarg.h> #include <stdarg.h>
inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) { inline static int snprintf(char *buffer, size_t n, const char *format, ...) {
va_list ap; va_list argp;
va_start(ap, fmt); va_start(argp, format);
int n = _vsprintf_p(buf, len, fmt, ap); int ret = _vscprintf(format, argp);
if (len) vsnprintf_s(buffer, n, _TRUNCATE, format, argp);
buf[len - 1] = '\0'; va_end(argp);
va_end(ap); return ret;
return n;
} }
#endif #endif
#endif
#if defined(__x86_64__) #if defined(__x86_64__)
# define BITS_PER_LONG 64 # define BITS_PER_LONG 64

1
test/fixtures/throws_error6.js

@ -0,0 +1 @@
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000k000000000

9
test/parallel/test-error-reporting.js

@ -54,11 +54,16 @@ errExec('throws_error4.js', function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(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) { errExec('throws_error5.js', function(err, stdout, stderr) {
assert.ok(/SyntaxError/.test(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() { process.on('exit', function() {
assert.equal(5, exits); assert.equal(6, exits);
}); });

Loading…
Cancel
Save