diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-13 10:58:41 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2023-05-13 11:37:46 +0200 |
commit | cd917f48454875ad6b7fc69455281d72760c44ee (patch) | |
tree | 70a7a43fe43b7f893468f9120def5513774a242c /src/sqlite | |
parent | add command module to handle request-response communications (diff) | |
download | cimple-cd917f48454875ad6b7fc69455281d72760c44ee.tar.gz cimple-cd917f48454875ad6b7fc69455281d72760c44ee.zip |
best practices & coding style fixes
* I don't really need to declare all variables at the top of the
function anymore.
* Default-initialize variables more.
* Don't set the output parameter until the object is completely
constructed.
Diffstat (limited to '')
-rw-r--r-- | src/sqlite.c | 45 |
1 files changed, 20 insertions, 25 deletions
diff --git a/src/sqlite.c b/src/sqlite.c index 0c58b0e..9134528 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -138,13 +138,11 @@ int sqlite_column_int(sqlite3_stmt *stmt, int index) return sqlite3_column_int(stmt, index); } -int sqlite_column_text(sqlite3_stmt *stmt, int index, char **result) +int sqlite_column_text(sqlite3_stmt *stmt, int index, char **_result) { - const unsigned char *value; - size_t nb; int ret = 0; - value = sqlite3_column_text(stmt, index); + const unsigned char *value = sqlite3_column_text(stmt, index); if (!value) { ret = sqlite3_errcode(sqlite3_db_handle(stmt)); if (ret) { @@ -152,30 +150,29 @@ int sqlite_column_text(sqlite3_stmt *stmt, int index, char **result) return ret; } - *result = NULL; + *_result = NULL; return 0; } ret = sqlite3_column_bytes(stmt, index); - nb = (size_t)ret; + size_t nb = (size_t)ret; - *result = calloc(nb + 1, 1); - if (!*result) { + char *result = calloc(nb + 1, 1); + if (!result) { log_errno("calloc"); return -1; } + memcpy(result, value, nb); - memcpy(*result, value, nb); + *_result = result; return 0; } -int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **result) +int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) { - const unsigned char *value; - size_t nb; int ret = 0; - value = sqlite3_column_blob(stmt, index); + const unsigned char *value = sqlite3_column_blob(stmt, index); if (!value) { ret = sqlite3_errcode(sqlite3_db_handle(stmt)); if (ret) { @@ -183,36 +180,34 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **result) return ret; } - *result = NULL; + *_result = NULL; return 0; } ret = sqlite3_column_bytes(stmt, index); - nb = (size_t)ret; + size_t nb = (size_t)ret; - *result = malloc(nb); - if (!*result) { + unsigned char *result = malloc(nb); + if (!result) { log_errno("malloc"); return -1; } + memcpy(result, value, nb); - memcpy(*result, value, nb); + *_result = result; return 0; } int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt) { static const char *const FMT = "BEGIN; %s COMMIT;"; - - char *full_stmt; - size_t nb; int ret = 0; ret = snprintf(NULL, 0, FMT, stmt); - nb = (size_t)ret + 1; + size_t nb = (size_t)ret + 1; ret = 0; - full_stmt = malloc(nb); + char *full_stmt = malloc(nb); if (!full_stmt) { log_errno("malloc"); return -1; @@ -230,8 +225,8 @@ free: int sqlite_get_user_version(sqlite3 *db, unsigned int *output) { - sqlite3_stmt *stmt; - int result, ret = 0; + sqlite3_stmt *stmt = NULL; + int result = -1, ret = 0; ret = sqlite_prepare(db, "PRAGMA user_version;", &stmt); if (ret < 0) |