From 1368f65002bf39fdde7dd736a75ae35475184371 Mon Sep 17 00:00:00 2001 From: Jonathan Nieder Date: Sun, 10 Oct 2010 21:59:26 -0500 Subject: compat: helper for detecting unsigned overflow The idiom (a + b < a) works fine for detecting that an unsigned integer has overflowed, but a more explicit unsigned_add_overflows(a, b) might be easier to read. Define such a macro, expanding roughly to ((a) < UINT_MAX - (b)). Because the expansion uses each argument only once outside of sizeof() expressions, it is safe to use with arguments that have side effects. Signed-off-by: Jonathan Nieder Signed-off-by: Junio C Hamano --- wrapper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'wrapper.c') diff --git a/wrapper.c b/wrapper.c index 8d7dd31c4b..79635f2e16 100644 --- a/wrapper.c +++ b/wrapper.c @@ -53,7 +53,7 @@ void *xmalloc(size_t size) void *xmallocz(size_t size) { void *ret; - if (size + 1 < size) + if (unsigned_add_overflows(size, 1)) die("Data too large to fit into virtual memory space."); ret = xmalloc(size + 1); ((char*)ret)[size] = 0; -- cgit v1.2.1