aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mandocdb.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2011-12-10 22:01:06 +0000
committerIngo Schwarze <schwarze@openbsd.org>2011-12-10 22:01:06 +0000
commite68e6cc5fe31f10d44e4c7fa6a9217560ecb80a3 (patch)
treedf52b60325e4e67684ef2dffffb43b5ab70b1d48 /mandocdb.c
parent04ee7f27d7caa7c8b690131c06deafe3af1d326b (diff)
downloadmandoc-e68e6cc5fe31f10d44e4c7fa6a9217560ecb80a3.tar.gz
mandoc-e68e6cc5fe31f10d44e4c7fa6a9217560ecb80a3.tar.zst
mandoc-e68e6cc5fe31f10d44e4c7fa6a9217560ecb80a3.zip
Avoid leaking index records:
Before allocating a record for a file, first make sure we actually want to use the file.
Diffstat (limited to 'mandocdb.c')
-rw-r--r--mandocdb.c46
1 files changed, 25 insertions, 21 deletions
diff --git a/mandocdb.c b/mandocdb.c
index baf7585f..b7c7e0b3 100644
--- a/mandocdb.c
+++ b/mandocdb.c
@@ -1,4 +1,4 @@
-/* $Id: mandocdb.c,v 1.32 2011/12/10 16:53:39 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.33 2011/12/10 22:01:06 schwarze Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
@@ -528,29 +528,15 @@ index_merge(const struct of *of, struct mparse *mp,
fn = of->fname;
/*
- * Reclaim an empty index record, if available.
+ * Try interpreting the file as mdoc(7) or man(7)
+ * source code, unless it is already known to be
+ * formatted. Fall back to formatted mode.
*/
- if (reccur > 0) {
- --reccur;
- rec = recs[(int)reccur];
- } else if (maxrec > 0) {
- rec = maxrec;
- maxrec = 0;
- } else
- rec++;
-
mparse_reset(mp);
- hash_reset(&hash);
mdoc = NULL;
man = NULL;
- /*
- * Try interpreting the file as mdoc(7) or man(7)
- * source code, unless it is already known to be
- * formatted. Fall back to formatted mode.
- */
-
if ((MANDOC_SRC & of->src_form ||
! (MANDOC_FORM & of->src_form)) &&
MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
@@ -625,8 +611,12 @@ index_merge(const struct of *of, struct mparse *mp,
sv = dbuf->len;
- /* Fix the record number in the btree value. */
+ /*
+ * Collect keyword/mask pairs.
+ * Each pair will become a new btree node.
+ */
+ hash_reset(&hash);
if (mdoc)
pmdoc_node(hash, buf, dbuf,
mdoc_node(mdoc), mdoc_meta(mdoc));
@@ -636,11 +626,25 @@ index_merge(const struct of *of, struct mparse *mp,
pformatted(hash, buf, dbuf, of);
/*
- * Copy from the in-memory hashtable of pending keywords
- * into the database.
+ * Reclaim an empty index record, if available.
+ * Use its record number for all new btree nodes.
*/
+ if (reccur > 0) {
+ --reccur;
+ rec = recs[(int)reccur];
+ } else if (maxrec > 0) {
+ rec = maxrec;
+ maxrec = 0;
+ } else
+ rec++;
vbuf.rec = htobe32(rec);
+
+ /*
+ * Copy from the in-memory hashtable of pending
+ * keyword/mask pairs into the database.
+ */
+
seq = R_FIRST;
while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
seq = R_NEXT;