Skip to content

Commit

Permalink
raises an exception if passed non-socket descriptor.
Browse files Browse the repository at this point in the history
Test Script:
----
require 'socket'
require 'test/unit/assertions.rb'
include Test::Unit::Assertions

assert_raise(ArgumentError){ BasicSocket.for_fd(2) }
  • Loading branch information
Watson1978 committed Dec 12, 2011
1 parent 2f4b532 commit f1a158a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ext/socket/socket.c
Expand Up @@ -15,6 +15,7 @@
#include "ruby/util.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
Expand Down Expand Up @@ -239,8 +240,18 @@ static VALUE
init_sock(VALUE sock, int fd)
{
rb_io_t *fp;
MakeOpenFile(sock, fp);

#ifdef S_ISSOCK
struct stat sbuf;
if (fstat(fd, &sbuf) < 0) {
rb_sys_fail(0);
}
if (!S_ISSOCK(sbuf.st_mode)) {
rb_raise(rb_eArgError, "not a socket file descriptor");
}
#endif

MakeOpenFile(sock, fp);
fp->fd = fp->read_fd = fp->write_fd = fd;
fp->mode = FMODE_READWRITE|FMODE_DUPLEX;
rb_io_ascii8bit_binmode(sock);
Expand Down

0 comments on commit f1a158a

Please sign in to comment.