git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Duy Nguyen <pclouds@gmail.com>
To: Patryk Obara <patryk.obara@gmail.com>
Cc: Git List <git@vger.kernel.org>,
	Junio C Hamano <gitster@pobox.com>,
	"brian m . carlson" <sandals@crustytoothpaste.ath.cx>,
	Jonathan Tan <jonathantanmy@google.com>
Subject: Re: [PATCH v2 07/14] match-trees: convert splice_tree to object_id
Date: Tue, 23 Jan 2018 08:29:33 +0700	[thread overview]
Message-ID: <20180123012933.GA15378@duynguyen.dek-tpc.internal> (raw)
In-Reply-To: <CAJfL8+TrFV=DkJPZAYYYc69recsYiOGdeV8Ev0r5xfcmmqP-Hw@mail.gmail.com>

On Mon, Jan 22, 2018 at 02:12:56PM +0100, Patryk Obara wrote:
> >> @@ -197,26 +195,26 @@ static int splice_tree(const unsigned char *hash1,
> >>               if (strlen(name) == toplen &&
> >>                   !memcmp(name, prefix, toplen)) {
> >>                       if (!S_ISDIR(mode))
> >> -                             die("entry %s in tree %s is not a tree",
> >> -                                 name, sha1_to_hex(hash1));
> >> -                     rewrite_here = (unsigned char *) oid->hash;
> >> +                             die("entry %s in tree %s is not a tree", name,
> >> +                                 oid_to_hex(hash1));
> >> +                     rewrite_here = (struct object_id *)oid;
> >
> > You don't need the typecast here anymore, do you?
> 
> Unfortunately, I do :(
> 
> Few lines above:
> 192: const struct object_id *oid;
> 194: oid = tree_entry_extract(&desc, &name, &mode);
> 
> Function tree_entry_extract returns const pointer, which leads to
> compiler warning:
> "assigning to 'struct object_id *' from 'const struct object_id *'
> discards qualifiers".
> 
> On the other hand, if I change const qualifier for 'rewrite_here'
> variable - warning will
> appear in line 216:
> 
> 216: oidcpy(rewrite_here, rewrite_with);
> 
> So the question here is rather: is it ok to overwrite buffer returned
> by tree_entry_extract?
> 
> When writing this I opted to preserve cv-qualifiers despite changing
> pointer type (which implied preservation of typecast) - partly
> because parameter 'desc' of tree_entry_extract is NOT const (which
> suggests to me, that it's ok).
> 
> But this cast might be indication of unintended modification inside
> tree description structure and might lead to an error is some other
> place, if there's an assumption, that this buffer is not
> overwritable.
> 
> Maybe const should be removed from return type of tree_entry_extract
> (and maybe from oid field of struct name_entry)?
>
> I will give it some more thought - maybe oidcpy from line 216 could
> be replaced.

I've read this code a bit more (sorry I didn't see the "const struct
object_id *oid" line when I read this patch). I think the typecast is
very much on purpose. Junio wanted to make a new tree with one
different hash in 68faf68938 (A new merge stragety 'subtree'. -
2007-02-15) but I think he kinda abused the tree walker for this
task.

A cleaner way is create a new tree by copying unmodified entries and
replacing just one entry. I think the old way was ok when we dealt
with SHA-1 directly, but with the object_id abstraction in place, this
kind of update looks iffy.

Alternatively, perhaps we can do something like this to keep tree
manipulation in tree-walk.c, one of the two places that know about
tree object on-disk format (the other one is cache-tree.c)

-- 8< --
diff --git a/match-trees.c b/match-trees.c
index 396b7338df..a8dc8a53d9 100644
--- a/match-trees.c
+++ b/match-trees.c
@@ -171,7 +171,7 @@ static int splice_tree(const unsigned char *hash1,
 	char *buf;
 	unsigned long sz;
 	struct tree_desc desc;
-	unsigned char *rewrite_here;
+	const object_id *rewrite_here;
 	const unsigned char *rewrite_with;
 	unsigned char subtree[20];
 	enum object_type type;
@@ -199,7 +199,7 @@ static int splice_tree(const unsigned char *hash1,
 			if (!S_ISDIR(mode))
 				die("entry %s in tree %s is not a tree",
 				    name, sha1_to_hex(hash1));
-			rewrite_here = (unsigned char *) oid->hash;
+			rewrite_here = oid->hash;
 			break;
 		}
 		update_tree_entry(&desc);
@@ -215,7 +215,7 @@ static int splice_tree(const unsigned char *hash1,
 	}
 	else
 		rewrite_with = hash2;
-	hashcpy(rewrite_here, rewrite_with);
+	replace_tree_entry_hash(&desc, rewrite_with, buf, sz);
 	status = write_sha1_file(buf, sz, tree_type, result);
 	free(buf);
 	return status;
diff --git a/tree-walk.c b/tree-walk.c
index 63a87ed666..f31a03569f 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -164,6 +164,17 @@ int tree_entry_gently(struct tree_desc *desc, struct name_entry *entry)
 	return 1;
 }
 
+void replace_tree_entry_hash(struct tree_desc *desc,
+			     const unsigned char *sha1,
+			     char *buf, unsigned long size)
+{
+	unsigned long offset = (const char *)desc->buffer - buf;
+	unsigned char *to_update;
+
+	to_update = (unsigned char *)buf + offset + tree_entry_len(&desc->entry);
+	hashcpy(to_update, sha1);
+}
+
 void setup_traverse_info(struct traverse_info *info, const char *base)
 {
 	int pathlen = strlen(base);
diff --git a/tree-walk.h b/tree-walk.h
index b6bd1b4ccf..9a7d133d68 100644
--- a/tree-walk.h
+++ b/tree-walk.h
@@ -35,6 +35,9 @@ int update_tree_entry_gently(struct tree_desc *);
 void init_tree_desc(struct tree_desc *desc, const void *buf, unsigned long size);
 int init_tree_desc_gently(struct tree_desc *desc, const void *buf, unsigned long size);
 
+void replace_tree_entry_hash(struct tree_desc *desc,
+			     const unsigned char *sha1,
+			     char *buf, unsigned long size);
 /*
  * Helper function that does both tree_entry_extract() and update_tree_entry()
  * and returns true for success
-- 8< --

  reply	other threads:[~2018-01-23  1:29 UTC|newest]

Thread overview: 75+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-18 14:50 [PATCH 00/11] Some fixes and bunch of object_id conversions Patryk Obara
2018-01-18 14:50 ` [PATCH 01/11] http-push: improve error log Patryk Obara
2018-01-18 14:50 ` [PATCH 02/11] clang-format: adjust penalty for return type line break Patryk Obara
2018-01-18 14:50 ` [PATCH 03/11] sha1_file: convert pretend_sha1_file to object_id Patryk Obara
2018-01-18 14:50 ` [PATCH 04/11] dir: convert struct sha1_stat to use object_id Patryk Obara
2018-01-18 14:50 ` [PATCH 05/11] sha1_file: convert hash_sha1_file to object_id Patryk Obara
2018-01-18 14:50 ` [PATCH 06/11] cache: clear whole hash buffer with oidclr Patryk Obara
2018-01-18 14:50 ` [PATCH 07/11] match-trees: convert splice_tree to object_id Patryk Obara
2018-01-18 14:51 ` [PATCH 08/11] commit: convert commit_tree* " Patryk Obara
2018-01-18 14:51 ` [PATCH 09/11] notes: convert combine_notes_* " Patryk Obara
2018-01-18 14:51 ` [PATCH 10/11] notes: convert write_notes_tree " Patryk Obara
2018-01-18 14:51 ` [PATCH 11/11] sha1_file: convert write_sha1_file " Patryk Obara
2018-01-20 20:44   ` brian m. carlson
2018-01-21 16:26     ` Patryk Obara
2018-01-18 17:42 ` [PATCH 00/11] Some fixes and bunch of object_id conversions Jonathan Tan
2018-01-20 20:58 ` brian m. carlson
2018-01-21 16:35   ` Patryk Obara
2018-01-22 11:04 ` [PATCH v2 00/14] " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 01/14] http-push: improve error log Patryk Obara
2018-01-22 11:04   ` [PATCH v2 02/14] clang-format: adjust penalty for return type line break Patryk Obara
2018-01-22 11:04   ` [PATCH v2 03/14] sha1_file: convert pretend_sha1_file to object_id Patryk Obara
2018-01-22 11:04   ` [PATCH v2 04/14] dir: convert struct sha1_stat to use object_id Patryk Obara
2018-01-22 11:04   ` [PATCH v2 05/14] sha1_file: convert hash_sha1_file to object_id Patryk Obara
2018-01-22 11:49     ` Duy Nguyen
2018-01-22 12:44       ` Patryk Obara
2018-01-22 12:52         ` Duy Nguyen
2018-01-22 11:04   ` [PATCH v2 06/14] cache: clear whole hash buffer with oidclr Patryk Obara
2018-01-22 11:04   ` [PATCH v2 07/14] match-trees: convert splice_tree to object_id Patryk Obara
2018-01-22 11:56     ` Duy Nguyen
2018-01-22 13:12       ` Patryk Obara
2018-01-23  1:29         ` Duy Nguyen [this message]
2018-01-22 11:04   ` [PATCH v2 08/14] commit: convert commit_tree* " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 09/14] notes: convert combine_notes_* " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 10/14] notes: convert write_notes_tree " Patryk Obara
2018-01-22 12:03     ` Duy Nguyen
2018-01-22 11:04   ` [PATCH v2 11/14] sha1_file: convert write_sha1_file " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 12/14] sha1_file: convert force_object_loose " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 13/14] sha1_file: convert write_loose_object " Patryk Obara
2018-01-22 11:04   ` [PATCH v2 14/14] sha1_file: rename hash_sha1_file_literally Patryk Obara
2018-01-22 12:14   ` [PATCH v2 00/14] Some fixes and bunch of object_id conversions Duy Nguyen
2018-01-22 13:26     ` Patryk Obara
2018-01-24 11:11   ` [PATCH v3 " Patryk Obara
2018-01-24 11:11     ` [PATCH v3 01/14] http-push: improve error log Patryk Obara
2018-01-24 11:11     ` [PATCH v3 02/14] clang-format: adjust penalty for return type line break Patryk Obara
2018-01-24 11:11     ` [PATCH v3 03/14] sha1_file: convert pretend_sha1_file to object_id Patryk Obara
2018-01-24 11:11     ` [PATCH v3 04/14] dir: convert struct sha1_stat to use object_id Patryk Obara
2018-01-24 21:36       ` Junio C Hamano
2018-01-24 11:11     ` [PATCH v3 05/14] sha1_file: convert hash_sha1_file to object_id Patryk Obara
2018-01-24 11:11     ` [PATCH v3 06/14] cache: clear whole hash buffer with oidclr Patryk Obara
2018-01-24 11:11     ` [PATCH v3 07/14] match-trees: convert splice_tree to object_id Patryk Obara
2018-01-24 11:12     ` [PATCH v3 08/14] commit: convert commit_tree* " Patryk Obara
2018-01-24 11:12     ` [PATCH v3 09/14] notes: convert combine_notes_* " Patryk Obara
2018-01-24 11:12     ` [PATCH v3 10/14] notes: convert write_notes_tree " Patryk Obara
2018-01-24 11:12     ` [PATCH v3 11/14] sha1_file: convert write_sha1_file " Patryk Obara
2018-01-24 11:12     ` [PATCH v3 12/14] sha1_file: convert force_object_loose " Patryk Obara
2018-01-24 11:12     ` [PATCH v3 13/14] sha1_file: convert write_loose_object " Patryk Obara
2018-01-26 23:14       ` brian m. carlson
2018-01-24 11:12     ` [PATCH v3 14/14] sha1_file: rename hash_sha1_file_literally Patryk Obara
2018-01-24 21:28     ` [PATCH v3 00/14] Some fixes and bunch of object_id conversions Junio C Hamano
2018-01-24 21:41     ` Junio C Hamano
2018-01-25  9:53       ` Duy Nguyen
2018-01-27  0:12       ` brian m. carlson
2018-01-28  0:13     ` [PATCH v4 00/12] A " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 01/12] sha1_file: convert pretend_sha1_file to object_id Patryk Obara
2018-01-28  0:13       ` [PATCH v4 02/12] dir: convert struct sha1_stat to use object_id Patryk Obara
2018-01-28  0:13       ` [PATCH v4 03/12] sha1_file: convert hash_sha1_file to object_id Patryk Obara
2018-01-28  0:13       ` [PATCH v4 04/12] cache: clear whole hash buffer with oidclr Patryk Obara
2018-01-28  0:13       ` [PATCH v4 05/12] match-trees: convert splice_tree to object_id Patryk Obara
2018-01-28  0:13       ` [PATCH v4 06/12] commit: convert commit_tree* " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 07/12] notes: convert combine_notes_* " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 08/12] notes: convert write_notes_tree " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 09/12] sha1_file: convert write_sha1_file " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 10/12] sha1_file: convert force_object_loose " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 11/12] sha1_file: convert write_loose_object " Patryk Obara
2018-01-28  0:13       ` [PATCH v4 12/12] sha1_file: rename hash_sha1_file_literally Patryk Obara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180123012933.GA15378@duynguyen.dek-tpc.internal \
    --to=pclouds@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jonathantanmy@google.com \
    --cc=patryk.obara@gmail.com \
    --cc=sandals@crustytoothpaste.ath.cx \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).