]> git.cameronkatri.com Git - mandoc.git/blobdiff - mandocdb.c
Improve error handling in dbopen(). If PRAGMA SQL statements fail,
[mandoc.git] / mandocdb.c
index 78ce121792ef5da38687bad777eae01da08aa752..f9e04602e7d5df27d2d1377f29da8643ca88c743 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mandocdb.c,v 1.142 2014/04/23 16:34:50 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>
@@ -471,8 +471,8 @@ main(int argc, char *argv[])
                 * manpath_parse() wants to do it.
                 */
                if (argc > 0) {
-                       dirs.paths = mandoc_calloc(argc,
-                           sizeof(char *));
+                       dirs.paths = mandoc_reallocarray(NULL,
+                           argc, sizeof(char *));
                        dirs.sz = (size_t)argc;
                        for (i = 0; i < argc; i++)
                                dirs.paths[i] = mandoc_strdup(argv[i]);
@@ -663,7 +663,13 @@ treescan(void)
                                fsec[-1] = '\0';
 
                        mlink = mandoc_calloc(1, sizeof(struct mlink));
-                       strlcpy(mlink->file, path, sizeof(mlink->file));
+                       if (strlcpy(mlink->file, path,
+                           sizeof(mlink->file)) >=
+                           sizeof(mlink->file)) {
+                               say(path, "Filename too long");
+                               free(mlink);
+                               continue;
+                       }
                        mlink->dform = dform;
                        mlink->dsec = dsec;
                        mlink->arch = arch;
@@ -819,13 +825,20 @@ filescan(const char *file)
                        say(file, "&stat");
                        return;
                }
-               strlcpy(buf, file, sizeof(buf));
+               if (strlcpy(buf, file, sizeof(buf)) >= sizeof(buf)) {
+                       say(file, "Filename too long");
+                       return;
+               }
                start = strstr(buf, basedir) == buf ?
                    buf + strlen(basedir) + 1 : buf;
        }
 
        mlink = mandoc_calloc(1, sizeof(struct mlink));
-       strlcpy(mlink->file, start, sizeof(mlink->file));
+       if (strlcpy(mlink->file, start, sizeof(mlink->file)) >=
+           sizeof(mlink->file)) {
+               say(start, "Filename too long");
+               return;
+       }
 
        /*
         * First try to guess our directory structure.
@@ -973,17 +986,13 @@ mlinks_undupe(struct mpage *mpage)
                        mpage->form = FORM_NONE;
                        goto nextlink;
                }
-               if (strlcpy(buf, mlink->file, PATH_MAX) >= PATH_MAX) {
-                       if (warnings)
-                               say(mlink->file, "Filename too long");
-                       goto nextlink;
-               }
+               (void)strlcpy(buf, mlink->file, sizeof(buf));
                bufp = strstr(buf, "cat");
                assert(NULL != bufp);
                memcpy(bufp, "man", 3);
                if (NULL != (bufp = strrchr(buf, '.')))
                        *++bufp = '\0';
-               strlcat(buf, mlink->dsec, PATH_MAX);
+               (void)strlcat(buf, mlink->dsec, sizeof(buf));
                if (NULL == ohash_find(&mlinks,
                    ohash_qlookup(&mlinks, buf)))
                        goto nextlink;
@@ -1775,7 +1784,7 @@ putkeys(const struct mpage *mpage,
                s->mask |= v;
                return;
        } else if (NULL == s) {
-               s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
+               s = mandoc_calloc(1, sizeof(struct str) + sz + 1);
                memcpy(s->key, cp, sz);
                ohash_insert(htab, slot, s);
        }
@@ -1987,6 +1996,8 @@ dbadd(struct mpage *mpage, struct mchars *mc)
        mlink = mpage->mlinks;
 
        if (nodb) {
+               if (0 == debug)
+                       return;
                while (NULL != mlink) {
                        fputs(mlink->name, stdout);
                        if (NULL == mlink->next ||
@@ -2198,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;
@@ -2212,32 +2223,23 @@ 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);
        }
 
-       if (strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
-           sizeof(tempfilename)) >= sizeof(tempfilename)) {
-               exitcode = (int)MANDOCLEVEL_SYSERR;
-               say("", "/tmp/mandocdb.XXXXXX: Filename too long");
-               return(0);
-       }
+       (void)strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
+           sizeof(tempfilename));
        if (NULL == mkdtemp(tempfilename)) {
                exitcode = (int)MANDOCLEVEL_SYSERR;
                say("", "&%s", tempfilename);
                return(0);
        }
-       if (strlcat(tempfilename, "/" MANDOC_DB,
-           sizeof(tempfilename)) >= sizeof(tempfilename)) {
-               exitcode = (int)MANDOCLEVEL_SYSERR;
-               say("", "%s/" MANDOC_DB ": Filename too long",
-                   tempfilename);
-               return(0);
-       }
+       (void)strlcat(tempfilename, "/" MANDOC_DB,
+           sizeof(tempfilename));
        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);
        }
 
@@ -2275,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=?)";
@@ -2303,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);
@@ -2314,7 +2331,7 @@ static void *
 hash_halloc(size_t sz, void *arg)
 {
 
-       return(mandoc_calloc(sz, 1));
+       return(mandoc_calloc(1, sz));
 }
 
 static void *