Skip to content

Unsound issues while converting value to utf8 #103

@CXWorks

Description

@CXWorks

Hi, thanks for your time to read this issue. Our static analysis tool found there might be an unsound issue in your value_to_string_unchecked converting the vallue to utf8 str:

pub fn value_to_string_unchecked(value: Value) -> String {
unsafe {
let str = string::rb_string_value_ptr(&value) as *const u8;
let len = string::rb_str_len(value) as usize;
let vec = ::std::slice::from_raw_parts(str, len).to_vec();
String::from_utf8_unchecked(vec)
}
}

This may introduce invalid utf8 conversion and producing an invalid value, which is considered as undefined behaviors in Rust. We expect either to mark the whole function as unsafe or leverage the safe verison to convert because this library can take raw bytes from the Ruby sys and missed the validation of utf8. As a reference, the safe version of the utf8 conversion in std is:

https://github.com/rust-lang/rust/blob/3002af6cb643138839537f6fd0265162610fdbbe/library/core/src/str/converts.rs#L131-L140

Could you please help us double check the potential probelm? Thanks again for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions