Skip to content

Commit beb85e7

Browse files
committed
[Bug #21705] Fix segfaults on Windows
It should check the type of the argument and coercion before converting the encoding.
1 parent a7f948c commit beb85e7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

ext/socket/unixsocket.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ unixsock_path_value(VALUE path)
4242
}
4343
}
4444
#endif
45+
path = rb_get_path(path);
4546
#ifdef _WIN32
4647
/* UNIXSocket requires UTF-8 per spec. */
4748
path = rb_str_export_to_enc(path, rb_utf8_encoding());
4849
#endif
49-
return rb_get_path(path);
50+
return path;
5051
}
5152

5253
VALUE

test/socket/test_unix.rb

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,18 @@ def bound_unix_socket(klass)
293293
File.unlink path if path && File.socket?(path)
294294
end
295295

296-
def test_open_nul_byte
297-
tmpfile = Tempfile.new("s")
298-
path = tmpfile.path
299-
tmpfile.close(true)
300-
assert_raise(ArgumentError) {UNIXServer.open(path+"\0")}
301-
assert_raise(ArgumentError) {UNIXSocket.open(path+"\0")}
302-
ensure
303-
File.unlink path if path && File.socket?(path)
296+
def test_open_argument
297+
assert_raise(TypeError) {UNIXServer.new(nil)}
298+
assert_raise(TypeError) {UNIXServer.new(1)}
299+
Tempfile.create("s") do |s|
300+
path = s.path
301+
s.close
302+
File.unlink(path)
303+
assert_raise(ArgumentError) {UNIXServer.open(path+"\0")}
304+
assert_raise(ArgumentError) {UNIXSocket.open(path+"\0")}
305+
arg = Struct.new(:to_path).new(path)
306+
assert_equal(path, UNIXServer.open(arg) { |server| server.path })
307+
end
304308
end
305309

306310
def test_addr

0 commit comments

Comments
 (0)