diff mbox series

[v2,09/10] mktree: stop setting *ntr++ to NIL

Message ID patch-09.10-e463fe5f6a-20210420T124428Z-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series object.c et al: tests, small bug fixes etc. | expand

Commit Message

Ævar Arnfjörð Bjarmason April 20, 2021, 12:50 p.m. UTC
Since 58ce21b819e (builtin/mktree: remove hard-coded constant,
2018-10-15) we have not made any subsequent use of the ntr variable
itself, but we did rely on it to NIL-delimit the string we were about
to feed to type_from_string().

Using type_from_string() here results in needless work, as we'd do a
strlen() on it, just to find point at which we had a SPC
character (now NIL) earlier in this function.

We can instead skip incrementing the ntr pointer, then pass the
pointer and length to the type_from_string() function instead.

Doing so would have been buggy in cases where the type was invalid
until a preceding commit fixed the die() invocation in
type_from_string() to also pay attention to the length. A preceding
commit added a test to t1010-mktree.sh which would fail if not for
that fix in type_from_string(), i.e. we'd end up printing the rest of
the line, not just the invalid type.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 builtin/mktree.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

Comments

Junio C Hamano April 29, 2021, 5:01 a.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Since 58ce21b819e (builtin/mktree: remove hard-coded constant,
> 2018-10-15) we have not made any subsequent use of the ntr variable
> itself, but we did rely on it to NIL-delimit the string we were about
> to feed to type_from_string().
>
> Using type_from_string() here results in needless work, as we'd do a
> strlen() on it, just to find point at which we had a SPC
> character (now NIL) earlier in this function.

Since when do we write in LISP? ;-)  The name of the ASCII character
with value 0 is NUL (null).

> We can instead skip incrementing the ntr pointer, then pass the
> pointer and length to the type_from_string() function instead.

Makes sense.  Not clobbering the input buffer is good.
diff mbox series

Patch

diff --git a/builtin/mktree.c b/builtin/mktree.c
index 891991b00d..7a27cfa2e0 100644
--- a/builtin/mktree.c
+++ b/builtin/mktree.c
@@ -95,9 +95,6 @@  static void mktree_line(char *buf, int nul_term_line, int allow_missing)
 	if (S_ISGITLINK(mode))
 		allow_missing = 1;
 
-
-	*ntr++ = 0; /* now at the beginning of SHA1 */
-
 	path = (char *)p + 1;  /* at the beginning of name */
 	if (!nul_term_line && path[0] == '"') {
 		struct strbuf p_uq = STRBUF_INIT;
@@ -111,7 +108,7 @@  static void mktree_line(char *buf, int nul_term_line, int allow_missing)
 	 * These should all agree.
 	 */
 	mode_type = object_type(mode);
-	if (mode_type != type_from_string(ptr)) {
+	if (mode_type != type_from_string_gently(ptr, ntr - ptr, 0)) {
 		die("entry '%s' object type (%s) doesn't match mode type (%s)",
 			path, ptr, type_name(mode_type));
 	}