Skip to content

Commit

Permalink
Bug correction: strings bigger than the buffer default size are corre…
Browse files Browse the repository at this point in the history
…ctly handled.
  • Loading branch information
tomasguisasola committed Oct 30, 2016
1 parent bed6e06 commit b83ddf1
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/ls_postgres.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,15 +375,33 @@ static int conn_escape (lua_State *L) {
int ret = 1;
luaL_Buffer b;
char *to;
#if !defined(LUA_VERSION_NUM) || (LUA_VERSION_NUM == 501)
/* Lua 5.0 and 5.1 */
luaL_buffinit (L, &b);
to = luaL_prepbuffsize (&b, 2*len+1);
do {
int max = LUAL_BUFFERSIZE / 2;
size_t bytes_copied;
size_t this_len = (len > max) ? max : len;
to = luaL_prepbuffer (&b);
bytes_copied = PQescapeStringConn (conn->pg_conn, to, from, this_len, &error);
if (error != 0) { /* failed ! */
return luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn));
}
luaL_addsize (&b, bytes_copied);
from += this_len;
len -= this_len;
} while (len > 0);
luaL_pushresult (&b);
#else
/* Lua 5.2 and 5.3 */
to = luaL_buffinitsize (L, &b, 2*len+1);
len = PQescapeStringConn (conn->pg_conn, to, from, len, &error);
if (error == 0) { /* success ! */
luaL_addsize (&b, len);
luaL_pushresult (&b);
luaL_pushresultsize (&b, len);
} else {
ret = luasql_failmsg (L, "cannot escape string. PostgreSQL: ", PQerrorMessage (conn->pg_conn));
}
#endif
return ret;
}

Expand Down

0 comments on commit b83ddf1

Please sign in to comment.