Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 591f8ef030fe
Choose a base ref
...
head repository: crystal-lang/crystal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a619476b887c
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Jan 11, 2017

  1. Copy the full SHA
    2e3571a View commit details

Commits on Jan 12, 2017

  1. Merge pull request #3870 from MakeNowJust/fix/char-reader

    Disallow 3-byte UTF-8 surrogate pair
    Santiago Palladino authored Jan 12, 2017
    Copy the full SHA
    a619476 View commit details
Showing with 8 additions and 2 deletions.
  1. +4 −0 spec/std/char/reader_spec.cr
  2. +4 −2 src/char/reader.cr
4 changes: 4 additions & 0 deletions spec/std/char/reader_spec.cr
Original file line number Diff line number Diff line change
@@ -91,6 +91,10 @@ describe "Char::Reader" do
expect_raises(InvalidByteSequenceError) { Char::Reader.new(String.new Bytes[0xe0, 0x9F, 0xA0]) }
end

it "errors if first_byte == 0xED && second_byte >= 0xA0" do
expect_raises(InvalidByteSequenceError) { Char::Reader.new(String.new Bytes[0xed, 0xB0, 0xA0]) }
end

it "errors if first_byte < 0xF0 && (third_byte & 0xC0) != 0x80" do
expect_raises(InvalidByteSequenceError) { Char::Reader.new(String.new Bytes[0xe0, 0xA0, 0]) }
end
6 changes: 4 additions & 2 deletions src/char/reader.cr
Original file line number Diff line number Diff line change
@@ -150,8 +150,6 @@ struct Char
end

private def decode_char_at(pos)
# See http://en.wikipedia.org/wiki/UTF-8#Sample_code

first = byte_at(pos)
if first < 0x80
return yield first, 1
@@ -180,6 +178,10 @@ struct Char
invalid_byte_sequence(second, pos + 1)
end

if first == 0xed && second >= 0xa0
invalid_byte_sequence(second, pos + 1)
end

return yield (first << 12) + (second << 6) + (third - 0xE2080), 3
end