-
Notifications
You must be signed in to change notification settings - Fork 31
add "Proper memory safety with GC.@preserve" #204
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
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #204 +/- ##
==========================================
- Coverage 91.10% 86.85% -4.25%
==========================================
Files 5 5
Lines 697 799 +102
==========================================
+ Hits 635 694 +59
- Misses 62 105 +43 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Convert a null-terminated byte buffer to string safely with bounds checking | ||
and memory protection. Internal helper function. | ||
""" | ||
function safe_string_convert(x::Vector{UInt8}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed? Why not using String(copy(x))
instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function serves a specific purpose that String(copy(x)) doesn't handle:
- Null Termination Handling
for example
# With null terminator
data = UInt8[65, 66, 67, 0, 68, 69] # "ABC\0DE"
# Using String(copy(x)):
String(copy(data)) # Would give "ABC\0DE" (includes null and data after it)
# Using safe_string_convert:
safe_string_convert(data) # Gives "ABC" (stops at null)
# Add Windows-specific configuration at the start of the test file | ||
if Sys.iswindows() | ||
ENV["JULIA_CPU_THREADS"] = "1" | ||
Base.GC.enable(true) | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? ENV["JULIA_CPU_THREADS"]
is also the wrong way to check the number of threads for two reasons:
- the environment variable may not be defined at all and you'd get an error in that case. To reference an env var which may not be defined you should use
get(ENV, "JULIA_CPU_THREADS", <a default value here>)
- the right way to check the number of threads is
Threads.nthreads()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i waill update this thanks for ur suggestion :)
if length(s) < 2 || s[1] != '\'' || s[end] != '\'' | ||
return nothing | ||
end | ||
GC.@preserve s begin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need to preserve s
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was unessary implmentaion
let me clear it
The issue on master has been fixed now, so this PR should not be necessary anymore. |
No worries thanks for letting me know :) |
Add GC.@preserve and enhance table string operations for Julia nightly compatibility
Description
This PR addresses the Julia nightly compatibility issues of this #196 highlighted in #194 by improving string handling in table operations and ensuring proper memory management. Key improvements include the addition of
GC.@preserve
blocks and a newsafe_table_string_convert
function.Changes
1. String Handling Improvements
Added new
safe_table_string_convert
function for safer string handling in table operations:The below function was initiated in #196 I have updated a lit bit:)
Implemented type traits pattern to prevent method overwriting: