From 2c15d97bb9adc2d1fb8c4492a39fd72be7e15fe5 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Mon, 24 Oct 2022 23:29:36 -0600 Subject: [PATCH] Ensure string options are converted to proper strings before passing to ccall (#204) Proper fix to #201. The core issue here is that if you take the `pointer` of a SubString, it doesn't properly account for the _substring_ length, because C strings continue until the NUL termination. --- src/api/capi.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/api/capi.jl b/src/api/capi.jl index ac8cc8b..a53a514 100644 --- a/src/api/capi.jl +++ b/src/api/capi.jl @@ -964,14 +964,20 @@ For more information about option files used by MySQL programs, see Section 4.2. function setoption(mysql::MYSQL, option::mysql_option, arg="0") if option in CUINTOPTS ref = Ref{Cuint}(Cuint(arg)) + return @checksuccess mysql mysql_options(mysql.ptr, option, ref) elseif option in CULONGOPTS ref = Ref{Culong}(Culong(arg)) + return @checksuccess mysql mysql_options(mysql.ptr, option, ref) elseif option in BOOLOPTS ref = Ref{Bool}(Bool(arg)) + return @checksuccess mysql mysql_options(mysql.ptr, option, ref) else - ref = arg == C_NULL ? C_NULL : convert(Ptr{Cvoid}, pointer(arg)) + str = arg == C_NULL ? C_NULL : String(arg) + GC.@preserve str begin + ref = str == C_NULL ? C_NULL : convert(Ptr{Cvoid}, pointer(str)) + return @checksuccess mysql mysql_options(mysql.ptr, option, ref) + end end - return @checksuccess mysql mysql_options(mysql.ptr, option, ref) end #="""