Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in peer_send if sending to non-"connected" peer #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

enet2.dll: enet.c
gcc -O2 -shared -o $@ $< -lenet -llua51 -lws2_32 -lwinmm

enet.so: enet.c
luarocks make --local enet-dev-1.rockspec

enet.dll: enet.c
gcc -O2 -shared -o $@ $< -lenet -llua5.1 -lws2_32 -lwinmm

2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ packets are guaranteed to arrive, and arrive in the order in which they are sent
Unsequenced packets are unreliable and have no guarantee on the order they
arrive. Defaults to reliable.

Returns 0 on success and < 0 on failure.

<a name="peerstate"></a>
### `peer:state()`

Expand Down
31 changes: 27 additions & 4 deletions enet.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/**
*
* Copyright (C) 2014 by Leaf Corcoran
*
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand Down Expand Up @@ -486,6 +486,28 @@ static int host_get_peer(lua_State *l) {
return 1;
}

static int host_send(lua_State* l) {
ENetHost *host = check_host(l, 1);
if (!host) {
return luaL_error(l, "Tried to index a nil host!");
}

ENetAddress address;
parse_address(l, luaL_checkstring(l, 2), &address);

size_t size;
const void *data = luaL_checklstring(l, 3, &size);

ENetBuffer buffer;
// Casting away the const here is dangerous, but it seems to work well.
buffer.data = (void*)data;
buffer.dataLength = size;

enet_socket_send(host->socket, &address, &buffer, 1);

return 0;
}

static int host_gc(lua_State *l) {
// We have to manually grab the userdata so that we can set it to NULL.
ENetHost** host = luaL_checkudata(l, 1, "enet_host");
Expand Down Expand Up @@ -744,6 +766,7 @@ static const struct luaL_Reg enet_host_funcs [] = {
{"service_time", host_service_time},
{"peer_count", host_peer_count},
{"get_peer", host_get_peer},
{"send_from_socket", host_send},
{NULL, NULL}
};

Expand Down Expand Up @@ -772,7 +795,7 @@ static const struct luaL_Reg enet_event_funcs [] = {
{NULL, NULL}
};

int luaopen_enet(lua_State *l) {
int luaopen_enet2(lua_State *l) {
enet_initialize();
atexit(enet_deinitialize);

Expand Down