From d2233fec47b60b989ede97f982d15c5159173b18 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 18 Nov 2026 05:57:21 -0700 Subject: [PATCH 1/3] Test that values which do not fit their explicit tag raise ConstructorError Co-Authored-By: Claude Opus 5.5 --- test/runtests.jl | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index bad3270..8203ff7 100755 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -495,11 +495,20 @@ end @testset "explicit float tags reject non-decimal text" begin # only a decimal that rounds to zero or infinity takes the arbitrary-precision path for input in ("!!float 0b101", "!!float 0b1e400", "!!float 1@5", "!!float 1@400", "!!float 0x1p2000") - @test_throws ArgumentError YAML.load(input) + @test_throws YAML.ConstructorError YAML.load(input) end @test YAML.load("!!float 1e400") == Inf end +@testset "values that do not fit their explicit tag" begin + bad = String([0xd8]) # not valid UTF-8 + for input in ("!!map x", "!!map [1]", "a: !!map b", "!!bool y", "!!bool " * bad, "!!int abc", + "!!int " * bad, "!!float abc", "!!float 1" * bad, "!!binary 0") + @test_throws YAML.ConstructorError YAML.load(input) + end + @test_throws YAML.ConstructorError YAML.load("!!map x"; dicttype = Dict{String,Any}) +end + # issue 114 - gracefully handle extra commas in flow collections @testset "issue114" begin @test YAML.load("[3,4,]") == [3,4] @@ -869,8 +878,8 @@ end @test YAML.load_file(path) === expected end end - @test_throws ArgumentError YAML.load("!!int 0b102") - @test_throws ArgumentError YAML.load("!!int 0b") + @test_throws YAML.ConstructorError YAML.load("!!int 0b102") + @test_throws YAML.ConstructorError YAML.load("!!int 0b") @test YAML.load("0b102") == "0b102" @test YAML.load("0b") == "0b" @test YAML.load("0b" * bitstring(typemax(Int))) === typemax(Int) @@ -894,7 +903,7 @@ end @test YAML.load(YAML.yaml(Dict("k" => x))) == Dict("k" => x) end @test YAML.load("!!int 09" * "0"^25) == 9 * big(10)^25 - @test_throws ArgumentError YAML.load("!!int 1 2") + @test_throws YAML.ConstructorError YAML.load("!!int 1 2") end @testset "octal integer construction" begin From 646c1c8b74b21c12941e0e13ad25a04d6fb7fa10 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 18 Nov 2026 05:58:56 -0700 Subject: [PATCH 2/3] Raise ConstructorError for values that do not fit their explicit tag A scalar with an explicit tag its text cannot satisfy threw a Base error: KeyError for '!!bool y', ArgumentError for '!!int abc', '!!float abc', and '!!binary 0', and InvalidCharError for invalid UTF-8 text. A collection or scalar tagged '!!map' threw MethodError. The bool, int, float, and binary constructors now convert their text through one helper that turns a rejected conversion into a ConstructorError at the node, and construct_mapping checks the node kind, as construct_sequence and construct_scalar already do. Co-Authored-By: Claude Opus 5.5 --- src/constructor.jl | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/constructor.jl b/src/constructor.jl index 8ec7e9b..8f14067 100644 --- a/src/constructor.jl +++ b/src/constructor.jl @@ -179,7 +179,13 @@ function flatten_mapping(node::MappingNode) end -function construct_mapping(dicttype::Union{Type,Function}, constructor::Constructor, node::MappingNode; strict_unique_keys::Bool=false) +function construct_mapping(dicttype::Union{Type,Function}, constructor::Constructor, node::Node; strict_unique_keys::Bool=false) + if !(node isa MappingNode) + throw(ConstructorError(nothing, nothing, + "expected a mapping node, but found $(typeof(node))", + node.start_mark)) + end + # If the mapping was constructed with a merge tag, skip the later # test for duplicates, since it is a key part of the merging # functionality that selected values can be changed. @@ -246,14 +252,27 @@ const bool_values = Dict( "off" => false ) -function construct_yaml_bool(constructor::Constructor, node::Node) +# Convert a scalar node's text with `parse_text`. Text that it rejects, such as `!!int abc`, +# does not fit the node's tag, so it raises a ConstructorError at the node. +function construct_parsed_scalar(parse_text::Function, constructor::Constructor, node::Node) value = construct_scalar(constructor, node) - bool_values[lowercase(value)] + try + return parse_text(value) + catch + throw(ConstructorError(nothing, nothing, + "could not convert $(repr(value)) to $(node.tag)", node.start_mark)) + end end -function construct_yaml_int(constructor::Constructor, node::Node) - value = string(construct_scalar(constructor, node)) +construct_yaml_bool(constructor::Constructor, node::Node) = + construct_parsed_scalar(value -> bool_values[lowercase(value)], constructor, node) + + +construct_yaml_int(constructor::Constructor, node::Node) = + construct_parsed_scalar(parse_yaml_int, constructor, node) + +function parse_yaml_int(value::AbstractString) value = lowercase(replace(value, "_" => "")) if in(':', value) @@ -294,8 +313,10 @@ function parse_int(value::AbstractString, base::Int) end -function construct_yaml_float(constructor::Constructor, node::Node) - value = string(construct_scalar(constructor, node)) +construct_yaml_float(constructor::Constructor, node::Node) = + construct_parsed_scalar(parse_yaml_float, constructor, node) + +function parse_yaml_float(value::AbstractString) value = lowercase(replace(value, "_" => "")) if in(':', value) @@ -460,10 +481,8 @@ function construct_undefined(constructor::Constructor, node::Node) end -function construct_yaml_binary(constructor::Constructor, node::Node) - value = replace(string(construct_scalar(constructor, node)), "\n" => "") - base64decode(value) -end +construct_yaml_binary(constructor::Constructor, node::Node) = + construct_parsed_scalar(value -> base64decode(replace(value, "\n" => "")), constructor, node) const default_yaml_constructors = Dict{Union{String, Nothing}, Function}( "tag:yaml.org,2002:null" => construct_yaml_null, From ad37c118b3b845f8af643b01754f8be184d12618 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Wed, 18 Nov 2026 07:03:09 -0700 Subject: [PATCH 3/3] Let an interrupt pass through a tagged scalar conversion Co-Authored-By: Claude Opus 5.5 --- src/constructor.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/constructor.jl b/src/constructor.jl index 8f14067..82b7d3d 100644 --- a/src/constructor.jl +++ b/src/constructor.jl @@ -258,7 +258,8 @@ function construct_parsed_scalar(parse_text::Function, constructor::Constructor, value = construct_scalar(constructor, node) try return parse_text(value) - catch + catch e + e isa InterruptException && rethrow() throw(ConstructorError(nothing, nothing, "could not convert $(repr(value)) to $(node.tag)", node.start_mark)) end