Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: check and throw on error for getsockname #12871

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,8 +1469,10 @@ Object.defineProperty(Server.prototype, 'listening', {
Server.prototype.address = function() {
if (this._handle && this._handle.getsockname) {
var out = {};
this._handle.getsockname(out);
// TODO(bnoordhuis) Check err and throw?
var err = this._handle.getsockname(out);
if (err) {
throw errnoException(err, 'address');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe

if (err) {
  self.emit('error', errnoException(err, 'address'));
  return;
}

Copy link
Contributor

@refack refack May 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@addaleax won't throwing make this semver-major?
Ohh just saw you marked it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that’s why I’ve labelled it as a major. ;) But emitting an error event a) would be semver-major too and b) doesn’t really make sense here, it’s a fully synchronous call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I agree.

}
return out;
} else if (this._pipeName) {
return this._pipeName;
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-socket-address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const net = require('net');

// This tests checks that if server._handle.getsockname
// returns an error number, an error is thrown.

const server = net.createServer({});
server.listen(0, common.mustCall(function() {
server._handle.getsockname = function(out) {
return -1;
};
assert.throws(() => this.address(),
/^Error: address ([\w|\s-\d])+$/);
server.close();
}));