From 406de2b3235c64cf6eca34867493e2f22c6d8338 Mon Sep 17 00:00:00 2001 From: Silvan Calarco Date: Mon, 14 Oct 2013 18:19:10 +0200 Subject: [PATCH] backend-sqlite3: correctly finalize sqlite3 prepared statements --- src/backend-sqlite3.c | 56 +++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 31 deletions(-) diff --git a/src/backend-sqlite3.c b/src/backend-sqlite3.c index 35b87e3..c89b32b 100644 --- a/src/backend-sqlite3.c +++ b/src/backend-sqlite3.c @@ -77,10 +77,11 @@ int SQLite_print_contents_subtree(sqlite3 *db, ft->numproviders); sqlite3_prepare_v2(db, sqlite3_query, -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, ft->name, -1, SQLITE_STATIC); - if (sqlite3_step(stmt) != SQLITE_DONE) { - fprintf(stderr, "ERROR: SQLite: %s (%s)\n", sqlite3_errmsg(db), sqlite3_query); + fprintf(stderr, "ERROR: sqlite3_step: %s (%s)\n", sqlite3_errmsg(db), sqlite3_query); + return 1; } + sqlite3_finalize(stmt); sqlite3_transaction_size += strlen(sqlite3_query); if (sqlite3_transaction_size >= sqlite3_transaction_maxsize) { @@ -128,13 +129,13 @@ int generateSQLite_files(struct configTag* ct, sqlite3 *db, int arch) { currpackage->id, currpackage->file[i]->id); if (sqlite3_prepare_v2(db, sqlite3_query, -1, &stmt, NULL)) { - fprintf(stderr, "ERROR: SQLite: (%s) %s\n", sqlite3_query, sqlite3_errmsg(db)); + fprintf(stderr, "ERROR: sqlite3_prepare_v2: %s (%s)\n", sqlite3_errmsg(db), sqlite3_query); } sqlite3_bind_text(stmt, 1, currpackage->name, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 2, currpackage->version, -1, SQLITE_STATIC); sqlite3_bind_text(stmt, 3, currpackage->release, -1, SQLITE_STATIC); if (sqlite3_step(stmt) != SQLITE_DONE) { - fprintf(stderr, "ERROR: SQLite: (%s) %s\n", sqlite3_query, sqlite3_errmsg(db)); + fprintf(stderr, "ERROR: sqlite3_step: %s (%s)\n", sqlite3_query, sqlite3_errmsg(db)); return 3; } sqlite3_finalize(stmt); @@ -159,33 +160,26 @@ int generateSQLite_provided(struct configTag* ct, sqlite3 *db, int arch) { int i; struct providedList* provided; -// for (i = 0; i < ARCHS_MAX && ct->arch[i]; i++) { - snprintf(sqlite3_query, PATH_MAX, "provided"); - SQLite_init_table(db, sqlite3_query, SQLITE_TABLE_provided); - SQLite_begin_transaction(db); + snprintf(sqlite3_query, PATH_MAX, "provided"); + SQLite_init_table(db, sqlite3_query, SQLITE_TABLE_provided); + SQLite_begin_transaction(db); - provided = ct->providedlist_idx[arch][0]; - while (provided) { - snprintf(sqlite3_query, PATH_MAX, "INSERT INTO provided VALUES(%d,?,%d,%d);", - provided->id, - provided->flags, - provided->numproviders); - - sqlite3_prepare_v2(db, sqlite3_query, -1, &stmt, NULL); - sqlite3_bind_text(stmt, 1, provided->name, -1, SQLITE_STATIC); - - if (sqlite3_step(stmt) != SQLITE_DONE) { - fprintf(stderr, "ERROR: SQLite: (%s) %s\n", sqlite3_query, sqlite3_errmsg(db)); - } - provided = provided->next; + provided = ct->providedlist_idx[arch][0]; + while (provided) { + snprintf(sqlite3_query, PATH_MAX, "INSERT INTO provided VALUES(%d,?,%d,%d);", + provided->id, + provided->flags, + provided->numproviders); + sqlite3_prepare_v2(db, sqlite3_query, -1, &stmt, NULL); + sqlite3_bind_text(stmt, 1, provided->name, -1, SQLITE_STATIC); + if (sqlite3_step(stmt) != SQLITE_DONE) { + fprintf(stderr, "ERROR: SQLite: (%s) %s\n", sqlite3_query, sqlite3_errmsg(db)); } + sqlite3_finalize(stmt); + provided = provided->next; + } - SQLite_commit_transaction(db); - -/* if (sqlite3_finalize(stmt)) { - fprintf(stderr, "ERROR: SQLite: (%s) %s", sqlite3_query, sqlite3_errmsg(db)); - }*/ -// } + SQLite_commit_transaction(db); return 0; } @@ -522,12 +516,12 @@ generateSQLiteFiles(struct configTag *ct) unlink(dbname); if (sqlite3_open_v2(dbname, &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL)) { - logmsg(LOG_ERROR, "sqlite3_open_v2: %s %s", sqlite3_errmsg(db), dbname); + logmsg(LOG_ERROR, "sqlite3_open_v2: %s (%s)", dbname, sqlite3_errmsg(db)); if (db) sqlite3_close(db); return 1; } - generateSQLite_files(ct, db, i); - sqlite3_close(db); + if (generateSQLite_files(ct, db, i)) return 1; + if (db) sqlite3_close(db); } return 0; }