From bf772fb62ac3b75e3422baaa8d9f8bc5e79254e1 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Tue, 23 Jul 2024 10:48:56 +0200 Subject: [PATCH] ensure that the input String is GC preserved in equality check (#81) --- src/InlineStrings.jl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/InlineStrings.jl b/src/InlineStrings.jl index 712a4df..1e314ef 100644 --- a/src/InlineStrings.jl +++ b/src/InlineStrings.jl @@ -1,7 +1,5 @@ module InlineStrings -import Base: == - export InlineString, InlineStringType, inlinestrings export @inline_str @@ -290,14 +288,16 @@ macro inline_str(ex) end -(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y) -function ==(x::String, y::T) where {T <: InlineString} +Base.:(==)(x::T, y::T) where {T <: InlineString} = Base.eq_int(x, y) +function Base.:(==)(x::String, y::T) where {T <: InlineString} sizeof(x) == sizeof(y) || return false ref = Ref{T}(_bswap(y)) - return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t), - pointer(x), ref, sizeof(x)) == 0 + GC.@preserve x begin + return ccall(:memcmp, Cint, (Ptr{UInt8}, Ref{T}, Csize_t), + pointer(x), ref, sizeof(x)) == 0 + end end -==(y::InlineString, x::String) = x == y +Base.:(==)(y::InlineString, x::String) = x == y Base.cmp(a::T, b::T) where {T <: InlineString} = Base.eq_int(a, b) ? 0 : Base.ult_int(a, b) ? -1 : 1