webmcp
changeset 412:7d43be9afa56
Improved memory cleanup in case of out-of-memory errors (PQnotifies and PQunescapeBytea)
author | jbe |
---|---|
date | Fri Jan 08 03:10:33 2016 +0100 (2016-01-08) |
parents | 02a53308f008 |
children | a09a0defb48a |
files | libraries/mondelefant/mondelefant_native.c |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.c Thu Jan 07 17:44:32 2016 +0100 1.2 +++ b/libraries/mondelefant/mondelefant_native.c Fri Jan 08 03:10:33 2016 +0100 1.3 @@ -471,13 +471,24 @@ 1.4 lua_setfield(L, 2, "message"); 1.5 return 1; 1.6 } 1.7 + // avoid cumulating memory leaks in case of previous out-of-memory errors: 1.8 + if (conn->todo_PQfreemem) { 1.9 + PQfreemem(conn->todo_PQfreemem); 1.10 + conn->todo_PQfreemem = NULL; 1.11 + } 1.12 notify = PQnotifies(conn->pgconn); 1.13 if (notify) { 1.14 + // ensure call of PQfreemem in case of out-of-memory errors: 1.15 + conn->todo_PQfreemem = notify; 1.16 + // do Lua operations: 1.17 lua_pushnil(L); 1.18 lua_pushstring(L, notify->relname); 1.19 lua_pushstring(L, notify->extra); 1.20 lua_pushinteger(L, notify->be_pid); 1.21 + // free memory allocated by PQnotifies: 1.22 PQfreemem(notify); 1.23 + // avoid double call of PQfreemem later: 1.24 + conn->todo_PQfreemem = NULL; 1.25 return 4; 1.26 } 1.27 } 1.28 @@ -628,7 +639,7 @@ 1.29 return luaL_error(L, "Could not allocate memory for binary quoting."); 1.30 } 1.31 } 1.32 - // ensure call of PQfreemem in case of repeated out-of-memory errors: 1.33 + // ensure call of PQfreemem in case of out-of-memory errors: 1.34 conn->todo_PQfreemem = output; 1.35 // create Lua string enclosed by single quotes: 1.36 luaL_buffinit(L, &buf); 1.37 @@ -947,7 +958,7 @@ 1.38 pgstatus = PQresultStatus(res); 1.39 rows = PQntuples(res); 1.40 cols = PQnfields(res); 1.41 - // ensure call of PQclear in case of repeated Lua errors: 1.42 + // ensure call of PQclear in case of Lua errors: 1.43 conn->todo_PQclear = res; 1.44 } 1.45 } 1.46 @@ -1170,6 +1181,12 @@ 1.47 } else if (binary[col]) { 1.48 size_t binlen; 1.49 char *binval; 1.50 + // avoid cumulating memory leaks in case of previous out-of-memory errors: 1.51 + if (conn->todo_PQfreemem) { 1.52 + PQfreemem(conn->todo_PQfreemem); 1.53 + conn->todo_PQfreemem = NULL; 1.54 + } 1.55 + // Unescape binary data: 1.56 binval = (char *)PQunescapeBytea( 1.57 (unsigned char *)PQgetvalue(res, row, col), &binlen 1.58 ); 1.59 @@ -1178,8 +1195,14 @@ 1.60 "Could not allocate memory for binary unescaping." 1.61 ); 1.62 } 1.63 + // ensure call of PQfreemem in case of out-of-memory error: 1.64 + conn->todo_PQfreemem = binval; 1.65 + // create Lua string: 1.66 lua_pushlstring(L, binval, binlen); 1.67 + // free memory allocated by PQunescapeBytea: 1.68 PQfreemem(binval); 1.69 + // avoid double call of PQfreemem later: 1.70 + conn->todo_PQfreemem = NULL; 1.71 } else { 1.72 lua_pushstring(L, PQgetvalue(res, row, col)); 1.73 } 1.74 @@ -1194,6 +1217,12 @@ 1.75 } else if (binary[col]) { 1.76 size_t binlen; 1.77 char *binval; 1.78 + // avoid cumulating memory leaks in case of previous out-of-memory errors: 1.79 + if (conn->todo_PQfreemem) { 1.80 + PQfreemem(conn->todo_PQfreemem); 1.81 + conn->todo_PQfreemem = NULL; 1.82 + } 1.83 + // Unescape binary data: 1.84 binval = (char *)PQunescapeBytea( 1.85 (unsigned char *)PQgetvalue(res, 0, col), &binlen 1.86 ); 1.87 @@ -1202,8 +1231,14 @@ 1.88 "Could not allocate memory for binary unescaping." 1.89 ); 1.90 } 1.91 + // ensure call of PQfreemem in case of out-of-memory error: 1.92 + conn->todo_PQfreemem = binval; 1.93 + // create Lua string: 1.94 lua_pushlstring(L, binval, binlen); 1.95 + // free memory allocated by PQunescapeBytea: 1.96 PQfreemem(binval); 1.97 + // avoid double call of PQfreemem later: 1.98 + conn->todo_PQfreemem = NULL; 1.99 } else { 1.100 lua_pushstring(L, PQgetvalue(res, 0, col)); 1.101 }