30

I see this a lot and haven't figured out a graceful solution. If user input contains invalid byte sequences, I need to be able to have it not raise an exception. For example:

# @raw_response comes from user and contains invalid UTF-8
# for example: @raw_response = "\xBF"  
regex.match(@raw_response)
ArgumentError: invalid byte sequence in UTF-8

Numerous similar questions have been asked and the result appears to be encoding or force encoding the string. Neither of these work for me however:

regex.match(@raw_response.force_encoding("UTF-8"))
ArgumentError: invalid byte sequence in UTF-8

or

regex.match(@raw_response.encode("UTF-8", :invalid=>:replace, :replace=>"?"))
ArgumentError: invalid byte sequence in UTF-8

Is this a bug with Ruby 2.0.0 or am I missing something?

What is strange is it appear to be encoding correctly, but match continues to raise an exception:

@raw_response.encode("UTF-8", :invalid=>:replace, :replace=>"?").encoding
 => #<Encoding:UTF-8>
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
  • 2
    Can you please provide a [verifiable example](http://stackoverflow.com/help/mcve) – mdesantis Jun 04 '14 at 12:19
  • You tag this as ruby-on-rails, but normally any rails form will post utf-8 to the server. How does your form look? Or what is the origin of your strings. – nathanvda Jun 04 '14 at 12:35
  • This happens several different ways in our app: user agent strings and meta information on files. – Tom Rossi Jun 04 '14 at 12:36
  • mdesantis sure, just use any invalid string: @raw_response = "\xBF" – Tom Rossi Jun 04 '14 at 12:39
  • Same question as http://stackoverflow.com/questions/11375342/stringencode-not-fixing-invalid-byte-sequence-in-utf-8-error – nathanvda Jun 04 '14 at 13:11

2 Answers2

51

In Ruby 2.0 the encode method is a no-op when encoding a string to its current encoding:

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 changed in 2.1, which also added the scrub method as an easier way to do this.

If you are unable to upgrade to 2.1, you’ll have to encode into a different encoding and back in order to remove invalid bytes, something like:

if ! s.valid_encoding?
  s = s.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
end
matt
  • 78,533
  • 8
  • 163
  • 197
11

Since you're using Rails and not just Ruby you can also use tidy_bytes. This works with Ruby 2.0 and also will probably give you back sensible data instead of just replacement characters.

Yogh
  • 591
  • 6
  • 17
  • 2
    I feel compelled to give you a big belated thank you. Knowing about "tidy_bytes" earlier would've saved me many hours of frustration...can't believe I hadn't heard of it until just now. – dancow Mar 09 '15 at 14:01
  • `tidy_bytes` is amazing! I wish I knew about it earlier too lol. – Tom Lehman Jan 26 '20 at 18:38