Module Name:    src
Committed By:   rillig
Date:           Wed Dec 15 09:29:56 UTC 2021

Modified Files:
        src/usr.bin/make: buf.c buf.h make.h

Log Message:
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.


To generate a diff of this commit:
cvs rdiff -u -r1.53 -r1.54 src/usr.bin/make/buf.c
cvs rdiff -u -r1.44 -r1.45 src/usr.bin/make/buf.h
cvs rdiff -u -r1.272 -r1.273 src/usr.bin/make/make.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/buf.c
diff -u src/usr.bin/make/buf.c:1.53 src/usr.bin/make/buf.c:1.54
--- src/usr.bin/make/buf.c:1.53	Sun Nov 28 22:48:06 2021
+++ src/usr.bin/make/buf.c	Wed Dec 15 09:29:55 2021
@@ -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

Index: src/usr.bin/make/buf.h
diff -u src/usr.bin/make/buf.h:1.44 src/usr.bin/make/buf.h:1.45
--- src/usr.bin/make/buf.h:1.44	Sun Nov 28 22:48:06 2021
+++ src/usr.bin/make/buf.h	Wed Dec 15 09:29:55 2021
@@ -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 */

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.272 src/usr.bin/make/make.h:1.273
--- src/usr.bin/make/make.h:1.272	Mon Dec 13 22:26:21 2021
+++ src/usr.bin/make/make.h	Wed Dec 15 09:29:55 2021
@@ -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. */

Reply via email to