aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mandocdb.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2014-04-27 23:08:56 +0000
committerIngo Schwarze <schwarze@openbsd.org>2014-04-27 23:08:56 +0000
commitb770d7afd08231270f0fb23c286e8111f65903ec (patch)
tree5db4f0429de6112086bbbd8705109bbd625056fa /mandocdb.c
parent9cd87470cdc8d66ea0c03415fc6b2a2bea8e82cb (diff)
downloadmandoc-b770d7afd08231270f0fb23c286e8111f65903ec.tar.gz
mandoc-b770d7afd08231270f0fb23c286e8111f65903ec.tar.zst
mandoc-b770d7afd08231270f0fb23c286e8111f65903ec.zip
Improve error handling in dbopen(). If PRAGMA SQL statements fail,
report the error, close the database, and return failure from dbopen(), such that the main program can recover and rebuild the database. As noticed by stsp@, this can happen when database files are accessible, but corrupt or in the wrong format, which will now automatically be repaired. Besides, use a safer idiom after sqlite3_open*() failure that also handles out-of-memory situations correctly, and do not forget to close the database after CREATE TABLE failure.
Diffstat (limited to 'mandocdb.c')
-rw-r--r--mandocdb.c29
1 files changed, 22 insertions, 7 deletions
diff --git a/mandocdb.c b/mandocdb.c
index 71a233da..f9e04602 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.145 2014/04/25 12:13:15 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.146 2014/04/27 23:08:56 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
@@ -2209,7 +2209,7 @@ dbopen(int real)
rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
if (SQLITE_OK != rc) {
exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB, "%s", sqlite3_errmsg(db));
+ say(MANDOC_DB, "%s", sqlite3_errstr(rc));
return(0);
}
goto prepare_statements;
@@ -2223,7 +2223,7 @@ dbopen(int real)
goto create_tables;
if (MPARSE_QUICK & mparse_options) {
exitcode = (int)MANDOCLEVEL_SYSERR;
- say(MANDOC_DB "~", "%s", sqlite3_errmsg(db));
+ say(MANDOC_DB "~", "%s", sqlite3_errstr(rc));
return(0);
}
@@ -2239,7 +2239,7 @@ dbopen(int real)
rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
if (SQLITE_OK != rc) {
exitcode = (int)MANDOCLEVEL_SYSERR;
- say("", "%s: %s", tempfilename, sqlite3_errmsg(db));
+ say("", "%s: %s", tempfilename, sqlite3_errstr(rc));
return(0);
}
@@ -2277,11 +2277,20 @@ create_tables:
if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
exitcode = (int)MANDOCLEVEL_SYSERR;
say(MANDOC_DB, "%s", sqlite3_errmsg(db));
+ sqlite3_close(db);
return(0);
}
prepare_statements:
- SQL_EXEC("PRAGMA foreign_keys = ON");
+ if (SQLITE_OK != sqlite3_exec(db,
+ "PRAGMA foreign_keys = ON", NULL, NULL, NULL)) {
+ exitcode = (int)MANDOCLEVEL_SYSERR;
+ say(MANDOC_DB, "PRAGMA foreign_keys: %s",
+ sqlite3_errmsg(db));
+ sqlite3_close(db);
+ return(0);
+ }
+
sql = "DELETE FROM mpages WHERE pageid IN "
"(SELECT pageid FROM mlinks WHERE "
"sec=? AND arch=? AND name=?)";
@@ -2305,8 +2314,14 @@ prepare_statements:
* synchronous mode for much better performance.
*/
- if (real)
- SQL_EXEC("PRAGMA synchronous = OFF");
+ if (real && SQLITE_OK != sqlite3_exec(db,
+ "PRAGMA synchronous = OFF", NULL, NULL, NULL)) {
+ exitcode = (int)MANDOCLEVEL_SYSERR;
+ say(MANDOC_DB, "PRAGMA synchronous: %s",
+ sqlite3_errmsg(db));
+ sqlite3_close(db);
+ return(0);
+ }
#endif
return(1);