Skip to content

Cstring does not check for null-termination #16499

@Ismael-VC

Description

@Ismael-VC

This is pasted verbatim from @ScottPJones report at julia-dev:


In the course of adding support to CSV.jl for being able to load into arbitrary AbstractFloat types, via a keyword argument floattype (which defaults to Float64),
I ran into a number of bugs (both in v0.4.x and v0.5-dev). The problem occurs because pointer_to_string() does not create a string that has a terminating \0 (which I feel is correct),
but Cstring does not ensure that a string passed to C has a terminating \0.
This is inconsistent with Cwstring, which does make sure that a Cwstring does have a terminating Cwchar(0).

I'd make a PR to fix these bugs, but since I'm unable to, I'm posting the issue here.

Here are some test cases demonstrating the bugs:

julia> s = "53.9abcdef"
"53.9abcdef"

julia> u = pointer_to_string(pointer(s.data),2)
"53"

julia> t = pointer_to_string(pointer(s.data),4)
"53.9"

julia> parse(Dec64, t)
ERROR: ArgumentError: invalid number format 53.9
 in parse at /j/DecFP.jl/src/DecFP.jl:82

julia> parse(Dec64, u)
ERROR: ArgumentError: invalid number format 53
 in parse at /j/DecFP.jl/src/DecFP.jl:82

julia> parse(BigInt, u)
ERROR: ArgumentError: invalid BigInt: "53"
 in tryparse_internal at gmp.jl:104
 in parse at parse.jl:146

julia> parse(BigFloat, t)
ERROR: ArgumentError: invalid number format "53.9" for BigFloat
 in parse at parse.jl:164

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions