From: Taylor Blau <me@ttaylorr.com>
To: Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Cc: git@vger.kernel.org, Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: Re: [PATCH 3/4] load_revindex_from_disk(): avoid accessing uninitialized data
Date: Thu, 27 Mar 2025 10:23:09 -0400 [thread overview]
Message-ID: <Z+VfTUrQe1x8DO+h@nand.local> (raw)
In-Reply-To: <b9901920de20ae29bd55bd68dab37a737867593b.1743079429.git.gitgitgadget@gmail.com>
On Thu, Mar 27, 2025 at 12:43:48PM +0000, Johannes Schindelin via GitGitGadget wrote:
> From: Johannes Schindelin <johannes.schindelin@gmx.de>
>
> The `revindex_size` value is uninitialized in case the function is
> erroring out, but we want to assign its value. Let's just initialize it.
>
> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
> ---
> pack-revindex.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/pack-revindex.c b/pack-revindex.c
> index d3832478d99..3b007d771b3 100644
> --- a/pack-revindex.c
> +++ b/pack-revindex.c
> @@ -208,7 +208,7 @@ static int load_revindex_from_disk(char *revindex_name,
> int fd, ret = 0;
> struct stat st;
> void *data = NULL;
> - size_t revindex_size;
> + size_t revindex_size = 0;
I'm certainly not opposed to initializing variables proactively, but in
this particular case I don't think it's necessary.
We assign 'revindex_size' out to 'len_p' when we enter the cleanup
routine label if 'ret' is zero. We'll use 'revindex_size' in the same
label to munmap() when 'ret' is non-zero, but only if 'data' is also
initialized.
So there are two conditions where we'll enter the cleanup label before
assigning 'revindex_size', when git_open() returns a negative value, or
fstat()ing the descriptor that git_open() gave us returns a non-zero
value. In both of those cases, ret is non-zero (it is assigned to 1 and
the return value of error_errno() in those cases, respectively). Since
'data' is also NULL here, this function will terminate without using
the uninitialized 'revindex_size'.
If both of those work (i.e., we opened the file and fstat()ed it
successfully), then we'll have revindex_size initialized to st.st_size
(really the result of calling xsize_t() on it). There are two sanity
checks on the size, both of which happen before we have mmap()ed the
file, and both sanity checks set 'ret' to a non-zero value upon failure.
So by the time we '*len_p = revindex_size' it is guaranteed to be
initialized and just junk bytes on the stack.
Did this trigger a warning from a static analyzer or something? If so,
I'm happy to take this patch to appease it. Perhaps that it what's going
on since I recall you mentioning that you were working on enabling
CodeQL in Microsoft's fork of Git.
But if not I might suggest dropping this patch for the reasons above.
Thanks,
Taylor
next prev parent reply other threads:[~2025-03-27 14:23 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-27 12:43 [PATCH 0/4] Initialize a few uninitialized variables Johannes Schindelin via GitGitGadget
2025-03-27 12:43 ` [PATCH 1/4] cat_one_file(): make it easy to see that the `size` variable is initialized Johannes Schindelin via GitGitGadget
2025-03-28 3:46 ` Jeff King
2025-03-27 12:43 ` [PATCH 2/4] fsck: avoid using an uninitialized variable Johannes Schindelin via GitGitGadget
2025-03-28 4:07 ` Jeff King
2025-03-27 12:43 ` [PATCH 3/4] load_revindex_from_disk(): avoid accessing uninitialized data Johannes Schindelin via GitGitGadget
2025-03-27 14:23 ` Taylor Blau [this message]
2025-03-27 12:43 ` [PATCH 4/4] load_pack_mtimes_file(): " Johannes Schindelin via GitGitGadget
2025-03-27 14:24 ` Taylor Blau
2025-07-17 16:45 ` [PATCH 0/4] Initialize a few uninitialized variables Johannes Schindelin
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=Z+VfTUrQe1x8DO+h@nand.local \
--to=me@ttaylorr.com \
--cc=git@vger.kernel.org \
--cc=gitgitgadget@gmail.com \
--cc=johannes.schindelin@gmx.de \
/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 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 NNTP newsgroup(s).