GSP
GSP

Reputation: 3789

How to create a string with a "bad encoding" in ruby?

I have a file somewhere out in production that I do not have access to that, when loaded by a ruby script, a regular expression against the contents fails with a ArgumentError => invalid byte sequence in UTF-8.

I believe I have a fix based on the answer with all the points here: ruby 1.9: invalid byte sequence in UTF-8

# Remove all invalid and undefined characters in the given string
# (ruby 1.9.3)
def safe_str str

  # edited based on matt's comment (thanks matt)
  s = str.encode('utf-16', 'utf-8', invalid: :replace, undef: :replace, replace: '')
  s.encode!('utf-8', 'utf-16')
end

However, I now want to build my rspec to verify that the code works. I don't have access to the file that caused the problem so I want to create a string with the bad encoding programatically.

I've tried variations on things like:

bad_str = (100..1000).to_a.inject('') {|s,c| s << c; s}
bad_str.length.should > safe_str(bad_str).length

or,

bad_str = (100..1000).to_a.pack(c*)
bad_str.length.should > safe_str(bad_str).length

but the length is always the same. I have also tried different character ranges; not always 100 to 1000.

Any suggestions on how to build a string with an invalid encoding within a ruby 1.9.3 script?

Upvotes: 7

Views: 2057

Answers (5)

Sergio Belevskij
Sergio Belevskij

Reputation: 2947

Following example can be used for testing purposes:

describe TestClass do
  let(:non_utf8_text) { "something\255 english." }

  it 'is not raise error on invalid byte sequence string' do
    expect(non_utf8_text).not_to be_valid_encoding
    expect { subject.call(non_utf8_text) }.not_to raise_error
  end
end

Thanks to Iwan B. for "\255" advise.

Upvotes: 1

Iwan B.
Iwan B.

Reputation: 4166

Try with s = "hi \255"

s.valid_encoding?
# => false

Upvotes: 3

matt
matt

Reputation: 79733

Your safe_str method will (currently) never actually do anything to the string, it is a no-op. The docs for String#encode on Ruby 1.9.3 say:

Please note that conversion from an encoding enc to the same encoding enc is a no-op, i.e. the receiver is returned without any changes, and no exceptions are raised, even if there are invalid bytes.

This is true for the current release of 2.0.0 (patch level 247), however a recent commit to Ruby trunk changes this, and also introduces a scrub method that pretty much does what you want.

Until a new version of Ruby is released you will need to round trip your text string to another encoding and back to clean it, as in the second example in this answer to the question you linked to, something like:

def safe_str str
  s = str.encode('utf-16', 'utf-8', invalid: :replace, undef: :replace, replace: '')
  s.encode!('utf-8', 'utf-16')
end

Note that your first example of an attempt to create an invalid string won’t work:

bad_str = (100..1000).to_a.inject('') {|s,c| s << c; s}
bad_str.valid_encoding? # => true

From the << docs:

If the object is a Integer, it is considered as a codepoint, and is converted to a character before concatenation.

So you’ll always get a valid string.

Your second method, using pack will create a string with the encoding ASCII-8BIT. If you then change this using force_encoding you can create a UTF-8 string with an invalid encoding:

bad_str = (100..1000).to_a.pack('c*').force_encoding('utf-8')
bad_str.valid_encoding? # => false

Upvotes: 3

parhamr
parhamr

Reputation: 597

In spec tests I’ve written, I haven’t found a way to fix this bad encoding:

Period%Basics

The %B string consistently produces ArgumentError: invalid byte sequence in UTF-8.

Upvotes: 0

Hew Wolff
Hew Wolff

Reputation: 1509

Lots of one-byte strings will make an invalid UTF-8 string, starting with 0x80. So 128.chr should work.

Upvotes: 5

Related Questions