git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Fix type-punning issues
@ 2009-05-12  1:17 Dan McGee
  2009-05-12  7:57 ` Junio C Hamano
  2009-05-12  8:13 ` [PATCH] Fix type-punning issues Johannes Schindelin
  0 siblings, 2 replies; 9+ messages in thread
From: Dan McGee @ 2009-05-12  1:17 UTC (permalink / raw)
  To: git; +Cc: Dan McGee

In these two places we are casting part of our unsigned char sha1 array into
an unsigned int, which violates GCCs strict-aliasing rules (and probably
other compilers). In addition, we had two hashing functions defined in
object.c. Keep the one function that is "correct" and adopt the other ones
to fit.

decorate.c: In function ‘hash_obj’:
decorate.c:11: warning: dereferencing type-punned pointer will break
strict-aliasing rules

Signed-off-by: Dan McGee <dpmcgee@gmail.com>
---

Let me know if this patch is completely off-base. A quick glance at the
generated assembly seems to show this isn't much of a performance hit,
especially given that these functions are inlined anyway. As an FYI the above
warning came when compiling with GCC 4.4; I'm not sure if <4.4 showed this
behavior.

-Dan

 decorate.c |    7 ++++---
 object.c   |   20 +++++++-------------
 2 files changed, 11 insertions(+), 16 deletions(-)

diff --git a/decorate.c b/decorate.c
index 82d9e22..bf83750 100644
--- a/decorate.c
+++ b/decorate.c
@@ -8,7 +8,8 @@
 
 static unsigned int hash_obj(const struct object *obj, unsigned int n)
 {
-	unsigned int hash = *(unsigned int *)obj->sha1;
+	unsigned int hash;
+	memcpy(&hash, obj->sha1, sizeof(unsigned int));
 	return hash % n;
 }
 
@@ -16,7 +17,7 @@ static void *insert_decoration(struct decoration *n, const struct object *base,
 {
 	int size = n->size;
 	struct object_decoration *hash = n->hash;
-	int j = hash_obj(base, size);
+	unsigned int j = hash_obj(base, size);
 
 	while (hash[j].base) {
 		if (hash[j].base == base) {
@@ -68,7 +69,7 @@ void *add_decoration(struct decoration *n, const struct object *obj,
 /* Lookup a decoration pointer */
 void *lookup_decoration(struct decoration *n, const struct object *obj)
 {
-	int j;
+	unsigned int j;
 
 	/* nothing to lookup */
 	if (!n->size)
diff --git a/object.c b/object.c
index 7e6a92c..96ef32d 100644
--- a/object.c
+++ b/object.c
@@ -43,15 +43,16 @@ int type_from_string(const char *str)
 	die("invalid object type \"%s\"", str);
 }
 
-static unsigned int hash_obj(struct object *obj, unsigned int n)
+static unsigned int hash_char(const unsigned char *sha1, unsigned int n)
 {
-	unsigned int hash = *(unsigned int *)obj->sha1;
-	return hash % n;
+	unsigned int i;
+	memcpy(&i, sha1, sizeof(unsigned int));
+	return (int)(i % n);
 }
 
 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
 {
-	int j = hash_obj(obj, size);
+	unsigned int j = hash_char(obj->sha1, size);
 
 	while (hash[j]) {
 		j++;
@@ -61,22 +62,15 @@ static void insert_obj_hash(struct object *obj, struct object **hash, unsigned i
 	hash[j] = obj;
 }
 
-static int hashtable_index(const unsigned char *sha1)
-{
-	unsigned int i;
-	memcpy(&i, sha1, sizeof(unsigned int));
-	return (int)(i % obj_hash_size);
-}
-
 struct object *lookup_object(const unsigned char *sha1)
 {
-	int i;
+	unsigned int i;
 	struct object *obj;
 
 	if (!obj_hash)
 		return NULL;
 
-	i = hashtable_index(sha1);
+	i = hash_char(sha1, obj_hash_size);
 	while ((obj = obj_hash[i]) != NULL) {
 		if (!hashcmp(sha1, obj->sha1))
 			break;
-- 
1.6.2.4

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-05-19  7:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-12  1:17 [PATCH] Fix type-punning issues Dan McGee
2009-05-12  7:57 ` Junio C Hamano
2009-05-19  4:32   ` Dan McGee
2009-05-19  4:34     ` [PATCH 1/3] Unify signedness in hashing calls Dan McGee
2009-05-19  4:34       ` [PATCH 2/3] Convert hash functions to char instead of struct object Dan McGee
2009-05-19  4:34         ` [PATCH 3/3] Unify sha1 char hash functions Dan McGee
2009-05-19  6:23         ` [PATCH 2/3] Convert hash functions to char instead of struct object Johannes Sixt
2009-05-19  7:04           ` Junio C Hamano
2009-05-12  8:13 ` [PATCH] Fix type-punning issues Johannes Schindelin

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).