[prev in list] [next in list] [prev in thread] [next in thread] 

List:       mandoc-source
Subject:    mdocml: Do not cast void pointers to pointers requiring alignment.
From:       schwarze () mdocml ! bsd ! lv
Date:       2011-12-20 21:41:11
Message-ID: 201112202141.pBKLfBWL019588 () krisdoz ! my ! domain
[Download RAW message or body]

Log Message:
-----------
Do not cast void pointers to pointers requiring alignment.
This makes mandocdb(8)/apropos(1) work on strict alignment architectures.
Basic way to fix this confirmed by deraadt@ and kettenis@, thanks.
ok kristaps@

This now works on both sparc64 and i386, but note that the binary
database format is still machine-dependent.

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

Revision Data
-------------
Index: apropos_db.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/apropos_db.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -Lapropos_db.c -Lapropos_db.c -u -p -r1.26 -r1.27
--- apropos_db.c
+++ apropos_db.c
@@ -171,7 +171,7 @@ btree_read(const DBT *k, const DBT *v, 
 		const struct mchars *mc, 
 		struct db_val *dbv, char **buf)
 {
-	const struct db_val *vp;
+	struct db_val	 raw_dbv;
 
 	/* Are our sizes sane? */
 	if (k->size < 2 || sizeof(struct db_val) != v->size)
@@ -181,10 +181,10 @@ btree_read(const DBT *k, const DBT *v, 
 	if ('\0' != ((const char *)k->data)[(int)k->size - 1])
 		return(0);
 
-	vp = v->data;
 	norm_string((const char *)k->data, mc, buf);
-	dbv->rec = betoh32(vp->rec);
-	dbv->mask = betoh64(vp->mask);
+	memcpy(&raw_dbv, v->data, v->size);
+	dbv->rec = betoh32(raw_dbv.rec);
+	dbv->mask = betoh64(raw_dbv.mask);
 	return(1);
 }
 
@@ -380,7 +380,8 @@ index_read(const DBT *key, const DBT *va
 		return(0);
 
 	cp = val->data;
-	rec->res.rec = *(recno_t *)key->data;
+	assert(sizeof(recno_t) == key->size);
+	memcpy(&rec->res.rec, key->data, key->size);
 	rec->res.volume = index;
 
 	if ('d' == (type = *cp++))
Index: mandocdb.c
===================================================================
RCS file: /usr/vhosts/mdocml.bsd.lv/cvs/mdocml/mandocdb.c,v
retrieving revision 1.36
retrieving revision 1.37
diff -Lmandocdb.c -Lmandocdb.c -u -p -r1.36 -r1.37
--- mandocdb.c
+++ mandocdb.c
@@ -525,6 +525,7 @@ index_merge(const struct of *of, struct 
 	struct mdoc	*mdoc;
 	struct man	*man;
 	const char	*fn, *msec, *mtitle, *arch;
+	uint64_t	 mask;
 	size_t		 sv;
 	unsigned	 seq;
 	struct db_val	 vbuf;
@@ -655,7 +656,9 @@ index_merge(const struct of *of, struct 
 		seq = R_FIRST;
 		while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
 			seq = R_NEXT;
-			vbuf.mask = htobe64(*(uint64_t *)val.data);
+			assert(sizeof(uint64_t) == val.size);
+			memcpy(&mask, val.data, val.size);
+			vbuf.mask = htobe64(mask);
 			val.size = sizeof(struct db_val);
 			val.data = &vbuf;
 			dbt_put(db, dbf, &key, &val);
@@ -708,7 +711,8 @@ index_prune(const struct of *ofile, DB *
 	seq = R_FIRST;
 	while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
 		seq = R_NEXT;
-		*maxrec = *(recno_t *)key.data;
+		assert(sizeof(recno_t) == key.size);
+		memcpy(maxrec, key.data, key.size);
 
 		/* Deleted records are zero-sized.  Skip them. */
 
@@ -1068,6 +1072,7 @@ pmdoc_Sh(MDOC_ARGS)
 static void
 hash_put(DB *db, const struct buf *buf, uint64_t mask)
 {
+	uint64_t	 oldmask;
 	DBT		 key, val;
 	int		 rc;
 
@@ -1080,8 +1085,11 @@ hash_put(DB *db, const struct buf *buf, 
 	if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
 		perror("hash");
 		exit((int)MANDOCLEVEL_SYSERR);
-	} else if (0 == rc)
-		mask |= *(uint64_t *)val.data;
+	} else if (0 == rc) {
+		assert(sizeof(uint64_t) == val.size);
+		memcpy(&oldmask, val.data, val.size);
+		mask |= oldmask;
+	}
 
 	val.data = &mask;
 	val.size = sizeof(uint64_t); 
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic