From: Junio C Hamano <gitster@pobox.com>
To: "Carlo Marcelo Arenas Belón" <carenas@gmail.com>
Cc: git@vger.kernel.org, pclouds@gmail.com, brad@comstyle.com,
collin.funk1@gmail.com
Subject: Re: [PATCH v2] builtin/gc: protect against sysctl() failure in total_ram
Date: Wed, 02 Jul 2025 11:42:59 -0700 [thread overview]
Message-ID: <xmqqikkacu30.fsf@gitster.g> (raw)
In-Reply-To: <20250702154649.44210-1-carenas@gmail.com> ("Carlo Marcelo Arenas Belón"'s message of "Wed, 2 Jul 2025 08:46:49 -0700")
Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
> In the unlikely scenario that sysctl() fails, and uninitialized
> value could be returned.
>
> Initialize the variable used and make sure its expected size
> was correct before using it.
Oh, that's interesting. I wonder if the system returns physical
memory in 32 bits, we would want fall back doing something silly
like
if (!sysctl(mib, 2, &i64_tmp, &length, NULL, 0)) {
if (length == sizeof(i64_tmp))
return i64_tmp;
else if (length == 4 &&
!sysctl(mib, 2, &i32_tmp, &length, NULL, 0) &&
length == 4)
return i32_tmp;
}
> While at it, add a cast for consistency.
OK, I do not mind being more explicit than necessary, but wouldn't
"return X" take care of casting X to the expected return type of
that function?
Anyway, thanks for improvements. Will queue.
> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
> ---
> V1 would regress FreeBSD, so instead make sure that the obsoleted name isn't
> used in OpenBSD/NetBSD instead
>
> builtin/gc.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/builtin/gc.c b/builtin/gc.c
> index 845876ff02..3958707feb 100644
> --- a/builtin/gc.c
> +++ b/builtin/gc.c
> @@ -539,7 +539,7 @@ static uint64_t total_ram(void)
> return total;
> }
> #elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
> - int64_t physical_memory;
> + int64_t physical_memory = 0;
> int mib[2];
> size_t length;
>
> @@ -552,8 +552,9 @@ static uint64_t total_ram(void)
> mib[1] = HW_PHYSMEM;
> # endif
> length = sizeof(int64_t);
> - if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
> - return physical_memory;
> + if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0) &&
> + length == sizeof(int64_t))
> + return (uint64_t)physical_memory;
> #elif defined(GIT_WINDOWS_NATIVE)
> MEMORYSTATUSEX memInfo;
next prev parent reply other threads:[~2025-07-02 18:43 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-02 14:42 [PATCH] builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTL Carlo Marcelo Arenas Belón
2025-07-02 15:23 ` Patrick Steinhardt
2025-07-02 17:04 ` Carlo Marcelo Arenas Belón
2025-07-02 15:46 ` [PATCH v2] builtin/gc: protect against sysctl() failure in total_ram Carlo Marcelo Arenas Belón
2025-07-02 18:42 ` Junio C Hamano [this message]
2025-07-02 20:22 ` Carlo Marcelo Arenas Belón
2025-07-02 20:59 ` Junio C Hamano
2025-07-02 20:21 ` [PATCH v3] builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTL Carlo Marcelo Arenas Belón
2025-07-02 21:14 ` Junio C Hamano
2025-07-02 22:42 ` Carlo Marcelo Arenas Belón
2025-07-02 22:59 ` Junio C Hamano
2025-07-03 8:01 ` [PATCH v4] " Carlo Marcelo Arenas Belón
2025-07-07 13:39 ` Junio C Hamano
2025-07-07 16:45 ` [PATCH v5] " Carlo Marcelo Arenas Belón
2025-07-07 17:09 ` Junio C Hamano
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=xmqqikkacu30.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=brad@comstyle.com \
--cc=carenas@gmail.com \
--cc=collin.funk1@gmail.com \
--cc=git@vger.kernel.org \
--cc=pclouds@gmail.com \
/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).