diff options
| author | rillig <rillig@NetBSD.org> | 2021-12-15 09:29:55 +0000 |
|---|---|---|
| committer | rillig <rillig@NetBSD.org> | 2021-12-15 09:29:55 +0000 |
| commit | e379dafa6d63e14f1c79fd4ad7a2a491428ecdfd (patch) | |
| tree | da93384b872ad24c3d8057beca9d16845d43ae7e /usr.bin/make | |
| parent | 09a4507e449d0fa578a94cd79117298b72268ee3 (diff) | |
make: prevent memory leaks from buffers
The warning about unused function results would have prevented the
memory leak that was fixed in cond.c 1.303 from 2021-12-13.
Diffstat (limited to 'usr.bin/make')
| -rw-r--r-- | usr.bin/make/buf.c | 7 | ||||
| -rw-r--r-- | usr.bin/make/buf.h | 8 | ||||
| -rw-r--r-- | usr.bin/make/make.h | 8 |
3 files changed, 15 insertions, 8 deletions
diff --git a/usr.bin/make/buf.c b/usr.bin/make/buf.c index 8c26cfa2495..b07f25d73b4 100644 --- a/usr.bin/make/buf.c +++ b/usr.bin/make/buf.c @@ -1,4 +1,4 @@ -/* $NetBSD: buf.c,v 1.53 2021/11/28 22:48:06 rillig Exp $ */ +/* $NetBSD: buf.c,v 1.54 2021/12/15 09:29:55 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -75,7 +75,7 @@ #include "make.h" /* "@(#)buf.c 8.1 (Berkeley) 6/6/93" */ -MAKE_RCSID("$NetBSD: buf.c,v 1.53 2021/11/28 22:48:06 rillig Exp $"); +MAKE_RCSID("$NetBSD: buf.c,v 1.54 2021/12/15 09:29:55 rillig Exp $"); /* Make space in the buffer for adding at least 16 more bytes. */ void @@ -214,8 +214,9 @@ Buf_DoneDataCompact(Buffer *buf) if (buf->cap - buf->len >= BUF_COMPACT_LIMIT) { /* We trust realloc to be smart */ char *data = bmake_realloc(buf->data, buf->len + 1); + buf->data = NULL; data[buf->len] = '\0'; /* XXX: unnecessary */ - Buf_DoneData(buf); + Buf_Done(buf); return data; } #endif diff --git a/usr.bin/make/buf.h b/usr.bin/make/buf.h index af36773405b..56e6788a3a6 100644 --- a/usr.bin/make/buf.h +++ b/usr.bin/make/buf.h @@ -1,4 +1,4 @@ -/* $NetBSD: buf.h,v 1.44 2021/11/28 22:48:06 rillig Exp $ */ +/* $NetBSD: buf.h,v 1.45 2021/12/15 09:29:55 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. @@ -101,7 +101,7 @@ Buf_AddByte(Buffer *buf, char byte) end[1] = '\0'; } -MAKE_INLINE bool +MAKE_INLINE bool MAKE_ATTR_USE Buf_EndsWith(const Buffer *buf, char ch) { return buf->len > 0 && buf->data[buf->len - 1] == ch; @@ -116,7 +116,7 @@ void Buf_Empty(Buffer *); void Buf_Init(Buffer *); void Buf_InitSize(Buffer *, size_t); void Buf_Done(Buffer *); -char *Buf_DoneData(Buffer *); -char *Buf_DoneDataCompact(Buffer *); +char *Buf_DoneData(Buffer *) MAKE_ATTR_USE; +char *Buf_DoneDataCompact(Buffer *) MAKE_ATTR_USE; #endif /* MAKE_BUF_H */ diff --git a/usr.bin/make/make.h b/usr.bin/make/make.h index 0d101364230..7a5595a113a 100644 --- a/usr.bin/make/make.h +++ b/usr.bin/make/make.h @@ -1,4 +1,4 @@ -/* $NetBSD: make.h,v 1.272 2021/12/13 22:26:21 rillig Exp $ */ +/* $NetBSD: make.h,v 1.273 2021/12/15 09:29:55 rillig Exp $ */ /* * Copyright (c) 1988, 1989, 1990, 1993 @@ -130,6 +130,12 @@ #define MAKE_ATTR_PRINTFLIKE(fmtarg, firstvararg) /* delete */ #endif +#if MAKE_GNUC_PREREQ(4, 0) +#define MAKE_ATTR_USE __attribute__((__warn_unused_result__)) +#else +#define MAKE_ATTR_USE /* delete */ +#endif + #define MAKE_INLINE static inline MAKE_ATTR_UNUSED /* MAKE_STATIC marks a function that may or may not be inlined. */ |
