Skip to content

Commit bf772fb

Browse files
authored
ensure that the input String is GC preserved in equality check (#81)
1 parent 66586a1 commit bf772fb

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/InlineStrings.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module InlineStrings
22

3-
import Base: ==
4-
53
export InlineString, InlineStringType, inlinestrings
64
export @inline_str
75

@@ -290,14 +288,16 @@ macro inline_str(ex)
290288
end
291289

292290

293-
(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y)
294-
function ==(x::String, y::T) where {T <: InlineString}
291+
Base.:(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y)
292+
function Base.:(==)(x::String, y::T) where {T <: InlineString}
295293
sizeof(x) == sizeof(y) || return false
296294
ref = Ref{T}(_bswap(y))
297-
return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t),
298-
pointer(x), ref, sizeof(x)) == 0
295+
GC.@preserve x begin
296+
return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t),
297+
pointer(x), ref, sizeof(x)) == 0
298+
end
299299
end
300-
==(y::InlineString, x::String) = x == y
300+
Base.:(==)(y::InlineString, x::String) = x == y
301301

302302
Base.cmp(a::T, b::T) where {T <: InlineString} =
303303
Base.eq_int(a, b) ? 0 : Base.ult_int(a, b) ? -1 : 1

0 commit comments

Comments
 (0)