diff options
author | Aapo Alasuutari <aapo.alasuutari@gmail.com> | 2022-08-05 19:26:54 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-05 21:56:54 +0530 |
commit | 569910856e648e8cdaad85916eb82fea3c75ed8b (patch) | |
tree | 8d6619ce0067b6c7bab12103d509367256aa8387 /ext/ffi/lib.rs | |
parent | 6e6912489cc0bf5f661c1940691fcec5879e1f80 (diff) |
fix(ext/ffi): Check CStr for UTF-8 validity on read (#15318)
Co-authored-by: Phosra <phosra@tutanota.com>
Diffstat (limited to 'ext/ffi/lib.rs')
-rw-r--r-- | ext/ffi/lib.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/ext/ffi/lib.rs b/ext/ffi/lib.rs index ec80d4786..ae4ddd605 100644 --- a/ext/ffi/lib.rs +++ b/ext/ffi/lib.rs @@ -2187,13 +2187,14 @@ where permissions.check(None)?; // SAFETY: Pointer is user provided. - let cstr = unsafe { CStr::from_ptr(ptr as *const c_char) }.to_bytes(); - let value: v8::Local<v8::Value> = - v8::String::new_from_utf8(scope, cstr, v8::NewStringType::Normal) - .ok_or_else(|| { - type_error("Invalid CString pointer, string exceeds max length") - })? - .into(); + let cstr = unsafe { CStr::from_ptr(ptr as *const c_char) } + .to_str() + .map_err(|_| type_error("Invalid CString pointer, not valid UTF-8"))?; + let value: v8::Local<v8::Value> = v8::String::new(scope, cstr) + .ok_or_else(|| { + type_error("Invalid CString pointer, string exceeds max length") + })? + .into(); Ok(value.into()) } |