Skip to content

Commit

Permalink
src: use standard conform snprintf on windows
Browse files Browse the repository at this point in the history
The version used before returned -1 on truncation which does not conform
to the standard.

PR-URL: #2404
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
skomski authored and rvagg committed Sep 6, 2015
1 parent b028978 commit ca6c322
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/node_internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,19 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& 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 <stdarg.h>
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
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/throws_error6.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions test/parallel/test-error-reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

0 comments on commit ca6c322

Please sign in to comment.