Skip to content

Commit

Permalink
Fixed some warnings from valgrind
Browse files Browse the repository at this point in the history
mumble.buffer:read(Number, String ...) now accepts multiple arguments and returns. Can accept "*a" for reading all remaining data.
  • Loading branch information
bkacjios committed Jan 4, 2025
1 parent 1ac5e45 commit c860b25
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,9 +572,6 @@ mumble.timer:stop()
A buffer object used to read/write data from. It will dynamically adjust its capacity to fit all written data.

``` lua
-- The buffers __tostring metamethod will return the written data as a string
String data = tostring(buffer)

-- Packs the buffer, moving all remaining data that has not been read to the start.
-- BEFORE PACK = [1234|5678|]
-- Read head ^ ^ Write head
Expand Down Expand Up @@ -621,7 +618,8 @@ Number position = buffer:seek([String mode ["read", "write", "both"] = "read", S
Number written = buffer:write(String data)

-- Reads the specified number of bytes from the buffer and returns the data as a string
String data = buffer:read(Number size)
-- Accepts multiple arguments, like buffer:read(4, 6, "*all")
String data = buffer:read([Number length, String format, ..]

-- Write a single byte to the buffer
-- Returns how many bytes were written to the buffer
Expand Down
39 changes: 33 additions & 6 deletions mumble/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,44 @@ int luabuffer_write(lua_State *l) {

int luabuffer_read(lua_State *l) {
ByteBuffer *buffer = luaL_checkudata(l, 1, METATABLE_BUFFER);
uint64_t size = luaL_checkinteger(l, 2);
char read[size];
int nargs = lua_gettop(l) - 1; // Get number of arguments
char *read;

if (!buffer_read(buffer, &read, size)) {
return luaL_error(l, "attempt to read beyond buffer limit");
for (int i = 2; i < nargs + 2; i++) {
size_t size = 0;

if (lua_type(l, i) == LUA_TNUMBER) {
// If the argument is a number, read that many bytes
size = (size_t) lua_tointeger(l, i);
} else {
const char *arg = lua_tostring(l, i);
if (arg && arg[0] == '*' && arg[1] == 'a') {
// If the argument is "*a", read the entire buffer
size = buffer_length(buffer);
} else {
return luaL_argerror(l, i, "invalid format");
}
}

// Allocate memory and perform the read operation
read = malloc(size);
if (!read) {
return luaL_error(l, "failed to allocate memory for read buffer");
}

if (!buffer_read(buffer, read, size)) {
free(read);
return luaL_error(l, "attempt to read beyond buffer limit");
}

lua_pushlstring(l, read, size);
free(read);
}

lua_pushlstring(l, read, size);
return 1;
return nargs;
}


int luabuffer_writeByte(lua_State *l) {
ByteBuffer *buffer = luaL_checkudata(l, 1, METATABLE_BUFFER);
lua_pushinteger(l, buffer_writeByte(buffer, luaL_checkinteger(l, 2)));
Expand Down
2 changes: 1 addition & 1 deletion mumble/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ static int client_openAudio(lua_State *l) {
const char* filepath = luaL_checkstring(l, 2);
float volume = (float) luaL_optnumber(l, 3, 1);

SF_INFO info;
SF_INFO info = {0};
SNDFILE* file = sf_open(filepath, SFM_READ, &info);

if (!file) {
Expand Down
19 changes: 16 additions & 3 deletions mumble/mumble.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,26 +828,33 @@ static void mumble_client_cleanup(MumbleClient *client) {
if (uv_is_active((uv_handle_t*) &client->socket_udp)) {
uv_udp_recv_stop(&client->socket_udp);
}

uv_close((uv_handle_t*) &client->socket_udp, NULL);

if (uv_is_active((uv_handle_t*) &client->ssl_poll)) {
uv_poll_stop(&client->ssl_poll);
}

uv_close((uv_handle_t*) &client->socket_tcp, NULL);

if (uv_is_active((uv_handle_t*) &client->ping_timer)) {
uv_timer_stop(&client->ping_timer);
}

if (uv_is_active((uv_handle_t*) &client->audio_timer)) {
uv_timer_stop(&client->audio_timer);
}

uv_cancel((uv_req_t*)&client->tcp_connect_req);

LinkNode* current = client->stream_list;

if (current) {
// Cleanup any active audio transmissions
while (current != NULL) {
LinkNode* next = current->next;
audio_transmission_unreference(client->l, current->data);
current = current->next;
current = next;
}
}

Expand All @@ -856,8 +863,9 @@ static void mumble_client_cleanup(MumbleClient *client) {
if (current) {
// Cleanup our user objects
while (current != NULL) {
LinkNode* next = current->next;
mumble_user_remove(client, current->index);
current = current->next;
current = next;
}
list_clear(&client->user_list);
}
Expand All @@ -867,8 +875,9 @@ static void mumble_client_cleanup(MumbleClient *client) {
if (current) {
// Cleanup our channel objects
while (current != NULL) {
LinkNode* next = current->next;
mumble_channel_remove(client, current->index);
current = current->next;
current = next;
}
list_clear(&client->channel_list);
}
Expand Down Expand Up @@ -1654,6 +1663,10 @@ void mumble_init(lua_State *l) {
lua_setmetatable(l, -2);
lua_setfield(l, -2, "thread");

// https://publist.mumble.info/v1/list
// lua_pushcfunction(l, mumble_getPublicServerList)
// lua_setfield(l, -2, "getPublicServerList")

// Register encoder metatable
luaL_newmetatable(l, METATABLE_AUDIOSTREAM);
{
Expand Down
16 changes: 7 additions & 9 deletions mumble/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,29 @@ void on_send(uv_udp_send_t* req, int status) {
} else {
mumble_log(LOG_TRACE, "[UDP] Sent UDP packet successfully");
}
free(req);
}

int packet_sendudp(MumbleClient* client, const void *message, const int length) {
uint8_t encrypted[length + 4];
if (crypt_isValid(client->crypt) && crypt_encrypt(client->crypt, message, encrypted, length)) {
uv_buf_t buf = uv_buf_init((char*)encrypted, length + 4);

send_context_t* context = (send_context_t*)malloc(sizeof(send_context_t));
context->is_done = 0;
send_context_t context;
context.is_done = 0;

// Prepare the request
uv_udp_send_t* req = &context->req;
req->data = context;
// Prepare the request
uv_udp_send_t req;
req.data = &context;

int ret = uv_udp_send(req, &client->socket_udp, &buf, 1, NULL, on_send);
int ret = uv_udp_send(&req, &client->socket_udp, &buf, 1, NULL, on_send);

if (ret < 0) {
mumble_log(LOG_ERROR, "[UDP] Unable to send UDP packet: %s", uv_strerror(ret));
free(req);
return 0;
}

// Wait until the send operation is done (helps prevent the audio timer from stuttering)
while (!context->is_done) {
while (!context.is_done) {
uv_run(uv_default_loop(), UV_RUN_ONCE);
}

Expand Down
12 changes: 9 additions & 3 deletions mumble/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,15 @@ void luaL_debugstack(lua_State *l, const char* text) {
switch (t) {
case LUA_TSTRING: { /* strings */
const char* str = lua_tolstring(l, i, &len);
printf("\t%d - %s[\"", i, tname);
print_unescaped(str, len);
printf("\"]" NEWLINE);
if (len < 64) {
printf("\t%d - %s[\"", i, tname);
print_unescaped(str, len);
printf("\"]" NEWLINE);
print_unescaped(str, len);
printf("\"]" NEWLINE);
} else {
printf("\t%d - %s[%ld]" NEWLINE, i, tname, len);
}
break;
}
case LUA_TBOOLEAN: /* booleans */
Expand Down

0 comments on commit c860b25

Please sign in to comment.