Skip to content

Commit 56fee39

Browse files
authored
avoid extra refcount increment in GetField (#155)
When returning the payload of a non-`None` `Optional` (i.e. just the value we were passed) in `GetField`, we were accidentally incrementing its refcount, resulting in a leak. Fixes #152 Signed-off-by: Joel Dice <[email protected]>
1 parent b9cbfff commit 56fee39

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

runtime/src/lib.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,10 @@ pub extern "C" fn componentize_py_get_field<'a>(
672672
.into_pyobject(*py)
673673
.unwrap()
674674
.into_any(),
675-
PAYLOAD_FIELD_INDEX => value.to_owned(),
675+
PAYLOAD_FIELD_INDEX => unsafe {
676+
// Avoid incrementing `value`'s refcount while returning it:
677+
Bound::from_owned_ptr(*py, value.as_ptr())
678+
},
676679
_ => unreachable!(),
677680
},
678681
Type::NestingOption => match i32::try_from(field).unwrap() {
@@ -682,7 +685,11 @@ pub extern "C" fn componentize_py_get_field<'a>(
682685
.into_any(),
683686
PAYLOAD_FIELD_INDEX => {
684687
if value.is_none() {
685-
value.to_owned()
688+
unsafe {
689+
// Avoid incrementing `value`'s refcount while returning
690+
// it:
691+
Bound::from_owned_ptr(*py, value.as_ptr())
692+
}
686693
} else {
687694
value.getattr("value").unwrap()
688695
}

0 commit comments

Comments
 (0)