[前][次][番号順一覧][スレッド一覧]

ruby-changes:41487

From: nobu <ko1@a...>
Date: Sun, 17 Jan 2016 15:31:03 +0900 (JST)
Subject: [ruby-changes:41487] nobu:r53561 (trunk): option.c: single byte boolean

nobu	2016-01-17 15:31:36 +0900 (Sun, 17 Jan 2016)

  New Revision: 53561

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53561

  Log:
    option.c: single byte boolean
    
    * ext/socket/option.c (sockopt_bool): relax boolean size to be one
      too not only sizeof(int).  Winsock getsockopt() returns a single
      byte as a boolean socket option.  [ruby-core:72730] [Bug #11958]

  Modified files:
    trunk/ChangeLog
    trunk/ext/socket/option.c
    trunk/test/socket/test_sockopt.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53560)
+++ ChangeLog	(revision 53561)
@@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jan 17 15:30:57 2016  Nobuyoshi Nakada  <nobu@r...>
+
+	* ext/socket/option.c (sockopt_bool): relax boolean size to be one
+	  too not only sizeof(int).  Winsock getsockopt() returns a single
+	  byte as a boolean socket option.  [ruby-core:72730] [Bug #11958]
+
 Sun Jan 17 14:43:01 2016  Kuniaki IGARASHI  <igaiga@g...>
 
 	* test/ruby/test_env.rb: [Fix GH-1201]
Index: ext/socket/option.c
===================================================================
--- ext/socket/option.c	(revision 53560)
+++ ext/socket/option.c	(revision 53561)
@@ -302,10 +302,15 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ext/socket/option.c#L302
 sockopt_bool(VALUE self)
 {
     int i;
+    long len;
     VALUE data = sockopt_data(self);
     StringValue(data);
-    check_size(RSTRING_LEN(data), sizeof(int));
-    memcpy((char*)&i, RSTRING_PTR(data), sizeof(int));
+    len = RSTRING_LEN(data);
+    if (len == 1) {
+	return *RSTRING_PTR(data) == 0 ? Qfalse : Qtrue;
+    }
+    check_size(len, sizeof(int));
+    memcpy((char*)&i, RSTRING_PTR(data), len);
     return i == 0 ? Qfalse : Qtrue;
 }
 
Index: test/socket/test_sockopt.rb
===================================================================
--- test/socket/test_sockopt.rb	(revision 53560)
+++ test/socket/test_sockopt.rb	(revision 53561)
@@ -25,6 +25,12 @@ class TestSocketOption < Test::Unit::Tes https://github.com/ruby/ruby/blob/trunk/test/socket/test_sockopt.rb#L25
     assert_equal(true, opt.bool)
     opt = Socket::Option.int(:INET, :SOCKET, :KEEPALIVE, 2)
     assert_equal(true, opt.bool)
+    Socket.open(:INET, :STREAM) {|s|
+      s.setsockopt(Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, true))
+      assert_equal(true, s.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool)
+      s.setsockopt(Socket::Option.bool(:INET, :SOCKET, :KEEPALIVE, false))
+      assert_equal(false, s.getsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE).bool)
+    }
   end
 
   def test_ipv4_multicast_loop

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]