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: fix usage of writeBuffer in makeSyncWrite #19103

Closed
wants to merge 2 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
29 changes: 19 additions & 10 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,23 @@ function message(key, args) {
* @returns {Error}
*/
function uvException(ctx) {
const err = new Error();
const [ code, uvmsg ] = errmap.get(ctx.errno);
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;

let path;
let dest;
if (ctx.path) {
path = ctx.path.toString();
message += ` '${path}'`;
}
if (ctx.dest) {
dest = ctx.dest.toString();
message += ` -> '${dest}'`;
}

// Pass the message to the constructor instead of setting it on the object
// to make sure it is the same as the one created in C++
const err = new Error(message);

for (const prop of Object.keys(ctx)) {
if (prop === 'message' || prop === 'path' || prop === 'dest') {
Expand All @@ -435,20 +451,13 @@ function uvException(ctx) {
err[prop] = ctx[prop];
}

const [ code, uvmsg ] = errmap.get(ctx.errno);
err.code = code;
let message = `${code}: ${uvmsg}, ${ctx.syscall}`;
if (ctx.path) {
const path = ctx.path.toString();
message += ` '${path}'`;
if (path) {
err.path = path;
}
if (ctx.dest) {
const dest = ctx.dest.toString();
message += ` -> '${dest}'`;
if (dest) {
err.dest = dest;
}
err.message = message;

Error.captureStackTrace(err, uvException);
return err;
Expand Down
11 changes: 6 additions & 5 deletions lib/internal/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const Buffer = require('buffer').Buffer;
const { isIPv6 } = process.binding('cares_wrap');
const { writeBuffer } = process.binding('fs');
const errors = require('internal/errors');

const octet = '(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])';
const re = new RegExp(`^${octet}[.]${octet}[.]${octet}[.]${octet}$`);
Expand Down Expand Up @@ -33,13 +34,13 @@ function makeSyncWrite(fd) {

this._bytesDispatched += chunk.length;

try {
writeBuffer(fd, chunk, 0, chunk.length, null);
} catch (ex) {
const ctx = {};
writeBuffer(fd, chunk, 0, chunk.length, null, undefined, ctx);
if (ctx.errno !== undefined) {
const ex = errors.uvException(ctx);
// Legacy: net writes have .code === .errno, whereas writeBuffer gives the
// raw errno number in .errno.
if (typeof ex.code === 'string')
ex.errno = ex.code;
ex.errno = ex.code;
return cb(ex);
}
cb();
Expand Down