Skip to content

Commit

Permalink
code: fix enet_packet_resize
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Mar 1, 2024
1 parent 67178b0 commit 2f7b08a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 10 additions & 12 deletions include/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* software.
*
*/
#ifndef ENET_INCLUDE_H
#define ENET_INCLUDE_H
#ifndef enet_include_h
#define enet_include_h

#include <assert.h>
#include <stdlib.h>
Expand All @@ -43,7 +43,7 @@

#define ENET_VERSION_MAJOR 2
#define ENET_VERSION_MINOR 3
#define ENET_VERSION_PATCH 7
#define ENET_VERSION_PATCH 6
#define ENET_VERSION_CREATE(major, minor, patch) (((major)<<16) | ((minor)<<8) | (patch))
#define ENET_VERSION_GET_MAJOR(version) (((version)>>16)&0xFF)
#define ENET_VERSION_GET_MINOR(version) (((version)>>8)&0xFF)
Expand Down Expand Up @@ -1400,7 +1400,7 @@ extern "C" {
*/
int enet_packet_resize(ENetPacket * packet, size_t dataLength)
{
enet_uint8 *newData = 0;
ENetPacket *newPacket = NULL;

if (dataLength <= packet->dataLength || (packet->flags & ENET_PACKET_FLAG_NO_ALLOCATE))
{
Expand All @@ -1409,15 +1409,13 @@ extern "C" {
return 0;
}

newData = (enet_uint8 *) enet_malloc(dataLength);
if (newData == NULL)
newPacket = (ENetPacket *)enet_malloc(sizeof (ENetPacket) + dataLength);
if (newPacket == NULL)
return -1;

memcpy(newData, packet->data, packet->dataLength);
enet_free(packet->data);

packet->data = newData;
packet->dataLength = dataLength;
memcpy(newPacket, packet, sizeof(ENetPacket) + packet->dataLength);
newPacket->data = (enet_uint8 *)newPacket + sizeof(ENetPacket);
enet_free(packet);

return 0;
}
Expand Down
8 changes: 8 additions & 0 deletions test/resize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#define ENET_IMPLEMENTATION
#include "enet.h"
int main() {
ENetPacket *packet = enet_packet_create("packet",
strlen("packet") + 1,
ENET_PACKET_FLAG_RELIABLE);
enet_packet_resize(packet, strlen("packetfoo") + 1);
}

0 comments on commit 2f7b08a

Please sign in to comment.