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.
This commit is contained in:
Peter Melnichenko 2016-09-16 16:23:52 +03:00
parent fb09e52d77
commit 6316be8d0f

View File

@ -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));
}