diff mbox series

[5/5] cache: make oidcpy always copy GIT_MAX_RAWSZ bytes

Message ID 20190110042551.915769-6-sandals@crustytoothpaste.net (mailing list archive)
State New, archived
Headers show
Series tree-walk object_id refactor | expand

Commit Message

brian m. carlson Jan. 10, 2019, 4:25 a.m. UTC
There are some situations in which we want to store an object ID into
struct object_id without the_hash_algo necessarily being set correctly.
One such case is when cloning a repository, where we must read refs from
the remote side without having a repository from which to read the
preferred algorithm.

In this cases, we may have the_hash_algo set to SHA-1, which is the
default, but read refs into struct object_id that are SHA-256. When
copying these values, we will want to copy them completely, not just the
first 20 bytes. Consequently, make sure that oidcpy copies the maximum
number of bytes at all times, regardless of the setting of
the_hash_algo.

Since oidcpy and hashcpy are no longer functionally identical, remove
the Cocinelle object_id transformations that convert from one into the
other.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 cache.h                            |  2 +-
 contrib/coccinelle/object_id.cocci | 30 ------------------------------
 2 files changed, 1 insertion(+), 31 deletions(-)

Comments

Jeff King Jan. 10, 2019, 6:50 a.m. UTC | #1
On Thu, Jan 10, 2019 at 04:25:51AM +0000, brian m. carlson wrote:

> Since oidcpy and hashcpy are no longer functionally identical, remove
> the Cocinelle object_id transformations that convert from one into the
> other.

Unfortunately this means we'll no longer automatically find cases where
"foo" got converted into "struct object_id" and could be updated. I
guess at some point we assume that most everything has been converted
anyway, and that coccinelle rule loses its usefulness.

-Peff
diff mbox series

Patch

diff --git a/cache.h b/cache.h
index ca36b44ee0..8b8c6a1a2a 100644
--- a/cache.h
+++ b/cache.h
@@ -1072,7 +1072,7 @@  static inline void hashcpy(unsigned char *sha_dst, const unsigned char *sha_src)
 
 static inline void oidcpy(struct object_id *dst, const struct object_id *src)
 {
-	hashcpy(dst->hash, src->hash);
+	memcpy(dst->hash, src->hash, GIT_MAX_RAWSZ);
 }
 
 static inline struct object_id *oiddup(const struct object_id *src)
diff --git a/contrib/coccinelle/object_id.cocci b/contrib/coccinelle/object_id.cocci
index 6a7cf3e02d..3e536a9834 100644
--- a/contrib/coccinelle/object_id.cocci
+++ b/contrib/coccinelle/object_id.cocci
@@ -86,36 +86,6 @@  struct object_id OID;
 - hashcmp(OID.hash, OIDPTR->hash)
 + oidcmp(&OID, OIDPTR)
 
-@@
-struct object_id OID1, OID2;
-@@
-- hashcpy(OID1.hash, OID2.hash)
-+ oidcpy(&OID1, &OID2)
-
-@@
-identifier f != oidcpy;
-struct object_id *OIDPTR1;
-struct object_id *OIDPTR2;
-@@
-  f(...) {<...
-- hashcpy(OIDPTR1->hash, OIDPTR2->hash)
-+ oidcpy(OIDPTR1, OIDPTR2)
-  ...>}
-
-@@
-struct object_id *OIDPTR;
-struct object_id OID;
-@@
-- hashcpy(OIDPTR->hash, OID.hash)
-+ oidcpy(OIDPTR, &OID)
-
-@@
-struct object_id *OIDPTR;
-struct object_id OID;
-@@
-- hashcpy(OID.hash, OIDPTR->hash)
-+ oidcpy(&OID, OIDPTR)
-
 @@
 struct object_id *OIDPTR1;
 struct object_id *OIDPTR2;