From dd5013b053d616d4fb8b0a135ac98ec39b2eac9c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Thu, 11 Apr 2019 22:03:50 -0500 Subject: [PATCH] Fail fast when encoding is bad --- lib/dhall.rb | 4 ++++ test/test_load.rb | 12 ++++++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/dhall.rb b/lib/dhall.rb index 5b404d7..d81ade0 100644 --- a/lib/dhall.rb +++ b/lib/dhall.rb @@ -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 diff --git a/test/test_load.rb b/test/test_load.rb index 1feb16a..1fb10c2 100644 --- a/test/test_load.rb +++ b/test/test_load.rb @@ -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), -- 2.45.2