From ae913ab6275ff746439a2d99f8a1550ec2e511ff Mon Sep 17 00:00:00 2001 From: Robin Haberkorn Date: Sat, 5 Jun 2021 17:43:58 +0200 Subject: teco_memory_check() now checks for addition overflows and negative searches () for multiplication overflows * Since the numbers come from "outside" (SciTECO scripts) this is easily possible, resulting either in missed checks or even memory corruption. * In particular, this fixes the "Searching with large counts" test case on 32-bit builds. Perhaps at least one CI build should be 32-bit? --- src/memory.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/memory.c') diff --git a/src/memory.c b/src/memory.c index 9846753..2d93b12 100644 --- a/src/memory.c +++ b/src/memory.c @@ -678,7 +678,15 @@ teco_memory_check(gsize request, GError **error) { gsize memory_usage = g_atomic_int_get(&teco_memory_usage) + request; - if (G_UNLIKELY(teco_memory_limit && memory_usage > teco_memory_limit)) { + /* + * Check for overflows. + * NOTE: Glib 2.48 has g_size_checked_add(). + */ + if (G_UNLIKELY(memory_usage < request)) + /* guaranteed to fail if memory limiting is enabled */ + memory_usage = G_MAXSIZE; + + if (G_UNLIKELY(teco_memory_limit && memory_usage >= teco_memory_limit)) { g_autofree gchar *limit_str = g_format_size(memory_usage); g_set_error(error, TECO_ERROR, TECO_ERROR_MEMLIMIT, -- cgit v1.2.3