source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: Improve error handling in dbopen().
Date: Sun, 27 Apr 2014 19:08:57 -0400 (EDT)	[thread overview]
Message-ID: <201404272308.s3RN8vqa017903@krisdoz.my.domain> (raw)

Log Message:
-----------
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.

Modified Files:
--------------
    mdocml:
        mandocdb.c

Revision Data
-------------
Index: mandocdb.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.c,v
retrieving revision 1.145
retrieving revision 1.146
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.145 -r1.146
--- mandocdb.c
+++ mandocdb.c
@@ -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);
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2014-04-27 23:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201404272308.s3RN8vqa017903@krisdoz.my.domain \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).