Skip to content

Commit 7af47af

Browse files
authored
Fix some string-related invalidation risks (#37799)
1 parent 8ccc4cc commit 7af47af

File tree

15 files changed

+35
-29
lines changed

15 files changed

+35
-29
lines changed

base/Base.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ const (∛)=cbrt
276276
delete_method(which(include, (Module, String)))
277277
let SOURCE_PATH = ""
278278
global function include(mod::Module, path::String)
279-
prev = SOURCE_PATH
279+
prev = SOURCE_PATH::String
280280
path = normpath(joinpath(dirname(prev), path))
281281
Core.println(path)
282282
ccall(:jl_uv_flush, Nothing, (Ptr{Nothing},), Core.io_pointer(Core.stdout))
@@ -335,16 +335,16 @@ using .MathConstants: ℯ, π, pi
335335
# metaprogramming
336336
include("meta.jl")
337337

338+
# Stack frames and traces
339+
include("stacktraces.jl")
340+
using .StackTraces
341+
338342
# utilities
339343
include("deepcopy.jl")
340344
include("download.jl")
341345
include("summarysize.jl")
342346
include("errorshow.jl")
343347

344-
# Stack frames and traces
345-
include("stacktraces.jl")
346-
using .StackTraces
347-
348348
include("initdefs.jl")
349349

350350
# worker threads

base/binaryplatforms.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ julia> triplet(Platform("armv7l", "Linux"; libgfortran_version="3"))
499499
"""
500500
function triplet(p::AbstractPlatform)
501501
str = string(
502-
arch(p),
502+
arch(p)::Union{Symbol,String},
503503
os_str(p),
504504
libc_str(p),
505505
call_abi_str(p),
@@ -552,15 +552,19 @@ end
552552

553553
# Helper functions for Linux and FreeBSD libc/abi mishmashes
554554
function libc_str(p::AbstractPlatform)
555-
if libc(p) === nothing
555+
lc = libc(p)
556+
if lc === nothing
556557
return ""
557-
elseif libc(p) === "glibc"
558+
elseif lc === "glibc"
558559
return "-gnu"
559560
else
560-
return string("-", libc(p))
561+
return string("-", lc)
561562
end
562563
end
563-
call_abi_str(p::AbstractPlatform) = (call_abi(p) === nothing) ? "" : call_abi(p)
564+
function call_abi_str(p::AbstractPlatform)
565+
cabi = call_abi(p)
566+
cabi === nothing ? "" : string(cabi::Union{Symbol,String})
567+
end
564568

565569
Sys.isapple(p::AbstractPlatform) = os(p) == "macos"
566570
Sys.islinux(p::AbstractPlatform) = os(p) == "linux"

base/errorshow.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ end
678678
# Print a stack frame where the module color is determined by looking up the parent module in
679679
# `modulecolordict`. If the module does not have a color, yet, a new one can be drawn
680680
# from `modulecolorcycler`.
681-
function print_stackframe(io, i, frame, n, digit_align_width, modulecolordict, modulecolorcycler)
681+
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolordict, modulecolorcycler)
682682
m = Base.parentmodule(frame)
683683
if m !== nothing
684684
while parentmodule(m) !== m
@@ -698,7 +698,7 @@ end
698698

699699

700700
# Print a stack frame where the module color is set manually with `modulecolor`.
701-
function print_stackframe(io, i, frame, n, digit_align_width, modulecolor)
701+
function print_stackframe(io, i, frame::StackFrame, n::Int, digit_align_width, modulecolor)
702702
file, line = string(frame.file), frame.line
703703
stacktrace_expand_basepaths() && (file = something(find_source_file(file), file))
704704
stacktrace_contract_userdir() && (file = replaceuserpath(file))

base/initdefs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ const DEPOT_PATH = String[]
9090
function append_default_depot_path!(DEPOT_PATH)
9191
path = joinpath(homedir(), ".julia")
9292
path in DEPOT_PATH || push!(DEPOT_PATH, path)
93-
path = abspath(Sys.BINDIR, "..", "local", "share", "julia")
93+
path = abspath(Sys.BINDIR::String, "..", "local", "share", "julia")
9494
path in DEPOT_PATH || push!(DEPOT_PATH, path)
95-
path = abspath(Sys.BINDIR, "..", "share", "julia")
95+
path = abspath(Sys.BINDIR::String, "..", "share", "julia")
9696
path in DEPOT_PATH || push!(DEPOT_PATH, path)
9797
end
9898

base/methodshow.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ const methodloc_callback = Ref{Union{Function, Nothing}}(nothing)
127127
function fixup_stdlib_path(path::String)
128128
# The file defining Base.Sys gets included after this file is included so make sure
129129
# this function is valid even in this intermediary state
130-
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB
130+
if isdefined(@__MODULE__, :Sys) && Sys.BUILD_STDLIB_PATH != Sys.STDLIB::String
131131
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
132-
path = replace(path, normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB))
132+
path = replace(path, normpath(Sys.BUILD_STDLIB_PATH) => normpath(Sys.STDLIB::String))
133133
end
134134
return path
135135
end

base/process.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ end
418418
419419
Run `command` and return the resulting output as a `String`.
420420
"""
421-
read(cmd::AbstractCmd, ::Type{String}) = String(read(cmd))
421+
read(cmd::AbstractCmd, ::Type{String}) = String(read(cmd))::String
422422

423423
"""
424424
run(command, args...; wait::Bool = true)

base/strings/util.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,9 @@ function split(str::T, splitter::AbstractChar;
391391
_split(str, isequal(splitter), limit, keepempty, T <: SubString ? T[] : SubString{T}[])
392392
end
393393

394-
function _split(str::AbstractString, splitter, limit::Integer, keepempty::Bool, strs::Vector)
394+
function _split(str::AbstractString, splitter::F, limit::Integer, keepempty::Bool, strs::Vector) where F
395+
# Forcing specialization on `splitter` improves performance (roughly 30% decrease in runtime)
396+
# and prevents a major invalidation risk (1550 MethodInstances)
395397
i = 1 # firstindex(str)
396398
n = lastindex(str)::Int
397399
r = findfirst(splitter,str)::Union{Nothing,Int,UnitRange{Int}}

base/sysinfo.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ end
122122
function __init_build()
123123
global BINDIR = ccall(:jl_get_julia_bindir, Any, ())::String
124124
vers = "v$(VERSION.major).$(VERSION.minor)"
125-
global STDLIB = abspath(BINDIR, "..", "share", "julia", "stdlib", vers)
125+
global STDLIB = abspath(BINDIR::String, "..", "share", "julia", "stdlib", vers)
126126
nothing
127127
end
128128

base/toml_parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ const Err{T} = Union{T, ParserError}
278278
function format_error_message_for_err_type(error::ParserError)
279279
msg = err_message[error.type]
280280
if error.type == ErrInvalidBareKeyCharacter
281-
c_escaped = escape_string(string(error.data))
281+
c_escaped = escape_string(string(error.data)::String)
282282
msg *= ": '$c_escaped'"
283283
end
284284
return msg

contrib/generate_precompile.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Sys.__init_build()
77
if !isdefined(Base, :uv_eventloop)
88
Base.reinit_stdio()
99
end
10-
Base.include(@__MODULE__, joinpath(Sys.BINDIR, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
10+
Base.include(@__MODULE__, joinpath(Sys.BINDIR::String, "..", "share", "julia", "test", "testhelpers", "FakePTYs.jl"))
1111
import .FakePTYs: open_fake_pty
1212

1313
CTRL_C = '\x03'
@@ -54,7 +54,7 @@ push!(Set{Method}(), first(methods(collect)))
5454
get(Base.pkgorigins, Base.PkgId(Base), nothing)
5555
"""
5656

57-
julia_exepath() = joinpath(Sys.BINDIR, Base.julia_exename())
57+
julia_exepath() = joinpath(Sys.BINDIR::String, Base.julia_exename())
5858

5959
have_repl = haskey(Base.loaded_modules,
6060
Base.PkgId(Base.UUID("3fa0cd96-eef1-5676-8a61-b3b8758bbffb"), "REPL"))
@@ -195,7 +195,7 @@ function generate_precompile_statements()
195195
readavailable(output_copy)
196196
# Input our script
197197
if have_repl
198-
precompile_lines = split(precompile_script, '\n'; keepempty=false)
198+
precompile_lines = split(precompile_script::String, '\n'; keepempty=false)
199199
curr = 0
200200
for l in precompile_lines
201201
sleep(0.1)
@@ -228,7 +228,7 @@ function generate_precompile_statements()
228228
push!(statements, statement)
229229
end
230230

231-
for statement in split(hardcoded_precompile_statements, '\n')
231+
for statement in split(hardcoded_precompile_statements::String, '\n')
232232
push!(statements, statement)
233233
end
234234

0 commit comments

Comments
 (0)