~singpolyma/dhall-ruby

dd5013b053d616d4fb8b0a135ac98ec39b2eac9c — Stephen Paul Weber 5 years ago da5b58b
Fail fast when encoding is bad
2 files changed, 16 insertions(+), 0 deletions(-)

M lib/dhall.rb
M test/test_load.rb
M lib/dhall.rb => lib/dhall.rb +4 -0
@@ 21,6 21,10 @@ module Dhall
	end

	def self.load_raw(source)
		unless source.valid_encoding?
			raise ArgumentError, "invalid byte sequence in #{source.encoding}"
		end

		begin
			return from_binary(source) if source.encoding == Encoding::BINARY
		rescue Exception # rubocop:disable Lint/RescueException

M test/test_load.rb => test/test_load.rb +12 -0
@@ 13,6 13,18 @@ class TestLoad < Minitest::Test
		assert_equal Dhall::Natural.new(value: 1), Dhall.load("1".b).sync
	end

	def test_load_invalid_utf8
		assert_raises ArgumentError do
			Dhall.load("\xc3\x28").sync
		end
	end

	def test_load_invalid_utf8_binary_input
		assert_raises ArgumentError do
			Dhall.load("\xc3\x28".b).sync
		end
	end

	def test_load_natural_binary
		assert_equal(
			Dhall::Natural.new(value: 1),