Skip to content

Commit 73413fc

Browse files
authored
Merge pull request #37633 from JuliaLang/sf/small_bp_fixes
[BinaryPlatforms]: normalize values a bit when inserting new tags
2 parents 0f782b2 + e4e0c29 commit 73413fc

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

base/binaryplatforms.jl

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,8 @@ struct Platform <: AbstractPlatform
152152
value = normver(value)
153153
end
154154

155-
# I know we said only alphanumeric and dots, but let's be generous so that we can expand
156-
# our support in the future while remaining as backwards-compatible as possible. The
157-
# only characters that are absolutely disallowed right now are `-`, `+`, ` ` and things
158-
# that are illegal in filenames:
159-
nonos = raw"""+- /<>:"'\|?*"""
160-
if any(occursin(nono, tag) for nono in nonos)
161-
throw(ArgumentError("Invalid character in tag name \"$(tag)\"!"))
162-
end
163-
164-
# Normalize and reject nonos
165-
value = lowercase(string(value))
166-
if any(occursin(nono, value) for nono in nonos)
167-
throw(ArgumentError("Invalid character in tag value \"$(value)\"!"))
168-
end
169-
tags[tag] = value
155+
# Use `add_tag!()` to add the tag to our collection of tags
156+
add_tag!(tags, tag, string(value)::String)
170157
end
171158

172159
# Auto-map call_abi and libc where necessary:
@@ -197,13 +184,36 @@ struct Platform <: AbstractPlatform
197184
end
198185
end
199186

187+
# Simple tag insertion that performs a little bit of validation
188+
function add_tag!(tags::Dict{String,String}, tag::String, value::String)
189+
# I know we said only alphanumeric and dots, but let's be generous so that we can expand
190+
# our support in the future while remaining as backwards-compatible as possible. The
191+
# only characters that are absolutely disallowed right now are `-`, `+`, ` ` and things
192+
# that are illegal in filenames:
193+
nonos = raw"""+- /<>:"'\|?*"""
194+
if any(occursin(nono, tag) for nono in nonos)
195+
throw(ArgumentError("Invalid character in tag name \"$(tag)\"!"))
196+
end
197+
198+
# Normalize and reject nonos
199+
value = lowercase(value)
200+
if any(occursin(nono, value) for nono in nonos)
201+
throw(ArgumentError("Invalid character in tag value \"$(value)\"!"))
202+
end
203+
tags[tag] = value
204+
return value
205+
end
206+
200207
# Other `Platform` types can override this (I'm looking at you, `AnyPlatform`)
201208
tags(p::Platform) = p.tags
202209

203210
# Make it act more like a dict
204211
Base.getindex(p::AbstractPlatform, k::String) = getindex(tags(p), k)
205212
Base.haskey(p::AbstractPlatform, k::String) = haskey(tags(p), k)
206-
Base.setindex!(p::AbstractPlatform, v::String, k::String) = (setindex!(tags(p), v, k); p)
213+
function Base.setindex!(p::AbstractPlatform, v::String, k::String)
214+
add_tag!(tags(p), k, v)
215+
return p
216+
end
207217

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

test/binaryplatforms.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ end
126126
@test p["foo"] == "bar"
127127
@test p["os"] == "linux"
128128
p["os"] = "JuliaOS"
129-
@test p["os"] == "JuliaOS"
129+
@test p["os"] == "juliaos"
130+
131+
# Test that trying to set illegal tags fails
132+
@test_throws ArgumentError p["os"] = "a+b"
130133
end
131134

132135
@testset "Triplet parsing" begin

0 commit comments

Comments
 (0)