Skip to content

Commit

Permalink
Merge pull request #192 from casperisfine/fstr
Browse files Browse the repository at this point in the history
Fix compatibility with the `--enable-string-literal` Ruby option
  • Loading branch information
alexdalitz authored Mar 28, 2024
2 parents ad8d4c6 + a0ab148 commit f86d3c1
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 33 deletions.
15 changes: 9 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby: [ '3.1', '3.2', '3.3' ] # , 'ruby-head' ]
rubyopt: ['']
include:
- ruby: '3.3'
rubyopt: "--enable-frozen-string-literal --debug-frozen-string-literal"

name: Ruby ${{ matrix.ruby }} tests
steps:
- uses: actions/checkout@v2
- name: Setup Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
- name: Bundle install
run: |
gem install bundler
bundle install --jobs 4 --retry 3
- name: Run tests
run: bundle exec rake test
bundler-cache: true
- name: Run tests ${{ matrix.rubyopt }}
run: bundle exec rake test RUBYOPT="${{ matrix.rubyopt }}"
4 changes: 2 additions & 2 deletions lib/dnsruby/bit_mapping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module BitMapping

# Converts from a binary string to a number, e.g. "\x01\x00" => 256
def binary_string_to_number(string)
string = string.clone.force_encoding(Encoding::ASCII_8BIT)
string = string.b
string.bytes.inject(0) do |number, byte|
number * 256 + byte.ord
end
Expand All @@ -25,7 +25,7 @@ def binary_string_to_number(string)
# Converts a number to a binary encoded string, e.g. 256 => "\x01\x00"
def number_to_binary_string(number, min_length = 0)
assert_non_negative(number)
binary_string = ''.force_encoding(Encoding::ASCII_8BIT)
binary_string = ''.b

while number > 0
byte_value = number & 0xFF
Expand Down
5 changes: 3 additions & 2 deletions lib/dnsruby/code_mappers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def unknown_code(arg)
# (RFC3597)
# See typesbyval and typesbyname, these beasts have the same functionality
def Classes.classesbyname(name) #:nodoc: all
name.upcase!;
name = name.upcase

if to_code(name)
return to_code(name)
end
Expand Down Expand Up @@ -202,7 +203,7 @@ def unknown_code(arg) #:nodoc: all
# mnemonic. If the TYPE mapping is not specified the generic mnemonic
# TYPE### is returned.
def Types.typesbyname(name) #:nodoc: all
name.upcase!
name = name.upcase

if to_code(name)
return to_code(name)
Expand Down
10 changes: 5 additions & 5 deletions lib/dnsruby/ipv6.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def self.create(arg)
when IPv6
return arg
when String
address = ''
address = +''
if Regex_8Hex =~ arg
arg.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')}
elsif Regex_CompressedHex =~ arg
prefix = $1
suffix = $2
a1 = ''
a2 = ''
a1 = +''
a2 = +''
prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
omitlen = 16 - a1.length - a2.length
Expand All @@ -80,8 +80,8 @@ def self.create(arg)
elsif Regex_CompressedHex4Dec =~ arg
prefix, suffix, a, b, c, d = $1, $2, $3.to_i, $4.to_i, $5.to_i, $6.to_i
if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d
a1 = ''
a2 = ''
a1 = +''
a2 = +''
prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')}
suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')}
omitlen = 12 - a1.length - a2.length
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/message/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Dnsruby
class MessageEncoder #:nodoc: all
def initialize
@data = ''
@data = +''
@names = {}
yield self if block_given?
end
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsruby/message/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ def rcode
end

def to_s
s = '' # the output string to return
s = +'' # the output string to return

if @answerfrom && (! @answerfrom.empty?)
s << ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"
Expand Down Expand Up @@ -457,7 +457,7 @@ def to_s


def old_to_s
retval = ''
retval = +''

if (@answerfrom != nil && @answerfrom != '')
retval = retval + ";; Answer received from #{@answerfrom} (#{@answersize} bytes)\n;;\n"
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/recursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ def _dorecursion(name, type, klass, known_zone, known_authorities, depth, no_val
return nil
end

known_zone.sub!(/\.*$/, ".")
known_zone = known_zone.sub(/\.*$/, ".")

ns = [] # Array of AddressCaches (was array of array of addresses)
@@mutex.synchronize{
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/resource/GPOS.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def build_rdata
end

def self.build_rdata(longitude, latitude, altitude)
binary_string = ''.force_encoding('ASCII-8BIT')
binary_string = ''.b

binary_string << longitude.length.chr
binary_string << longitude
Expand Down
4 changes: 2 additions & 2 deletions lib/dnsruby/resource/NSEC.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def encode_types
end

def self.encode_types(nsec)
output = ''
output = +''
# types represents all 65536 possible RR types.
# Split up types into sets of 256 different types.
type_codes = []
Expand All @@ -171,7 +171,7 @@ def self.encode_types(nsec)

unless types_to_go.empty?
# Then create the bitmap for them
bitmap = ''
bitmap = +''
# keep on adding them until there's none left
pos = 0
bitmap_pos = 0
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/resource/NXT.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def self.build_rdata(next_domain, types)
next_domain = Name.create(next_domain) if next_domain.is_a?(String)
types = TypeBitmap.from_type_codes(types) if types.is_a?(Array)

binary_string = ''.force_encoding('ASCII-8BIT')
binary_string = ''.b
binary_string << next_domain.canonical
binary_string << BitMapping.reverse_binary_string_bits(types.to_binary_string)
binary_string
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/resource/TXT.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def TXT.parse(input)
unquoted = false
seen_strings = false
pos = 0
input.sub!(/^\s*\(\s*/, "")
input = input.sub(/^\s*\(\s*/, "")
input.sub!(/\s*\)\s*$/, "")
input.each_char {|c|
if (((c == "'") || (c == '"')) && (!in_escaped) && (!unquoted))
Expand Down
8 changes: 4 additions & 4 deletions lib/dnsruby/select_thread.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,12 @@ def tcp_read(socket)
# Keep buffer for all TCP sockets, and return
# to select after reading available data. Once all data has been received,
# then process message.
buf=""
buf = +""
expected_length = 0
@@mutex.synchronize {
buf, expected_length = @@tcp_buffers[socket]
if (!buf)
buf = ""
buf = +""
expected_length = 2
@@tcp_buffers[socket]=[buf, expected_length]
end
Expand All @@ -443,7 +443,7 @@ def tcp_read(socket)

return false
end
buf << input
buf << input if input
rescue
# Oh well - better luck next time!
return false
Expand All @@ -455,7 +455,7 @@ def tcp_read(socket)
# We just read the data_length field. Now we need to start reading that many bytes.
@@mutex.synchronize {
answersize = buf.unpack('n')[0]
@@tcp_buffers[socket] = ["", answersize]
@@tcp_buffers[socket] = [+"", answersize]
}
return tcp_read(socket)
else
Expand Down
2 changes: 1 addition & 1 deletion lib/dnsruby/single_verifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ def verify_rrset(rrset, keys = nil)
}.to_s # @TODO@ worry about wildcards here?
rec.ttl = old_ttl
if (RUBY_VERSION >= "1.9")
data.force_encoding("ASCII-8BIT")
data.force_encoding(Encoding::BINARY)
end
sig_data += data
end
Expand Down
8 changes: 6 additions & 2 deletions test/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
end

if ENV['RUN_EXTRA_TASK'] == 'TRUE'
require 'coveralls'
Coveralls.wear!
unless "test".frozen?
# Coverall setup term-ansi-color which isn't yet frozen string literal compatible
# Ref: https://github.com/flori/term-ansicolor/pull/38
require 'coveralls'
Coveralls.wear!
end

require 'simplecov'

Expand Down
2 changes: 1 addition & 1 deletion test/tc_gpos.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_decode_encode
response_binary = "E0\x84\x80\x00\x01\x00\x01\x00\x01\x00\x01\x01a\adnsruby\x03com\x00\x00\e\x00\x01\xC0\f\x00\e\x00\x01\x00\x00*0\x00\x0F\x0410.0\x0420.0\x0430.0\xC0\x0E\x00\x02\x00\x01\x00\x00*0\x00\x06\x03ns1\xC0\x0E\xC0F\x00\x01\x00\x01\x00\x00*0\x00\x04\x7F\x00\x00\x01"
message_object = Message.decode(response_binary)
reconstructed_binary = message_object.encode
assert_equal response_binary.force_encoding('ASCII-8BIT'), reconstructed_binary
assert_equal response_binary.b, reconstructed_binary
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/tc_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_2eq
end

def test_equals
response_as_string = "\x10\a\x81\x90\x00\x01\x00\x04\x00\x00\x00\x06\x03cnn\x03com\x00\x00\x02\x00\x01\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x14\x03ns3\ntimewarner\x03net\x00\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x11\x03ns2\x03p42\x06dynect\xC04\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x06\x03ns1\xC0)\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x06\x03ns1\xC0I\xC0%\x00\x01\x00\x01\x00\x001\xA2\x00\x04\xC7\aD\xEE\xC0E\x00\x01\x00\x01\x00\x00\xB1\x0E\x00\x04\xCC\r\xFA*\xC0b\x00\x01\x00\x01\x00\x009`\x00\x04\xCCJl\xEE\xC0t\x00\x01\x00\x01\x00\x00\xBDg\x00\x04\xD0NF*\xC0t\x00\x1C\x00\x01\x00\x00\x00\xBB\x00\x10 \x01\x05\x00\x00\x90\x00\x01\x00\x00\x00\x00\x00\x00\x00B\x00\x00)\x0F\xA0\x00\x00\x80\x00\x00\x00".force_encoding("ASCII-8BIT")
response_as_string = "\x10\a\x81\x90\x00\x01\x00\x04\x00\x00\x00\x06\x03cnn\x03com\x00\x00\x02\x00\x01\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x14\x03ns3\ntimewarner\x03net\x00\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x11\x03ns2\x03p42\x06dynect\xC04\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x06\x03ns1\xC0)\xC0\f\x00\x02\x00\x01\x00\x01QC\x00\x06\x03ns1\xC0I\xC0%\x00\x01\x00\x01\x00\x001\xA2\x00\x04\xC7\aD\xEE\xC0E\x00\x01\x00\x01\x00\x00\xB1\x0E\x00\x04\xCC\r\xFA*\xC0b\x00\x01\x00\x01\x00\x009`\x00\x04\xCCJl\xEE\xC0t\x00\x01\x00\x01\x00\x00\xBDg\x00\x04\xD0NF*\xC0t\x00\x1C\x00\x01\x00\x00\x00\xBB\x00\x10 \x01\x05\x00\x00\x90\x00\x01\x00\x00\x00\x00\x00\x00\x00B\x00\x00)\x0F\xA0\x00\x00\x80\x00\x00\x00".b
message = Message.decode(response_as_string)
assert(message == message, message.to_s)
end
Expand Down

0 comments on commit f86d3c1

Please sign in to comment.