Skip to content

Commit 0f3d301

Browse files
authored
Merge pull request #136 from scitokens/sqlite3_err_handling
Improve error handling around the sqlite3 library
2 parents 892076d + cf5b5cc commit 0f3d301

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

src/scitokens_cache.cpp

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -95,38 +95,53 @@ std::string get_cache_file() {
9595
return keycache_file;
9696
}
9797

98-
void remove_issuer_entry(sqlite3 *db, const std::string &issuer,
99-
bool new_transaction) {
100-
101-
if (new_transaction)
102-
sqlite3_exec(db, "BEGIN", 0, 0, 0);
98+
// Remove a given issuer from the database. Starts a new transaction
99+
// if `new_transaction` is true.
100+
// If a failure occurs, then this function returns nonzero and closes
101+
// the database handle.
102+
int remove_issuer_entry(sqlite3 *db, const std::string &issuer,
103+
bool new_transaction) {
104+
105+
int rc;
106+
if (new_transaction) {
107+
if ((rc = sqlite3_exec(db, "BEGIN", 0, 0, 0)) != SQLITE_OK) {
108+
sqlite3_close(db);
109+
return -1;
110+
}
111+
}
103112

104113
sqlite3_stmt *stmt;
105-
int rc = sqlite3_prepare_v2(db, "DELETE FROM keycache WHERE issuer = ?", -1,
106-
&stmt, NULL);
114+
rc = sqlite3_prepare_v2(db, "DELETE FROM keycache WHERE issuer = ?", -1,
115+
&stmt, NULL);
107116
if (rc != SQLITE_OK) {
108117
sqlite3_close(db);
109-
return;
118+
return -1;
110119
}
111120

112121
if (sqlite3_bind_text(stmt, 1, issuer.c_str(), issuer.size(),
113122
SQLITE_STATIC) != SQLITE_OK) {
114123
sqlite3_finalize(stmt);
115124
sqlite3_close(db);
116-
return;
125+
return -1;
117126
}
118127

119128
rc = sqlite3_step(stmt);
120129
if (rc != SQLITE_DONE) {
121130
sqlite3_finalize(stmt);
122131
sqlite3_close(db);
123-
return;
132+
return -1;
124133
}
125134

126135
sqlite3_finalize(stmt);
127136

128-
if (new_transaction)
129-
sqlite3_exec(db, "COMMIT", 0, 0, 0);
137+
if (new_transaction) {
138+
if ((rc = sqlite3_exec(db, "COMMIT", 0, 0, 0)) != SQLITE_OK) {
139+
sqlite3_close(db);
140+
return -1;
141+
}
142+
}
143+
144+
return 0;
130145
}
131146

132147
} // namespace
@@ -170,27 +185,35 @@ bool scitokens::Validator::get_public_keys_from_db(const std::string issuer,
170185
picojson::value json_obj;
171186
auto err = picojson::parse(json_obj, metadata);
172187
if (!err.empty() || !json_obj.is<picojson::object>()) {
173-
remove_issuer_entry(db, issuer, true);
188+
if (remove_issuer_entry(db, issuer, true) != 0) {
189+
return false;
190+
}
174191
sqlite3_close(db);
175192
return false;
176193
}
177194
auto top_obj = json_obj.get<picojson::object>();
178195
auto iter = top_obj.find("jwks");
179196
if (iter == top_obj.end() || !iter->second.is<picojson::object>()) {
180-
remove_issuer_entry(db, issuer, true);
197+
if (remove_issuer_entry(db, issuer, true) != 0) {
198+
return false;
199+
}
181200
sqlite3_close(db);
182201
return false;
183202
}
184203
auto keys_local = iter->second;
185204
iter = top_obj.find("expires");
186205
if (iter == top_obj.end() || !iter->second.is<int64_t>()) {
187-
remove_issuer_entry(db, issuer, true);
206+
if (remove_issuer_entry(db, issuer, true) != 0) {
207+
return false;
208+
}
188209
sqlite3_close(db);
189210
return false;
190211
}
191212
auto expiry = iter->second.get<int64_t>();
192213
if (now > expiry) {
193-
remove_issuer_entry(db, issuer, true);
214+
if (remove_issuer_entry(db, issuer, true) != 0) {
215+
return false;
216+
}
194217
sqlite3_close(db);
195218
return false;
196219
}
@@ -238,9 +261,14 @@ bool scitokens::Validator::store_public_keys(const std::string &issuer,
238261
return false;
239262
}
240263

241-
sqlite3_exec(db, "BEGIN", 0, 0, 0);
264+
if ((rc = sqlite3_exec(db, "BEGIN", 0, 0, 0)) != SQLITE_OK) {
265+
sqlite3_close(db);
266+
return false;
267+
}
242268

243-
remove_issuer_entry(db, issuer, false);
269+
if (remove_issuer_entry(db, issuer, false) != 0) {
270+
return false;
271+
}
244272

245273
sqlite3_stmt *stmt;
246274
rc = sqlite3_prepare_v2(db, "INSERT INTO keycache VALUES (?, ?)", -1, &stmt,
@@ -270,10 +298,13 @@ bool scitokens::Validator::store_public_keys(const std::string &issuer,
270298
sqlite3_close(db);
271299
return false;
272300
}
301+
sqlite3_finalize(stmt);
273302

274-
sqlite3_exec(db, "COMMIT", 0, 0, 0);
303+
if (sqlite3_exec(db, "COMMIT", 0, 0, 0) != SQLITE_OK) {
304+
sqlite3_close(db);
305+
return false;
306+
}
275307

276-
sqlite3_finalize(stmt);
277308
sqlite3_close(db);
278309
return true;
279310
}

0 commit comments

Comments
 (0)