# HG changeset patch # User jbe # Date 1452117579 -3600 # Node ID 0e641ac7664754a8d99e436d72945376933ce1f2 # Parent 36f0d1d5ed7dc68f113fb323491ba7506ad75d05 Some code-cleanup and fixed comments; Better behavior on out-of-memory errors in certain cases diff -r 36f0d1d5ed7d -r 0e641ac76647 libraries/mondelefant/mondelefant_native.c --- a/libraries/mondelefant/mondelefant_native.c Wed Jan 06 18:59:58 2016 +0100 +++ b/libraries/mondelefant/mondelefant_native.c Wed Jan 06 22:59:39 2016 +0100 @@ -255,11 +255,11 @@ if (!conn->pgconn) { lua_gc(L, LUA_GCCOLLECT, 0); conn->pgconn = PQconnectdb(conninfo); + // throw error in case of (unexpected) error of PQconnectdb call: + if (!conn->pgconn) return luaL_error(L, + "Error in libpq while creating 'PGconn' structure." + ); } - // throw error in case of (unexpected) error of PQconnectdb call: - if (!conn->pgconn) return luaL_error(L, - "Error in libpq while creating 'PGconn' structure." - ); // set metatable for userdata (ensure PQfinish on unexpected error below): luaL_setmetatable(L, MONDELEFANT_CONN_MT_REGKEY); // check result of PQconnectdb call: @@ -559,9 +559,10 @@ mondelefant_conn_t *conn; const char *input; size_t input_len; + luaL_Buffer buf; char *output; size_t output_len; - // get 'conn' attribute of C-struct of database connection: + // get database connection object: conn = mondelefant_get_conn(L, 1); // get second argument, which must be a string: input = luaL_checklstring(L, 2, &input_len); @@ -570,10 +571,7 @@ return luaL_error(L, "String to be escaped is too long."); } // allocate memory for quoted string: - output = malloc((2 * input_len + 3) * sizeof(char)); - if (!output) { - return luaL_error(L, "Could not allocate memory for string quoting."); - } + output = luaL_buffinitsize(L, &buf, (2 * input_len + 3) * sizeof(char)); // do escaping by calling PQescapeStringConn and enclosing result with // single quotes: output[0] = '\''; @@ -583,9 +581,7 @@ output[output_len + 1] = '\''; output[output_len + 2] = 0; // create Lua string: - lua_pushlstring(L, output, output_len + 2); - // free allocated memory: - free(output); + luaL_addsize(&buf, output_len + 2); // return Lua string: return 1; } @@ -598,7 +594,7 @@ char *output; size_t output_len; luaL_Buffer buf; - // get 'conn' attribute of C-struct of database connection: + // get database connection object: conn = mondelefant_get_conn(L, 1); // get second argument, which must be a string: input = luaL_checklstring(L, 2, &input_len); @@ -629,7 +625,7 @@ const char *template; size_t template_pos = 0; luaL_Buffer buf; - // get 'conn' attribute of C-struct of database connection: + // get database connection object: conn = mondelefant_get_conn(L, 1); // if second argument is a string, return this string: if (lua_type(L, 2) == LUA_TSTRING) { @@ -847,7 +843,7 @@ int sent_success; PGresult *res; int rows, cols, row, col; - // get 'conn' attribute of C-struct of database connection: + // get database connection object: conn = mondelefant_get_conn(L, 1); // calculate number of commands (2 arguments for one command): command_count = lua_gettop(L) / 2;