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:
parent
fb09e52d77
commit
6316be8d0f
@ -374,21 +374,13 @@ static int conn_escape (lua_State *L) {
|
|||||||
int error;
|
int error;
|
||||||
int ret = 1;
|
int ret = 1;
|
||||||
luaL_Buffer b;
|
luaL_Buffer b;
|
||||||
#if defined(luaL_buffinitsize)
|
|
||||||
char *to = luaL_buffinitsize (L, &b, 2*len+1);
|
|
||||||
#else
|
|
||||||
char *to;
|
char *to;
|
||||||
luaL_buffinit (L, &b);
|
luaL_buffinit (L, &b);
|
||||||
to = luaL_prepbuffer (&b);
|
to = luaL_prepbuffsize (&b, 2*len+1);
|
||||||
#endif
|
|
||||||
len = PQescapeStringConn (conn->pg_conn, to, from, len, &error);
|
len = PQescapeStringConn (conn->pg_conn, to, from, len, &error);
|
||||||
if (error == 0) { /* success ! */
|
if (error == 0) { /* success ! */
|
||||||
#if defined(luaL_pushresultsize)
|
|
||||||
luaL_pushresultsize (&b, len);
|
|
||||||
#else
|
|
||||||
luaL_addsize (&b, len);
|
luaL_addsize (&b, len);
|
||||||
luaL_pushresult (&b);
|
luaL_pushresult (&b);
|
||||||
#endif
|
|
||||||
} else {
|
} else {
|
||||||
ret = luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn));
|
ret = luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user