Skip to content
This repository
Browse code

net: fix net.Server.listen({fd:x}) error reporting

* don't assert when fd isn't an open file descriptor

* don't die with a ReferenceError when fd isn't a file descriptor
  you can listen() on

Fixes #3699.
  • Loading branch information...
commit 3a6314dbe159c40aa4cd95786bf55f6dcd0580bb 1 parent 5d97d72
Ben Noordhuis bnoordhuis authored
1  lib/net.js
@@ -851,6 +851,7 @@ var createServerHandle = exports._createServerHandle =
851 851 default:
852 852 // Not a fd we can listen on. This will trigger an error.
853 853 debug('listen invalid fd=' + fd + ' type=' + type);
  854 + global.errno = 'EINVAL'; // hack, callers expect that errno is set
854 855 handle = null;
855 856 break;
856 857 }
3  src/tty_wrap.cc
@@ -104,6 +104,9 @@ Handle<Value> TTYWrap::GuessHandleType(const Arguments& args) {
104 104 case UV_FILE:
105 105 return scope.Close(String::New("FILE"));
106 106
  107 + case UV_UNKNOWN_HANDLE:
  108 + return scope.Close(String::New("UNKNOWN"));
  109 +
107 110 default:
108 111 assert(0);
109 112 return v8::Undefined();
38 test/simple/test-listen-fd-ebadf.js
... ... @@ -0,0 +1,38 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var common = require('../common');
  23 +var assert = require('assert');
  24 +var net = require('net');
  25 +
  26 +var gotError = 0;
  27 +
  28 +process.on('exit', function() {
  29 + assert.equal(gotError, 2);
  30 +});
  31 +
  32 +net.createServer(assert.fail).listen({fd:2}).on('error', onError);
  33 +net.createServer(assert.fail).listen({fd:42}).on('error', onError);
  34 +
  35 +function onError(ex) {
  36 + assert.equal(ex.code, 'EINVAL');
  37 + gotError++;
  38 +}

0 comments on commit 3a6314d

Please sign in to comment.
Something went wrong with that request. Please try again.