Skip to content

Commit 9a4e155

Browse files
committed
alloc: restore trust to native malloc
Do not kneel in front of bad static analyzers and try to align your allocs. This method of extending/aligning over the native malloc uapi will render some of the memory/malloc analyzer detection useless, like not seeing out of bounds accesses on non aligned ones. Instead of catering to some bad analyzer, use native malloc and thus give full visibility to proper ones. If some architecture still needs aligment to long, it should be special cased and not taint the sane ones. Remove aligment padding from alloc(), return void ptrs like in native malloc and deny zero sized allocs.
1 parent 0378860 commit 9a4e155

File tree

2 files changed

+23
-38
lines changed

2 files changed

+23
-38
lines changed

include/global.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ extern char *dupstr_n(const char *string,
320320
unsigned *lenout) NONNULL NONNULLPTRS;
321321

322322
/* declare alloc.c's alloc(); allocations made with it use ordinary free() */
323-
extern long *alloc(unsigned int) NONNULL; /* alloc.c */
324-
extern long *re_alloc(long *, unsigned int) NONNULL;
323+
extern void *alloc(unsigned int) NONNULL; /* alloc.c */
324+
extern void *re_alloc(void *, unsigned int) NONNULL;
325325

326326
/* Used for consistency checks of various data files; declare it here so
327327
that utility programs which include config.h but not hack.h can see it. */

src/alloc.c

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,40 @@ extern unsigned FITSuint_(unsigned long long, const char *, int) NONNULLARG2;
2121

2222
char *fmt_ptr(const genericptr) NONNULL;
2323

24-
/*
25-
* For historical reasons, nethack's alloc() returns 'long *' rather
26-
* than 'void *' or 'char *'.
27-
*
28-
* Some static analysis complains if it can't deduce that the number
29-
* of bytes being allocated is a multiple of 'sizeof (long)'. It
30-
* recognizes that the following manipulation overcomes that via
31-
* rounding the requested length up to the next long. NetHack doesn't
32-
* make a lot of tiny allocations, so this shouldn't waste much memory
33-
* regardless of whether malloc() does something similar. NetHack
34-
* isn't expected to call alloc(0), but if that happens treat it as
35-
* alloc(sizeof (long)) instead.
36-
*/
37-
#define ForceAlignedLength(LTH) \
38-
do { \
39-
if (!(LTH) || (LTH) % sizeof (long) != 0) \
40-
(LTH) += sizeof (long) - (LTH) % sizeof (long); \
41-
} while (0)
42-
43-
long *alloc(unsigned int) NONNULL;
44-
long *re_alloc(long *, unsigned int) NONNULL;
24+
void *alloc(unsigned int) NONNULL;
25+
void *re_alloc(void *, unsigned int) NONNULL;
4526
ATTRNORETURN extern void panic(const char *, ...) PRINTF_F(1, 2) NORETURN;
4627

47-
long *
48-
alloc(unsigned int lth)
28+
void *
29+
alloc(unsigned int bytes)
4930
{
50-
genericptr_t ptr;
31+
void *p;
32+
33+
if (!bytes)
34+
panic("Zero sized allocs not allowed");
5135

52-
ForceAlignedLength(lth);
53-
ptr = malloc(lth);
54-
if (!ptr)
55-
panic("Memory allocation failure; cannot get %u bytes", lth);
36+
p = malloc(bytes);
37+
if (!p)
38+
panic("Memory allocation failure; cannot get %u bytes", bytes);
5639

57-
return (long *) ptr;
40+
return p;
5841
}
5942

6043
/* realloc() call that might get substituted by nhrealloc(p,n,file,line) */
61-
long *
62-
re_alloc(long *oldptr, unsigned int newlth)
44+
void *
45+
re_alloc(void *old, unsigned int newlth)
6346
{
64-
long *newptr;
47+
void *p;
48+
49+
if (!newlth)
50+
panic("Zero sized reallocs not allowed");
6551

66-
ForceAlignedLength(newlth);
67-
newptr = (long *) realloc((genericptr_t) oldptr, (size_t) newlth);
52+
p = realloc(old, (size_t) newlth);
6853
/* "extend to": assume it won't ever fail if asked to shrink */
69-
if (newlth && !newptr)
54+
if (!p)
7055
panic("Memory allocation failure; cannot extend to %u bytes", newlth);
7156

72-
return newptr;
57+
return p;
7358
}
7459

7560
#ifdef HAS_PTR_FMT

0 commit comments

Comments
 (0)