diff options
author | Ben Noordhuis <info@bnoordhuis.nl> | 2013-04-15 21:05:20 +0200 |
---|---|---|
committer | Ben Noordhuis <info@bnoordhuis.nl> | 2013-04-15 21:13:29 +0200 |
commit | afbaddecd37a77d9304f55e440bc6c741516b580 (patch) | |
tree | 62e6221efb99a59edec1475c3d29987f94742925 | |
parent | 78c5de598bb6ebd68d8d93fabdcebdca1f024580 (diff) | |
download | node-afbaddecd37a77d9304f55e440bc6c741516b580.tar.gz |
os: handle 256 character hostnames
Fix a (rather academic) buffer overflow. MAXHOSTNAMELEN is 256 on most
platforms, which means the buffer wasn't big enough to hold the
trailing nul byte on a system with a maximum length hostname.
-rw-r--r-- | src/node_os.cc | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/src/node_os.cc b/src/node_os.cc index 3047d0744..d4399756b 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -33,7 +33,9 @@ #endif #ifdef __POSIX__ -# include <unistd.h> // gethostname, sysconf +# include <netdb.h> // MAXHOSTNAMELEN on Solaris. +# include <unistd.h> // gethostname, sysconf +# include <sys/param.h> // MAXHOSTNAMELEN on Linux and the BSDs. # include <sys/utsname.h> #endif @@ -51,10 +53,9 @@ static Handle<Value> GetEndianness(const Arguments& args) { static Handle<Value> GetHostname(const Arguments& args) { HandleScope scope; - char s[255]; - int r = gethostname(s, 255); + char buf[MAXHOSTNAMELEN + 1]; - if (r < 0) { + if (gethostname(buf, sizeof(buf))) { #ifdef __POSIX__ return ThrowException(ErrnoException(errno, "gethostname")); #else // __MINGW32__ @@ -62,7 +63,7 @@ static Handle<Value> GetHostname(const Arguments& args) { #endif // __MINGW32__ } - return scope.Close(String::New(s)); + return scope.Close(String::New(buf)); } static Handle<Value> GetOSType(const Arguments& args) { |