From ab4991930a27bbd93e1830a44ff13aa8076e479e Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 23 Jan 2024 23:56:29 -0800 Subject: [PATCH] alloc(0) Have alloc() treat a request for 0 bytes as if it was one for 'sizeof (long)' bytes so that the issue of malloc(0) maybe yielding NULL becomes moot. I think instances of attempting to allocate 0 bytes should probably still be checked for and made to avoid calling alloc(0). Assuming that alloc() will clean up requests for nothing feels sloppy. --- src/alloc.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index 14cd68893..690ffd1f3 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 alloc.c $NHDT-Date: 1703716159 2023/12/27 22:29:19 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.32 $ */ +/* NetHack 3.7 alloc.c $NHDT-Date: 1706082987 2024/01/24 07:56:27 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.33 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2012. */ /* NetHack may be freely redistributed. See license for details. */ @@ -41,11 +41,13 @@ static boolean tried_heaplog = FALSE; * recognizes that the following manipulation overcomes that via * rounding the requested length up to the next long. NetHack doesn't * make a lot of tiny allocations, so this shouldn't waste much memory - * regardless of whether malloc() does something similar. + * regardless of whether malloc() does something similar. NetHack + * isn't expected to call alloc(0), but if that happens treat it as + * alloc(sizeof (long)) instead. */ #define ForceAlignedLength(LTH) \ do { \ - if ((LTH) % sizeof (long) != 0) \ + if (!(LTH) || (LTH) % sizeof (long) != 0) \ (LTH) += sizeof (long) - (LTH) % sizeof (long); \ } while (0)