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

[PATCH] builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTL

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Jul 2, 2025, 14:42 UTC
Message-ID
<20250702144244.43858-1-carenas@gmail.com>

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
Next: Patrick Steinhardt
Message 1 of 15 in “builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTL”
  1. builtin/gc: improve total_ram calculation for HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 2, 2025
  2. Patrick SteinhardtJul 2, 2025
  3. Carlo Marcelo Arenas BelónJul 2, 2025
  4. builtin/gc: protect against sysctl() failure in total_ramCarlo Marcelo Arenas Belón, Jul 2, 2025
  5. Junio C HamanoJul 2, 2025
  6. Carlo Marcelo Arenas BelónJul 2, 2025
  7. Junio C HamanoJul 2, 2025
  8. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 2, 2025
  9. Junio C HamanoJul 2, 2025
  10. Carlo Marcelo Arenas BelónJul 2, 2025
  11. Junio C HamanoJul 2, 2025
  12. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 3, 2025
  13. Junio C HamanoJul 7, 2025
  14. builtin/gc: correct total_ram calculation with HAVE_BSD_SYSCTLCarlo Marcelo Arenas Belón, Jul 7, 2025
  15. Junio C HamanoJul 7, 2025

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.