Skip to content

Commit acd50ee

Browse files
theanarkhbmeck
authored andcommitted
src: add UV_PIPE_NO_TRUNCATE for bind in pipe_wrap.cc
PR-URL: nodejs#52347 Reviewed-By: Daeyeon Jeong <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 2619301 commit acd50ee

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

doc/api/net.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ sockets on other operating systems.
3737
[`socket.connect()`][] take a `path` parameter to identify IPC endpoints.
3838

3939
On Unix, the local domain is also known as the Unix domain. The path is a
40-
file system pathname. It gets truncated to an OS-dependent length of
41-
`sizeof(sockaddr_un.sun_path) - 1`. Typical values are 107 bytes on Linux and
42-
103 bytes on macOS. If a Node.js API abstraction creates the Unix domain socket,
43-
it will unlink the Unix domain socket as well. For example,
44-
[`net.createServer()`][] may create a Unix domain socket and
40+
file system pathname. It will throw an error when the length of pathname is
41+
greater than the length of `sizeof(sockaddr_un.sun_path)`. Typical values are
42+
107 bytes on Linux and 103 bytes on macOS. If a Node.js API abstraction creates
43+
the Unix domain socket, it will unlink the Unix domain socket as well. For
44+
example, [`net.createServer()`][] may create a Unix domain socket and
4545
[`server.close()`][] will unlink it. But if a user creates the Unix domain
4646
socket outside of these abstractions, the user will need to remove it. The same
4747
applies when a Node.js API creates a Unix domain socket but the program then

src/pipe_wrap.cc

+8-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ void PipeWrap::Bind(const FunctionCallbackInfo<Value>& args) {
162162
PipeWrap* wrap;
163163
ASSIGN_OR_RETURN_UNWRAP(&wrap, args.Holder());
164164
node::Utf8Value name(args.GetIsolate(), args[0]);
165-
int err = uv_pipe_bind2(&wrap->handle_, *name, name.length(), 0);
165+
int err =
166+
uv_pipe_bind2(&wrap->handle_, *name, name.length(), UV_PIPE_NO_TRUNCATE);
166167
args.GetReturnValue().Set(err);
167168
}
168169

@@ -225,8 +226,12 @@ void PipeWrap::Connect(const FunctionCallbackInfo<Value>& args) {
225226

226227
ConnectWrap* req_wrap =
227228
new ConnectWrap(env, req_wrap_obj, AsyncWrap::PROVIDER_PIPECONNECTWRAP);
228-
int err = req_wrap->Dispatch(
229-
uv_pipe_connect2, &wrap->handle_, *name, name.length(), 0, AfterConnect);
229+
int err = req_wrap->Dispatch(uv_pipe_connect2,
230+
&wrap->handle_,
231+
*name,
232+
name.length(),
233+
UV_PIPE_NO_TRUNCATE,
234+
AfterConnect);
230235
if (err) {
231236
delete req_wrap;
232237
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const net = require('net');
5+
const fs = require('fs');
6+
const tmpdir = require('../common/tmpdir');
7+
tmpdir.refresh();
8+
9+
// Test UV_PIPE_NO_TRUNCATE
10+
11+
// See pipe_overlong_path in https://github.com/libuv/libuv/blob/master/test/test-pipe-bind-error.c
12+
if (common.isWindows) {
13+
common.skip('UV_PIPE_NO_TRUNCATE is not supported on window');
14+
}
15+
16+
// See https://github.com/libuv/libuv/issues/4231
17+
const pipePath = `${tmpdir.path}/${'x'.repeat(10000)}.sock`;
18+
19+
const server = net.createServer()
20+
.listen(pipePath)
21+
// It may work on some operating systems
22+
.on('listening', () => {
23+
// The socket file must exsit
24+
assert.ok(fs.existsSync(pipePath));
25+
const socket = net.connect(pipePath, common.mustCall(() => {
26+
socket.destroy();
27+
server.close();
28+
}));
29+
})
30+
.on('error', (error) => {
31+
assert.ok(error.code === 'EINVAL', error.message);
32+
net.connect(pipePath)
33+
.on('error', common.mustCall((error) => {
34+
assert.ok(error.code === 'EINVAL', error.message);
35+
}));
36+
});

0 commit comments

Comments
 (0)