Skip to content

Commit e4e0c29

Browse files
committed
[BinaryPlatforms]: normalize values a bit when inserting new tags
1 parent 4ef8313 commit e4e0c29

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

base/binaryplatforms.jl

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,21 +84,8 @@ struct Platform <: AbstractPlatform
8484
value = normver(value)
8585
end
8686

87-
# I know we said only alphanumeric and dots, but let's be generous so that we can expand
88-
# our support in the future while remaining as backwards-compatible as possible. The
89-
# only characters that are absolutely disallowed right now are `-`, `+`, ` ` and things
90-
# that are illegal in filenames:
91-
nonos = raw"""+- /<>:"'\|?*"""
92-
if any(occursin(nono, tag) for nono in nonos)
93-
throw(ArgumentError("Invalid character in tag name \"$(tag)\"!"))
94-
end
95-
96-
# Normalize and reject nonos
97-
value = lowercase(string(value))
98-
if any(occursin(nono, value) for nono in nonos)
99-
throw(ArgumentError("Invalid character in tag value \"$(value)\"!"))
100-
end
101-
tags[tag] = value
87+
# Use `add_tag!()` to add the tag to our collection of tags
88+
add_tag!(tags, tag, string(value)::String)
10289
end
10390

10491
# Auto-map call_abi and libc where necessary:
@@ -129,13 +116,36 @@ struct Platform <: AbstractPlatform
129116
end
130117
end
131118

119+
# Simple tag insertion that performs a little bit of validation
120+
function add_tag!(tags::Dict{String,String}, tag::String, value::String)
121+
# I know we said only alphanumeric and dots, but let's be generous so that we can expand
122+
# our support in the future while remaining as backwards-compatible as possible. The
123+
# only characters that are absolutely disallowed right now are `-`, `+`, ` ` and things
124+
# that are illegal in filenames:
125+
nonos = raw"""+- /<>:"'\|?*"""
126+
if any(occursin(nono, tag) for nono in nonos)
127+
throw(ArgumentError("Invalid character in tag name \"$(tag)\"!"))
128+
end
129+
130+
# Normalize and reject nonos
131+
value = lowercase(value)
132+
if any(occursin(nono, value) for nono in nonos)
133+
throw(ArgumentError("Invalid character in tag value \"$(value)\"!"))
134+
end
135+
tags[tag] = value
136+
return value
137+
end
138+
132139
# Other `Platform` types can override this (I'm looking at you, `AnyPlatform`)
133140
tags(p::Platform) = p.tags
134141

135142
# Make it act more like a dict
136143
Base.getindex(p::AbstractPlatform, k::String) = getindex(tags(p), k)
137144
Base.haskey(p::AbstractPlatform, k::String) = haskey(tags(p), k)
138-
Base.setindex!(p::AbstractPlatform, v::String, k::String) = (setindex!(tags(p), v, k); p)
145+
function Base.setindex!(p::AbstractPlatform, v::String, k::String)
146+
add_tag!(tags(p), k, v)
147+
return p
148+
end
139149

140150
# Allow us to easily serialize Platform objects
141151
function Base.repr(p::Platform; context=nothing)

test/binaryplatforms.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ end
114114
@test p["foo"] == "bar"
115115
@test p["os"] == "linux"
116116
p["os"] = "JuliaOS"
117-
@test p["os"] == "JuliaOS"
117+
@test p["os"] == "juliaos"
118+
119+
# Test that trying to set illegal tags fails
120+
@test_throws ArgumentError p["os"] = "a+b"
118121
end
119122

120123
@testset "Triplet parsing" begin
@@ -363,4 +366,4 @@ end
363366
@test !platforms_match(ac, bc)
364367
@test platforms_match(ac, ac)
365368
@test platforms_match(bc, bc)
366-
end
369+
end

0 commit comments

Comments
 (0)