From 6316be8d0fa241657b4d337f29c4b740452c2dc9 Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Fri, 16 Sep 2016 16:23:52 +0300 Subject: [PATCH] luasql.postres: fix buffer allocation for conn:escape result `luaL_buffinitsize(L, B, sz)` is equivalent to `luaL_buffinit(L, B); luaL_prepbuffsize(B, sz)`, not `luaL_buffinit(L, B); luaL_prepbuffer(B)`. The latter uses `LUAL_BUFFERSIZE` (8192 by default) as buffer size, which may be not enough, causing a segfault. Additionally, detection of availability of `luaL_buffinitsize` and `luaL_pushresultsize` was broken: these functions are not macros, so `#if defined(...)` does not work. Always use fallback implementations instead; they are short and the functions are only used once. Ref #55. --- src/ls_postgres.c | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/ls_postgres.c b/src/ls_postgres.c index 06569b5..d3ed790 100644 --- a/src/ls_postgres.c +++ b/src/ls_postgres.c @@ -374,21 +374,13 @@ static int conn_escape (lua_State *L) { int error; int ret = 1; luaL_Buffer b; -#if defined(luaL_buffinitsize) - char *to = luaL_buffinitsize (L, &b, 2*len+1); -#else char *to; luaL_buffinit (L, &b); - to = luaL_prepbuffer (&b); -#endif + to = luaL_prepbuffsize (&b, 2*len+1); len = PQescapeStringConn (conn->pg_conn, to, from, len, &error); if (error == 0) { /* success ! */ -#if defined(luaL_pushresultsize) - luaL_pushresultsize (&b, len); -#else luaL_addsize (&b, len); luaL_pushresult (&b); -#endif } else { ret = luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn)); }