webmcp

changeset 407:0e641ac76647

Some code-cleanup and fixed comments; Better behavior on out-of-memory errors in certain cases
author jbe
date Wed Jan 06 22:59:39 2016 +0100 (2016-01-06)
parents 36f0d1d5ed7d
children fd5880964d99
files libraries/mondelefant/mondelefant_native.c
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.c	Wed Jan 06 18:59:58 2016 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_native.c	Wed Jan 06 22:59:39 2016 +0100
     1.3 @@ -255,11 +255,11 @@
     1.4    if (!conn->pgconn) {
     1.5      lua_gc(L, LUA_GCCOLLECT, 0);
     1.6      conn->pgconn = PQconnectdb(conninfo);
     1.7 +    // throw error in case of (unexpected) error of PQconnectdb call:
     1.8 +    if (!conn->pgconn) return luaL_error(L,
     1.9 +      "Error in libpq while creating 'PGconn' structure."
    1.10 +    );
    1.11    }
    1.12 -  // throw error in case of (unexpected) error of PQconnectdb call:
    1.13 -  if (!conn->pgconn) return luaL_error(L,
    1.14 -    "Error in libpq while creating 'PGconn' structure."
    1.15 -  );
    1.16    // set metatable for userdata (ensure PQfinish on unexpected error below):
    1.17    luaL_setmetatable(L, MONDELEFANT_CONN_MT_REGKEY);
    1.18    // check result of PQconnectdb call:
    1.19 @@ -559,9 +559,10 @@
    1.20    mondelefant_conn_t *conn;
    1.21    const char *input;
    1.22    size_t input_len;
    1.23 +  luaL_Buffer buf;
    1.24    char *output;
    1.25    size_t output_len;
    1.26 -  // get 'conn' attribute of C-struct of database connection:
    1.27 +  // get database connection object:
    1.28    conn = mondelefant_get_conn(L, 1);
    1.29    // get second argument, which must be a string:
    1.30    input = luaL_checklstring(L, 2, &input_len);
    1.31 @@ -570,10 +571,7 @@
    1.32      return luaL_error(L, "String to be escaped is too long.");
    1.33    }
    1.34    // allocate memory for quoted string:
    1.35 -  output = malloc((2 * input_len + 3) * sizeof(char));
    1.36 -  if (!output) {
    1.37 -    return luaL_error(L, "Could not allocate memory for string quoting.");
    1.38 -  }
    1.39 +  output = luaL_buffinitsize(L, &buf, (2 * input_len + 3) * sizeof(char));
    1.40    // do escaping by calling PQescapeStringConn and enclosing result with
    1.41    // single quotes:
    1.42    output[0] = '\'';
    1.43 @@ -583,9 +581,7 @@
    1.44    output[output_len + 1] = '\'';
    1.45    output[output_len + 2] = 0;
    1.46    // create Lua string:
    1.47 -  lua_pushlstring(L, output, output_len + 2);
    1.48 -  // free allocated memory:
    1.49 -  free(output);
    1.50 +  luaL_addsize(&buf, output_len + 2);
    1.51    // return Lua string:
    1.52    return 1;
    1.53  }
    1.54 @@ -598,7 +594,7 @@
    1.55    char *output;
    1.56    size_t output_len;
    1.57    luaL_Buffer buf;
    1.58 -  // get 'conn' attribute of C-struct of database connection:
    1.59 +  // get database connection object:
    1.60    conn = mondelefant_get_conn(L, 1);
    1.61    // get second argument, which must be a string:
    1.62    input = luaL_checklstring(L, 2, &input_len);
    1.63 @@ -629,7 +625,7 @@
    1.64    const char *template;
    1.65    size_t template_pos = 0;
    1.66    luaL_Buffer buf;
    1.67 -  // get 'conn' attribute of C-struct of database connection:
    1.68 +  // get database connection object:
    1.69    conn = mondelefant_get_conn(L, 1);
    1.70    // if second argument is a string, return this string:
    1.71    if (lua_type(L, 2) == LUA_TSTRING) {
    1.72 @@ -847,7 +843,7 @@
    1.73    int sent_success;
    1.74    PGresult *res;
    1.75    int rows, cols, row, col;
    1.76 -  // get 'conn' attribute of C-struct of database connection:
    1.77 +  // get database connection object:
    1.78    conn = mondelefant_get_conn(L, 1);
    1.79    // calculate number of commands (2 arguments for one command):
    1.80    command_count = lua_gettop(L) / 2;

Impressum / About Us