Closed oylbin closed 9 months ago
enet_packet_resize
was intentionally removed
https://github.com/zpl-c/enet/blob/67178b030f75079b47d4c22115edc19cc10daf24/misc/ChangeLog#L5-L6
Hey, good find! I will get it fixed, _resize was re-added recently but I haven't considered we allocate the packet differently, hence the mismatch.
hi @zpl-zak , thanks for the quick response. I think there is still an issue in the new code. The pointer passed in by the user is freed, but the pointer to the newly allocated memory is not returned out. I think the return value type of the resize function may need to be modified.
I created a pull request
Right, sorry about that, thanks for the PR!
I'm using the latest
enet.h
from the master branch. Below is a simpletest.c
file to reproduce the error:Compile and run it, and then I encounter an error:
Upon debugging and following the code, the error is triggered by
enet_free(packet->data);
inenet_packet_resize
.https://github.com/zpl-c/enet/blob/67178b030f75079b47d4c22115edc19cc10daf24/include/enet.h#L1401-L1417
The program tries to free
packet->data
, but it is not the correct memory address allocated byenet_malloc
.As seen in
enet_packet_create
, the ENetPacket packet is allocated byenet_malloc
, andpacket->data
is an offset of this address.https://github.com/zpl-c/enet/blob/67178b030f75079b47d4c22115edc19cc10daf24/include/enet.h#L1374-L1379
The official ENet does not have this issue as
packet->data
is allocated byenet_malloc
.https://github.com/lsalzman/enet/blob/2a85cd64459f6ba038d233a634d9440490dbba12/packet.c#L19-L33
I also noticed issue #44. So, I think maybe
enet_packet_resize
was intentionally removed initially.Therefore, the solution may be to:
enet_packet_resize
and clearly document to users why this library does not provide it, as it saves one malloc call inenet_packet_create
.enet_packet_create
andenet_packet_resize
match.