diff --git a/src/lua_rds.c b/src/lua_rds.c index 18d186c..787fa7f 100644 --- a/src/lua_rds.c +++ b/src/lua_rds.c @@ -549,16 +549,16 @@ void init_lua(RDSModulator* rds_mod) { } } -void run_lua(char *str, char *cmd_output) { +void run_lua(char *str, char *cmd_output, size_t* out_len) { pthread_mutex_lock(&lua_mutex); lua_getglobal(L, "data_handle"); if (lua_isfunction(L, -1)) { lua_pushstring(L, str); if (lua_pcall(L, 1, 1, 0) == LUA_OK) { - if (lua_isstring(L, -1) && cmd_output) _strncpy(cmd_output, lua_tostring(L, -1), 254); + if (lua_isstring(L, -1) && cmd_output) _strncpy(cmd_output, lua_tolstring(L, -1, out_len), 254); } else fprintf(stderr, "Lua error: %s at 'data_handle'\n", lua_tostring(L, -1)); - } else if (lua_isstring(L, -1) && cmd_output) _strncpy(cmd_output, lua_tostring(L, -1), 254); + } else if (lua_isstring(L, -1) && cmd_output) _strncpy(cmd_output, lua_tolstring(L, -1, out_len), 254); lua_pop(L, 1); pthread_mutex_unlock(&lua_mutex); } diff --git a/src/lua_rds.h b/src/lua_rds.h index e839ae4..ba267f8 100644 --- a/src/lua_rds.h +++ b/src/lua_rds.h @@ -7,7 +7,7 @@ #include "modulator.h" void init_lua(RDSModulator* rds_mod); -void run_lua(char *str, char *cmd_output); +void run_lua(char *str, char *cmd_output, size_t* out_len); int lua_group(RDSGroup* group, const char grp); int lua_rds2_group(RDSGroup* group, int stream); void lua_call_function_nolock(const char* function); diff --git a/src/udp_server.c b/src/udp_server.c index 03c2e56..4a6d177 100644 --- a/src/udp_server.c +++ b/src/udp_server.c @@ -58,9 +58,9 @@ void poll_udp_server() { strncpy(cmd_buf, token, BUF_SIZE - 1); memset(cmd_output, 0, BUF_SIZE); - run_lua(cmd_buf, cmd_output); - size_t out_len = strlen(cmd_output); + size_t out_len = 0; + run_lua(cmd_buf, cmd_output, &out_len); if (out_len > 0 && sendto(sockfd, cmd_output, out_len, 0, (struct sockaddr *)&client_addr, client_len) == -1) perror("sendto"); // no walrus } token = strtok(NULL, "\r\n");