Skip to content

Commit e739d88

Browse files
committed
Fix crash when enabling debugger under Linux
Rename buffer_init() to avoid clashing with a function with the same name in luasocket code: under Linux, due to the of "flat" ELF namespaces, when luasocket shared library is loaded into premake process the existing premake function is used instead of the function defined in luasocket code, resulting in luasocket buffer not being properly initialized which, in turn, leads to mysterious crashes as soon as it's used, e.g. dereferencing null timeout field: (gdb) bt #0 0x00007ffff7cce642 in timeout_markstart (tm=0x0) at ../../binmodules/luasocket/src/timeout.c:116 #1 0x00007ffff7cc7618 in buffer_meth_receive (L=0x5555557882a8, buf=0x5555559044a0) at ../../binmodules/luasocket/src/buffer.c:113 #2 0x00007ffff7ccd545 in meth_receive (L=0x5555557882a8) at ../../binmodules/luasocket/src/tcp.c:135 #3 0x000055555558c1cc in luaD_precall (L=0x5555557882a8, func=0x5555558e36b0, nresults=1) at ../../contrib/lua/src/ldo.c:434 #4 0x00005555555a971f in luaV_execute (L=0x5555557882a8) at ../../contrib/lua/src/lvm.c:1134 #5 0x000055555558c555 in luaD_call (L=0x5555557882a8, func=0x5555558e3640, nResults=0) at ../../contrib/lua/src/ldo.c:499 premake#6 0x000055555558c5b3 in luaD_callnoyield (L=0x5555557882a8, func=0x5555558e3640, nResults=0) at ../../contrib/lua/src/ldo.c:509 premake#7 0x0000555555588af5 in lua_callk (L=0x5555557882a8, nargs=2, nresults=0, ctx=0, k=0x0) at ../../contrib/lua/src/lapi.c:925 premake#8 0x00005555555af4f2 in hookf (L=0x5555557882a8, ar=0x7fffffffd270) at ../../contrib/lua/src/ldblib.c:316 premake#9 0x000055555558bafb in luaD_hook (L=0x5555557882a8, event=2, line=273) at ../../contrib/lua/src/ldo.c:269 premake#10 0x000055555558b284 in luaG_traceexec (L=0x5555557882a8) at ../../contrib/lua/src/ldebug.c:687 premake#11 0x00005555555a6b71 in luaV_execute (L=0x5555557882a8) at ../../contrib/lua/src/lvm.c:801 premake#12 0x000055555558c555 in luaD_call (L=0x5555557882a8, func=0x555555889360, nResults=1) at ../../contrib/lua/src/ldo.c:499 premake#13 0x000055555558c5b3 in luaD_callnoyield (L=0x5555557882a8, func=0x555555889360, nResults=1) at ../../contrib/lua/src/ldo.c:509 premake#14 0x0000555555588b60 in f_call (L=0x5555557882a8, ud=0x7fffffffdbb0) at ../../contrib/lua/src/lapi.c:943 premake#15 0x000055555558b5a5 in luaD_rawrunprotected (L=0x5555557882a8, f=0x555555588b2b <f_call>, ud=0x7fffffffdbb0) at ../../contrib/lua/src/ldo.c:142 premake#16 0x000055555558cd85 in luaD_pcall (L=0x5555557882a8, func=0x555555588b2b <f_call>, u=0x7fffffffdbb0, old_top=64, ef=48) at ../../contrib/lua/src/ldo.c:729 premake#17 0x0000555555588c28 in lua_pcallk (L=0x5555557882a8, nargs=0, nresults=1, errfunc=3, ctx=0, k=0x0) at ../../contrib/lua/src/lapi.c:969 premake#18 0x0000555555584734 in premake_pcall (L=0x5555557882a8, nargs=0, nresults=1) at ../../src/host/premake.c:287 premake#19 0x0000555555584867 in premake_execute (L=0x5555557882a8, argc=5, argv=0x7fffffffdd98, script=0x555555685052 "src/_premake_main.lua") at ../../src/host/premake.c:316 premake#20 0x000055555558535e in main (argc=5, argv=0x7fffffffdd98) at ../../src/host/premake_main.c:19 For consistency, rename all the other buffer_xxx functions too, even though they don't conflict with anything right now.
1 parent 703ff07 commit e739d88

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/host/buffered_io.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
#include "premake.h"
1010
#include "buffered_io.h"
1111

12-
void buffer_init(Buffer* b)
12+
void premake_buffer_init(Buffer* b)
1313
{
1414
b->capacity = 0;
1515
b->length = 0;
1616
b->data = NULL;
1717
}
1818

19-
void buffer_destroy(Buffer* b)
19+
void premake_buffer_destroy(Buffer* b)
2020
{
2121
free(b->data);
2222
b->capacity = 0;
2323
b->length = 0;
2424
b->data = NULL;
2525
}
2626

27-
void buffer_puts(Buffer* b, const void* ptr, size_t len)
27+
void premake_buffer_puts(Buffer* b, const void* ptr, size_t len)
2828
{
2929
char* data;
3030

@@ -53,23 +53,23 @@ void buffer_puts(Buffer* b, const void* ptr, size_t len)
5353
b->length += len;
5454
}
5555

56-
void buffer_printf(Buffer* b, const char *fmt, ...)
56+
void premake_buffer_printf(Buffer* b, const char *fmt, ...)
5757
{
5858
char text[2048];
5959
int len;
6060
va_list args;
6161
va_start(args, fmt);
6262
len = vsnprintf(text, sizeof(text) - 1, fmt, args);
6363
va_end(args);
64-
buffer_puts(b, text, len);
64+
premake_buffer_puts(b, text, len);
6565
}
6666

6767
// -- Lua wrappers ----------------------------------------
6868

6969
int buffered_new(lua_State* L)
7070
{
7171
Buffer* b = (Buffer*)malloc(sizeof(Buffer));
72-
buffer_init(b);
72+
premake_buffer_init(b);
7373
lua_pushlightuserdata(L, b);
7474
return 1;
7575
}
@@ -80,7 +80,7 @@ int buffered_write(lua_State* L)
8080
const char *s = luaL_checklstring(L, 2, &len);
8181
Buffer* b = (Buffer*)lua_touserdata(L, 1);
8282

83-
buffer_puts(b, s, len);
83+
premake_buffer_puts(b, s, len);
8484
return 0;
8585
}
8686

@@ -91,15 +91,15 @@ int buffered_writeln(lua_State* L)
9191
Buffer* b = (Buffer*)lua_touserdata(L, 1);
9292

9393
if (s != NULL)
94-
buffer_puts(b, s, len);
95-
buffer_puts(b, "\r\n", 2);
94+
premake_buffer_puts(b, s, len);
95+
premake_buffer_puts(b, "\r\n", 2);
9696
return 0;
9797
}
9898

9999
int buffered_close(lua_State* L)
100100
{
101101
Buffer* b = (Buffer*)lua_touserdata(L, 1);
102-
buffer_destroy(b);
102+
premake_buffer_destroy(b);
103103
free(b);
104104
return 0;
105105
}

src/host/buffered_io.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ typedef struct struct_Buffer
1515
char* data;
1616
} Buffer;
1717

18-
void buffer_init(Buffer* b);
19-
void buffer_destroy(Buffer* b);
18+
void premake_buffer_init(Buffer* b);
19+
void premake_buffer_destroy(Buffer* b);
2020

21-
void buffer_puts(Buffer* b, const void* ptr, size_t len);
22-
void buffer_printf(Buffer* b, const char* s, ...);
21+
void premake_buffer_puts(Buffer* b, const void* ptr, size_t len);
22+
void premake_buffer_printf(Buffer* b, const char* s, ...);
2323

2424
#endif

src/host/curl_utils.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int curlProgressCallback(curl_state* state, double dltotal, double dlnow, double
3535
size_t curlWriteCallback(char *ptr, size_t size, size_t nmemb, curl_state* state)
3636
{
3737
size_t length = size * nmemb;
38-
buffer_puts(&state->S, ptr, length);
38+
premake_buffer_puts(&state->S, ptr, length);
3939
return length;
4040
}
4141

@@ -74,7 +74,7 @@ CURL* curlRequest(lua_State* L, curl_state* state, int optionsIndex, int progres
7474
state->RefIndex = 0;
7575
state->errorBuffer[0] = '\0';
7676
state->headers = NULL;
77-
buffer_init(&state->S);
77+
premake_buffer_init(&state->S);
7878

7979
curl_init();
8080
curl = curl_easy_init();

src/host/http_download.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int http_download(lua_State* L)
6868
lua_pushstring(L, "OK");
6969
}
7070

71-
buffer_destroy(&state.S);
71+
premake_buffer_destroy(&state.S);
7272
lua_pushnumber(L, (lua_Number)responseCode);
7373
return 2;
7474
}

src/host/http_get.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int http_get(lua_State* L)
5151
lua_pushstring(L, "OK");
5252
}
5353

54-
buffer_destroy(&state.S);
54+
premake_buffer_destroy(&state.S);
5555
lua_pushnumber(L, (lua_Number)responseCode);
5656
return 3;
5757
}

src/host/http_post.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int http_post(lua_State* L)
4949
lua_pushstring(L, "OK");
5050
}
5151

52-
buffer_destroy(&state.S);
52+
premake_buffer_destroy(&state.S);
5353
lua_pushnumber(L, (lua_Number)responseCode);
5454
return 3;
5555
}

0 commit comments

Comments
 (0)