From d2862209889e6711444259a55e869d7dcb0580c3 Mon Sep 17 00:00:00 2001 From: Andre Marques Lee Date: Wed, 3 Dec 2014 00:21:28 +0000 Subject: [PATCH 1/4] Implemented a couple of bug fixes: * Connection#search now does more graceful nil-checking in its ensure clause, reducing cryptic crash messages * Filter class methods should now be able to gracefully accept numeric literals as filter rhs values --- lib/net/ldap/connection.rb | 3 ++- lib/net/ldap/filter.rb | 12 ++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index 91fb7e90..c690a8c3 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -562,12 +562,13 @@ def search(args = nil) result_pdu || OpenStruct.new(:status => :failure, :result_code => Net::LDAP::ResultCodeOperationsError, :message => "Invalid search") end # instrument ensure + # clean up message queue for this search messages = message_queue.delete(message_id) # in the exceptional case some messages were *not* consumed from the queue, # instrument the event but do not fail. - unless messages.empty? + unless messages.nil? or messages.empty? instrument "search_messages_unread.net_ldap_connection", message_id: message_id, messages: messages end diff --git a/lib/net/ldap/filter.rb b/lib/net/ldap/filter.rb index 5d91f9f6..5a2528bd 100644 --- a/lib/net/ldap/filter.rb +++ b/lib/net/ldap/filter.rb @@ -644,10 +644,18 @@ def match(entry) end ## - # Converts escaped characters (e.g., "\\28") to unescaped characters + # If the argument is a string, converts escaped characters (e.g., "\\28") to unescaped characters. + # If the argument is a number, just return as-is. + # Otherwise, an exception is thrown and the rhs argument is rejected. # ("("). def unescape(right) - right.gsub(/\\([a-fA-F\d]{2})/) { [$1.hex].pack("U") } + if defined? right.gsub + right.gsub(/\\([a-fA-F\d]{2})/) { [$1.hex].pack("U") } + elsif right.is_a? Fixnum + right.to_s + else + raise ArgumentError, "Did not know how to convert argument \"#{right}\" into the rhs of an LDAP filter" + end end private :unescape From b133b317b320c051d25dfcbcd2dfb2dca5261972 Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 7 Jan 2015 12:35:01 -0800 Subject: [PATCH 2/4] unescape always to_s. test --- lib/net/ldap/filter.rb | 13 ++----------- test/test_filter.rb | 5 +++++ 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/net/ldap/filter.rb b/lib/net/ldap/filter.rb index 1f43ced2..4f5da3e5 100644 --- a/lib/net/ldap/filter.rb +++ b/lib/net/ldap/filter.rb @@ -644,18 +644,9 @@ def match(entry) end ## - # If the argument is a string, converts escaped characters (e.g., "\\28") to unescaped characters. - # If the argument is a number, just return as-is. - # Otherwise, an exception is thrown and the rhs argument is rejected. - # ("("). + # Converts escaped characters (e.g., "\\28") to unescaped characters def unescape(right) - if defined? right.gsub - right.gsub(/\\([a-fA-F\d]{2})/) { [$1.hex].pack("U") } - elsif right.is_a? Fixnum - right.to_s - else - raise ArgumentError, "Did not know how to convert argument \"#{right}\" into the rhs of an LDAP filter" - end + right.to_s.gsub(/\\([a-fA-F\d]{2})/) { [$1.hex].pack("U") } end private :unescape diff --git a/test/test_filter.rb b/test/test_filter.rb index eb6192d0..139612a1 100644 --- a/test/test_filter.rb +++ b/test/test_filter.rb @@ -215,4 +215,9 @@ def test_parse_ber_escapes_characters filter = Net::LDAP::Filter.parse_ber(ber.read_ber(Net::LDAP::AsnSyntax)) assert_equal "(objectclass=#{escaped}*#{escaped}*#{escaped})", filter.to_s end + + def test_unescape_fixnums + filter = Net::LDAP::Filter.eq("objectclass", 3) + assert_equal "\xA3\x10\x04\vobjectclass\x04\x013".b, filter.to_ber + end end From f046564db16d2c19dc16f2307bcf7f86250a769c Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 7 Jan 2015 12:52:24 -0800 Subject: [PATCH 3/4] use if for conjunction conditional --- lib/net/ldap/connection.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index 53c5fde2..7ed0bdb5 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -577,7 +577,7 @@ def search(args = nil) # in the exceptional case some messages were *not* consumed from the queue, # instrument the event but do not fail. - unless messages.nil? or messages.empty? + if !messages.nil? && !messages.empty? instrument "search_messages_unread.net_ldap_connection", message_id: message_id, messages: messages end From 82bf3124b42bdccaeeea19cdd3bce4d52191f73d Mon Sep 17 00:00:00 2001 From: Jerry Cheung Date: Wed, 7 Jan 2015 12:59:06 -0800 Subject: [PATCH 4/4] add contributor --- Contributors.rdoc | 1 + 1 file changed, 1 insertion(+) diff --git a/Contributors.rdoc b/Contributors.rdoc index b3b25ff6..e40b20db 100644 --- a/Contributors.rdoc +++ b/Contributors.rdoc @@ -21,3 +21,4 @@ Contributions since: * nowhereman * David J. Lee (DavidJLee) * Cody Cutrer (ccutrer) +* WoodsBagotAndreMarquesLee