napi-rs/crates/napi/src/bindgen_runtime/js_values
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
..
array.rs feat(napi): support rust array to js array 2022-08-12 17:53:36 +08:00
arraybuffer.rs fix(napi): memory leak in Buffer/ArrayBuffer 2022-07-11 21:34:27 +08:00
bigint.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
boolean.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
buffer.rs fix(napi): some of the unsoundness in Buffer (#1294) 2022-09-05 13:04:43 +08:00
class.rs feat(napi): implement as_object and validate for ClassInstance (#1284) 2022-08-23 17:02:41 +08:00
date.rs refactor(napi): use libloading on Windows 2022-05-06 22:27:09 +08:00
either.rs feat(napi): implement as_unknown and validate for Either types (#1285) 2022-08-23 17:02:51 +08:00
external.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
function.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
map.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
nil.rs feat(napi): add some useful derived traits for the Null type (#1241) 2022-07-22 23:45:02 +08:00
number.rs feat(napi): implement ToNapiValue for f32 2022-08-23 16:18:08 +08:00
object.rs chore(napi): misc cleanup 2022-08-23 16:18:08 +08:00
promise.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
serde.rs feat: impl FromNapiValue for serde_json::Number, fix it for Null, throw for impossible types (#1052) 2022-02-06 11:58:17 +08:00
string.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
symbol.rs refactor(napi): Either now perform ValidateNapiValue::validate rather than type_of 2022-07-05 17:01:21 +08:00
task.rs refactor(napi): use libloading on Windows 2022-05-06 22:27:09 +08:00
value_ref.rs fix(napi): remove previous reference if value_ref existed 2022-08-25 21:51:06 +08:00