All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Elia Pinto <gitter.spiros@gmail.com>
Cc: git@vger.kernel.org,
	"Johannes Schindelin" <Johannes.Schindelin@gmx.de>,
	"René Scharfe" <l.s.r@web.de>,
	"Eric W. Biederman" <ebiederm@xmission.com>,
	"Jeff King" <peff@peff.net>
Subject: Re: [PATCH 1/2] tree-walk: use size_t for the size of tree buffers
Date: Tue, 17 Nov 2026 18:32:52 -0800	[thread overview]
Message-ID: <xmqqmrr698dn.fsf@gitster.g> (raw)
In-Reply-To: <908bcf7a607195d7f5215f191a85f25ab0059e8a.1793357680.git.gitter.spiros@gmail.com> (Elia Pinto's message of "Fri, 30 Oct 2026 11:58:22 +0100")

Elia Pinto <gitter.spiros@gmail.com> writes:

> diff --git a/tree-walk.c b/tree-walk.c
> index a67f06b9eb..aaac1fd48d 100644
> --- a/tree-walk.c
> +++ b/tree-walk.c
> @@ -14,7 +14,7 @@
>  #include "environment.h"
>  #include "read-cache-ll.h"
>  
> -static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
> +static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)
>  {
>  	const char *path;
>  	unsigned int len;

This change (and all other changes) is not very interesting by
itself.  Because the size of a tree object may not fit in unsigned
long but fits in size_t, it is obviously the right thing to use
size_t in place of unsigned long.  The only thing we may want to pay
attention to is if arithmetic in the function that uses these
quantities does not cause unsigned integer wraparound.  The type
swap from unsigned long to size_t would affect the correctness (in
other words, if the original was correct, then the move to size_t
would not make it incorrect), so if we assume that the original was
correct, unsigned integer wraparound can be ignored.

Other parts of this function, not the type of "size", are much more
interesting.


        static int decode_tree_entry(struct tree_desc *desc, const char *buf, size_t size, struct strbuf *err)
        {
                const char *path;
                unsigned int len;
                uint16_t mode;
                const unsigned hashsz = desc->algo->rawsz;

Our buf[] starts at the beginning of a sequence of tree entries.
Each tree entry is formatted as a variable-length record that
looks like this (see update_one() in 'cache-tree.c' that writes these):

        <octal mode> SP <name> NUL <binary oid>

The primary caller of this function is update_tree_entry().  It
calls this function starting at the beginning of the inflated tree
object buffer in buf[] with its size in size.  This function carves
out one tree entry of the above shape.  The caller then increments
buf[] by the size of the entry this function read, decrements size by
the same amount, and calls this function again.

With that in mind, let's see the initial "sanity check".

                if (size < hashsz + 3 || buf[size - (hashsz + 1)]) {
                        strbuf_addstr(err, _("too-short tree object"));
                        return -1;
                }

The first half of the sanity check is very pessimistic.  Given that
an octal mode must be at least one byte long (in practice, an entry
that records a tree object with mode 40000 has the shortest octal
mode, which is five bytes long), SP and NUL take two bytes, and the
name also has to be at least one byte long, hashsz (which is the
length of binary oid) + 3 is too short an estimate for the minimum
length of an entry.  It is correct to reject if size is shorter than
that, of course.

The latter half of the check is constant across calls.  It insists
that the very end of the sequence of tree entries contained in the
inflated tree object buffer must end with NUL followed by a binary
oid.  As I explained earlier, because the caller increments buf[] by
the same byte count as it decrements size when it calls this
function again, checking it only once (probably in init_tree_desc())
should be sufficient.  This does not affect correctness, but I see an
optimization opportunity here.  #leftoverbits 

                path = parse_mode(buf, &mode);

We read the <octal mode> SP out of the buf, and point <name> with
this variable path.

                if (!path) {
                        strbuf_addstr(err, _("malformed mode in tree entry"));
                        return -1;
                }
                if (!*path) {
                        strbuf_addstr(err, _("empty filename in tree entry"));
                        return -1;
                }

After sanity checkint the <name> part, we try to figure out its
length.

                len = strlen(path) + 1;

Here, we assume we can run strlen() on it, but have we checked that
there would be a NUL before we go beyond size?  Actually we did,
sort of.  The redundantly repeated "last tree entry that this tree
object records must end with NUL followed by <binary oid>" check
makes sure that there is one NUL that would stop this strlen().

That does not invalidate my earlier comment on optimization
opportunity.  As long as the caller does the right thing when
incrementing buf[] and decrementing size, checking for that NUL just
once at the beginning is sufficient.

                /* Initialize the descriptor entry */
                desc->entry.path = path;
                desc->entry.mode = (desc->flags & TREE_DESC_RAW_MODES) ? mode : canon_mode(mode);
                desc->entry.pathlen = len - 1;
                oidread(&desc->entry.oid, (const unsigned char *)path + len,
                        desc->algo);

                return 0;
        }

What the patch does looks good, by the way ;-)


  reply	other threads:[~2026-11-18  2:32 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-30 10:58 [PATCH 0/2] tree-walk: use size_t for tree sizes Elia Pinto
2026-10-30 10:58 ` [PATCH 1/2] tree-walk: use size_t for the size of tree buffers Elia Pinto
2026-11-18  2:32   ` Junio C Hamano [this message]
2026-10-30 10:58 ` [PATCH 2/2] tree: use size_t for the size of parsed trees Elia Pinto
2026-11-18  3:47   ` Junio C Hamano
2026-11-17 21:42 ` [PATCH 0/2] tree-walk: use size_t for tree sizes Elia Pinto

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

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

  git send-email \
    --in-reply-to=xmqqmrr698dn.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=ebiederm@xmission.com \
    --cc=git@vger.kernel.org \
    --cc=gitter.spiros@gmail.com \
    --cc=l.s.r@web.de \
    --cc=peff@peff.net \
    /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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.