Closed gperciva closed 3 years ago
Neither fix is correct. findleaf
can pass the cookie to btree_node_fetch
and see it come back later; that alone breaks the patch in #206, while #205 fails to free the cookie if findleaf
fails when it is invoked from btree_node_fetch
(aka after needing to page in a node).
The correct fix is to say that findleaf
take ownership of the cookie and is responsible for ensuring that it is freed even if findleaf
returns non-zero.
Updated
Another attempt, again with low confidence in the comments.
I'm particularly un-enthused about
if we found the leaf and (C->e != NULL), then C->callback_range is responsible for freeing C->e.
I mean, I get that you don't want to use kvldskey_dup(C->e)
to pass a new value to the callback, to avoid an unnecessary memory allocation. But that makes the explanation of what's happening a bit more wordy.
How about simply "findleaf is responsible for ensuring that C and C->e are freed"? Along with a comment you already have in the code about how C->e was freed by callback_range.
Updated.
Two alternative fixes for the same bug. #205 is a smaller fix, while #206 is bigger but makes the code easier to reason about. (That said, I might be missing something about the code organization, so I wouldn't be shocked if you prefer the smaller fix.)