Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using String constructor on Vector{UInt8} #52

Merged
merged 2 commits into from
May 10, 2024

Conversation

nhz2
Copy link
Member

@nhz2 nhz2 commented May 10, 2024

The String constructor doesn't behave consistently for all AbstractVector{UInt8}. Normally it makes a copy, except for Vector{UInt8} where it truncates the input. In Julia this can in some weird cases lead to undefined behavior. Ref: JuliaLang/julia#54372 JuliaLang/julia#54424

This PR avoids potential issues with String(v::Vector{UInt8}) by using an internal copy_string(v::AbstractVector{UInt8}) function that should always make a copy of the input.

# version of String(v::AbstractVector{UInt8}) that works consistently.
function copy_string(v::AbstractVector{UInt8})::String
    String(v)
end
function copy_string(v::Vector{UInt8})::String
    GC.@preserve v unsafe_string(pointer(v), length(v))
end

Copy link

codecov bot commented May 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.61%. Comparing base (e550feb) to head (f632847).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #52   +/-   ##
=======================================
  Coverage   99.61%   99.61%           
=======================================
  Files           6        6           
  Lines         772      776    +4     
=======================================
+ Hits          769      773    +4     
  Misses          3        3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nhz2 nhz2 merged commit 20d6ae7 into main May 10, 2024
14 checks passed
@nhz2 nhz2 deleted the Avoid-using-String-on-vector-of-bytes branch May 10, 2024 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant