Skip to content

Commit a74ae76

Browse files
skomskirvagg
authored andcommitted
src: use standard conform snprintf on windows
The version used before returned -1 on truncation which does not conform to the standard. PR-URL: #2404 Reviewed-By: Trevor Norris <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent f4be258 commit a74ae76

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

src/node_internals.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,19 @@ void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
9595
#ifdef _WIN32
9696
// emulate snprintf() on windows, _snprintf() doesn't zero-terminate the buffer
9797
// on overflow...
98+
// VS 2015 added a standard conform snprintf
99+
#if defined( _MSC_VER ) && (_MSC_VER < 1900)
98100
#include <stdarg.h>
99-
inline static int snprintf(char* buf, unsigned int len, const char* fmt, ...) {
100-
va_list ap;
101-
va_start(ap, fmt);
102-
int n = _vsprintf_p(buf, len, fmt, ap);
103-
if (len)
104-
buf[len - 1] = '\0';
105-
va_end(ap);
106-
return n;
101+
inline static int snprintf(char *buffer, size_t n, const char *format, ...) {
102+
va_list argp;
103+
va_start(argp, format);
104+
int ret = _vscprintf(format, argp);
105+
vsnprintf_s(buffer, n, _TRUNCATE, format, argp);
106+
va_end(argp);
107+
return ret;
107108
}
108109
#endif
110+
#endif
109111

110112
#if defined(__x86_64__)
111113
# define BITS_PER_LONG 64

test/fixtures/throws_error6.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/parallel/test-error-reporting.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,16 @@ errExec('throws_error4.js', function(err, stdout, stderr) {
5454
assert.ok(/SyntaxError/.test(stderr));
5555
});
5656

57-
// Long exception line doesn't result in stack overflow
57+
// Specific long exception line doesn't result in stack overflow
5858
errExec('throws_error5.js', function(err, stdout, stderr) {
5959
assert.ok(/SyntaxError/.test(stderr));
6060
});
6161

62+
// Long exception line with length > errorBuffer doesn't result in assertion
63+
errExec('throws_error6.js', function(err, stdout, stderr) {
64+
assert.ok(/SyntaxError/.test(stderr));
65+
});
66+
6267
process.on('exit', function() {
63-
assert.equal(5, exits);
68+
assert.equal(6, exits);
6469
});

0 commit comments

Comments
 (0)