Skip to content

Commit c3901ad

Browse files
committed
src: add UV_PIPE_NO_TRUNCATE for bind in pipe_wrap.cc
1 parent 4e9ce7c commit c3901ad

File tree

3 files changed

+42
-8
lines changed

3 files changed

+42
-8
lines changed

doc/api/net.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ sockets on other operating systems.
2929
[`socket.connect()`][] take a `path` parameter to identify IPC endpoints.
3030

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

src/pipe_wrap.cc

Lines changed: 8 additions & 3 deletions
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 {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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, see pipw_wrap.cc
10+
const pipePath = `${common.PIPE}-${'x'.repeat(300)}`;
11+
12+
const server = net.createServer()
13+
.listen(pipePath)
14+
// It may work on some operating systems
15+
.on('listening', () => {
16+
// The socket file must exsit
17+
assert.ok(fs.existsSync(pipePath));
18+
const socket = net.connect(pipePath, common.mustCall(() => {
19+
socket.destroy();
20+
server.close();
21+
}));
22+
})
23+
.on('error', (error) => {
24+
assert.ok(error.code === 'EINVAL', error.message);
25+
net.connect(pipePath)
26+
.on('error', common.mustCall((error) => {
27+
assert.ok(error.code === 'EINVAL', error.message);
28+
}));
29+
});

0 commit comments

Comments
 (0)