napi-rs/crates/napi/src
Dennis Duda fc63ba8b52
fix(napi): some of the unsoundness in Buffer (#1294)
* fix leaked napi refcount in `Buffer` when cloning

Cloning unconditionally increased the refcount in `Buffer::clone`, but only called `napi_reference_unref` on dropping the last Buffer (the one with `strong_count == 1`). This means that the refcount will never drop back to zero after cloning, leaking the Buffer.

This commit changes it to also unconditionally unref the buffer.

* fix multiple sources of UB in `Buffer`

- `slice::from_raw_parts` may never be created with a null pointer, but `napi_get_buffer_info` was not sufficiently checked → UB when passing an empty Buffer
- `&'static mut [u8],` is invalid, as it certainly doesn't live for `'static`

Switching to `NonNull<u8>` and a `len` field fixes both of these.

- I also don't really understand how the `impl ToNapiValue for &mut Buffer` could have been sound. It creates an entirely new `Arc`, but reuses the same `Vec` allocation, leading to... a double free of the `Vec` on drop? I have replaced it with a simple call to `clone` instead.

* remove overcomplicated bool and drop impl

As far as I can tell, by just removing the bool and letting the drop code do its thing we clean up correctly in all cases. Because `napi_create_external_buffer` gets an owned `Buffer` attached to it via the Box, we can rely on `from_raw` retrieving it in the `drop_buffer` function.
2022-09-05 13:04:43 +08:00
..
bindgen_runtime fix(napi): some of the unsoundness in Buffer (#1294) 2022-09-05 13:04:43 +08:00
js_values feat(napi-derive): support set property attribute in napi macro (#1257) 2022-08-06 21:54:58 +08:00
async_cleanup_hook.rs Introduce #[napi] procedural macro to automation development boilerplate (#696) 2021-09-23 01:29:09 +08:00
async_work.rs fix(napi): clippy error on 1.58 2022-01-14 11:21:03 +08:00
call_context.rs refactor(napi): remove compatible Either struct 2021-11-25 17:53:41 +08:00
cleanup_env.rs Introduce #[napi] procedural macro to automation development boilerplate (#696) 2021-09-23 01:29:09 +08:00
env.rs feat(napi-derive): catch_unwind attribute (#1280) 2022-08-19 23:36:36 +08:00
error.rs feat(napi): error_anyhow feature 2022-08-17 22:50:52 +08:00
lib.rs feat(napi): error_anyhow feature 2022-08-17 22:50:52 +08:00
promise.rs fix(napi): clippy error on 1.58 2022-01-14 11:21:03 +08:00
status.rs Introduce #[napi] procedural macro to automation development boilerplate (#696) 2021-09-23 01:29:09 +08:00
task.rs feat(napi): implement AsyncTask with AbortSignal support 2021-11-06 13:51:54 +08:00
threadsafe_function.rs fix(napi): segfault when ThreadsafeFunction's callback closure captures data (#1281) 2022-08-20 22:40:26 +08:00
tokio_runtime.rs feat(napi): call sync functions within tokio runtime (#1242) 2022-08-04 00:12:35 +08:00
value_type.rs feat(napi): BigInt codegen support 2021-11-11 16:18:25 +08:00
version.rs fix(napi): correct the CString usage 2021-11-15 16:55:13 +08:00