git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTL
@ 2025-07-02 14:42 Carlo Marcelo Arenas Belón
  2025-07-02 15:23 ` Patrick Steinhardt
  2025-07-02 15:46 ` [PATCH v2] builtin/gc: protect against sysctl() failure in total_ram Carlo Marcelo Arenas Belón
  0 siblings, 2 replies; 15+ messages in thread
From: Carlo Marcelo Arenas Belón @ 2025-07-02 14:42 UTC (permalink / raw)
  To: git; +Cc: pclouds, brad, collin.funk1, Carlo Marcelo Arenas Belón

In BSD systems other than macOS, since 9806f5a7bf (gc --auto:
exclude base pack if not enough mem to "repack -ad", 2018-04-15),
sysctl() use HW_PHYSMEM with the wrong size for the target.

Use the correct type for physical_memory on each option and make
sure it is initialized, so it is safe to use even if sysctl() fails.

While at it, add a cast to the returned value for consistency.

Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 builtin/gc.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/builtin/gc.c b/builtin/gc.c
index 845876ff02..3b2ca99af9 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -539,21 +539,25 @@ 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;
 	int mib[2];
 	size_t length;
-
-	mib[0] = CTL_HW;
 # if defined(HW_MEMSIZE)
+	int64_t physical_memory = 0;
+
 	mib[1] = HW_MEMSIZE;
 # elif defined(HW_PHYSMEM64)
+	int64_t physical_memory = 0;
+
 	mib[1] = HW_PHYSMEM64;
 # else
+	int physical_memory = 0;
+
 	mib[1] = HW_PHYSMEM;
 # endif
-	length = sizeof(int64_t);
+	mib[0] = CTL_HW;
+	length = sizeof(physical_memory);
 	if (!sysctl(mib, 2, &physical_memory, &length, NULL, 0))
-		return physical_memory;
+		return (uint64_t)physical_memory;
 #elif defined(GIT_WINDOWS_NATIVE)
 	MEMORYSTATUSEX memInfo;
 
-- 
2.50.0.147.gafe0d4ec5b


^ permalink raw reply related	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2025-07-07 17:09 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

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).