git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] tree-walk: use size_t for the size of tree buffers

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 18, 2026, 02:32 UTC
Message-ID
<xmqqmrr698dn.fsf@gitster.g>
In-Reply-To
<908bcf7a607195d7f5215f191a85f25ab0059e8a.1793357680.git.gitter.spiros@gmail.com>
Elia Pinto <gitter.spiros@gmail.com> writes:
Show 13 quoted lines
> 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 ;-)
Previous: Elia PintoNext: Elia Pinto
Message 3 of 6 in “tree-walk: use size_t for tree sizes”
  1. 0/2 tree-walk: use size_t for tree sizesElia Pinto, Oct 30, 2026
  2. 1/2 tree-walk: use size_t for the size of tree buffersElia Pinto, Oct 30, 2026
  3. Junio C HamanoNov 18, 2026
  4. 2/2 tree: use size_t for the size of parsed treesElia Pinto, Oct 30, 2026
  5. Junio C HamanoNov 18, 2026
  6. Elia PintoNov 17, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.