Module Name:    src
Committed By:   rillig
Date:           Sat Aug  1 21:40:50 UTC 2020

Modified Files:
        src/usr.bin/make: buf.c buf.h cond.c for.c main.c var.c

Log Message:
make(1): switch Buffer size from int to size_t

This change helps to make the various integer types compatible and is a
preparational step for setting WARNS=6 in the Makefile.

The documentation of buf.c has been cleaned up and condensed since it
was mostly redundant, and some statements were even slightly wrong.

All code changes are covered by the existing unit tests, except for the
few lines in for.c around for_var_len.  These changes have been reviewed
thoroughly and manually, like all the others in this commit.

Those buffer functions that deal with sizes have been renamed by
appending a Z, to make sure that no function call was accidentally
forgotten.  They will be renamed back in a follow-up commit.

As usual, the scope of a few affected variables has been reduced, and
some variables had to be split since they had been incorrectly merged
before.

The order of the arguments to Buf_AddBytes has changed from (mem_len,
mem) to (mem, mem_len), in order to make it consistent with the
functions from the C standard library, such as snprintf.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/usr.bin/make/buf.c
cvs rdiff -u -r1.21 -r1.22 src/usr.bin/make/buf.h
cvs rdiff -u -r1.86 -r1.87 src/usr.bin/make/cond.c
cvs rdiff -u -r1.58 -r1.59 src/usr.bin/make/for.c
cvs rdiff -u -r1.293 -r1.294 src/usr.bin/make/main.c
cvs rdiff -u -r1.387 -r1.388 src/usr.bin/make/var.c

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.28 src/usr.bin/make/buf.c:1.29
--- src/usr.bin/make/buf.c:1.28	Sun Jul 26 15:09:10 2020
+++ src/usr.bin/make/buf.c	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 rillig Exp $	*/
+/*	$NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,40 +70,31 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 rillig Exp $";
+static char rcsid[] = "$NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)buf.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: buf.c,v 1.28 2020/07/26 15:09:10 rillig Exp $");
+__RCSID("$NetBSD: buf.c,v 1.29 2020/08/01 21:40:49 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
 
-/*-
- * buf.c --
- *	Functions for automatically-expanded NUL-terminated buffers.
- */
+/* Functions for automatically-expanded NUL-terminated buffers. */
 
 #include <limits.h>
 #include "make.h"
 #include "buf.h"
 
 #ifndef max
-#define max(a,b)  ((a) > (b) ? (a) : (b))
+#define max(a, b)  ((a) > (b) ? (a) : (b))
 #endif
 
 #define BUF_DEF_SIZE	256 	/* Default buffer size */
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_Expand_1 --
- *	Extend buffer for single byte add.
- *
- *-----------------------------------------------------------------------
- */
+/* Extend the buffer for adding a single byte. */
 void
 Buf_Expand_1(Buffer *bp)
 {
@@ -111,55 +102,38 @@ Buf_Expand_1(Buffer *bp)
     bp->buffer = bmake_realloc(bp->buffer, bp->size);
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_AddBytes --
- *	Add a number of bytes to the buffer.
- *
- * Results:
- *	None.
- *
- * Side Effects:
- *	Guess what?
- *
- *-----------------------------------------------------------------------
- */
+/* Add the given bytes to the buffer. */
 void
-Buf_AddBytes(Buffer *bp, int numBytes, const Byte *bytesPtr)
+Buf_AddBytesZ(Buffer *bp, const Byte *bytesPtr, size_t numBytes)
 {
-    int count = bp->count;
-    Byte *ptr;
+    size_t count = bp->count;
 
     if (__predict_false(count + numBytes >= bp->size)) {
 	bp->size += max(bp->size, numBytes + 16);
 	bp->buffer = bmake_realloc(bp->buffer, bp->size);
     }
 
-    ptr = bp->buffer + count;
+    Byte *ptr = bp->buffer + count;
     bp->count = count + numBytes;
-    ptr[numBytes] = 0;
     memcpy(ptr, bytesPtr, numBytes);
+    ptr[numBytes] = '\0';
 }
 
+/* Add the bytes between start and end to the buffer. */
 void
 Buf_AddBytesBetween(Buffer *bp, const char *start, const char *end)
 {
-    Buf_AddBytes(bp, (int)(end - start), start);
+    Buf_AddBytesZ(bp, start, (size_t)(end - start));
 }
 
+/* Add the given string to the buffer. */
 void
 Buf_AddStr(Buffer *bp, const char *str)
 {
-    Buf_AddBytes(bp, (int)strlen(str), str);
+    Buf_AddBytesZ(bp, str, strlen(str));
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_AddInt --
- *	Add the given number to the buffer.
- *
- *-----------------------------------------------------------------------
- */
+/* Add the given number to the buffer. */
 void
 Buf_AddInt(Buffer *bp, int n)
 {
@@ -171,74 +145,36 @@ Buf_AddInt(Buffer *bp, int n)
     size_t bits = sizeof(int) * CHAR_BIT;
     char buf[1 + (bits + 2) / 3 + 1];
 
-    int len = snprintf(buf, sizeof buf, "%d", n);
-    Buf_AddBytes(bp, len, buf);
+    size_t len = (size_t)snprintf(buf, sizeof buf, "%d", n);
+    Buf_AddBytesZ(bp, buf, len);
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_GetAll --
- *	Get all the available data at once.
- *
- * Results:
- *	A pointer to the data and the number of bytes available.
+/* Get the data (usually a string) from the buffer.
+ * The returned data is valid until the next modifying operation
+ * on the buffer.
  *
- * Side Effects:
- *	None.
- *
- *-----------------------------------------------------------------------
- */
+ * Returns the pointer to the data and optionally the length of the
+ * data in the buffer. */
 Byte *
-Buf_GetAll(Buffer *bp, int *numBytesPtr)
+Buf_GetAllZ(Buffer *bp, size_t *numBytesPtr)
 {
-
     if (numBytesPtr != NULL)
 	*numBytesPtr = bp->count;
-
     return bp->buffer;
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_Empty --
- *	Throw away bytes in a buffer.
- *
- * Results:
- *	None.
- *
- * Side Effects:
- *	The bytes are discarded.
- *
- *-----------------------------------------------------------------------
- */
+/* Mark the buffer as empty, so it can be filled with data again. */
 void
 Buf_Empty(Buffer *bp)
 {
-
     bp->count = 0;
-    *bp->buffer = 0;
+    bp->buffer[0] = '\0';
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_Init --
- *	Initialize a buffer. If no initial size is given, a reasonable
- *	default is used.
- *
- * Input:
- *	size		Initial size for the buffer
- *
- * Results:
- *	A buffer to be given to other functions in this library.
- *
- * Side Effects:
- *	The buffer is created, the space allocated and pointers
- *	initialized.
- *
- *-----------------------------------------------------------------------
- */
+/* Initialize a buffer.
+ * If the given initial size is 0, a reasonable default is used. */
 void
-Buf_Init(Buffer *bp, int size)
+Buf_InitZ(Buffer *bp, size_t size)
 {
     if (size <= 0) {
 	size = BUF_DEF_SIZE;
@@ -249,29 +185,13 @@ Buf_Init(Buffer *bp, int size)
     *bp->buffer = 0;
 }
 
-/*-
- *-----------------------------------------------------------------------
- * Buf_Destroy --
- *	Nuke a buffer and all its resources.
- *
- * Input:
- *	buf		Buffer to destroy
- *	freeData	TRUE if the data should be destroyed
- *
- * Results:
- *	Data buffer, NULL if freed
- *
- * Side Effects:
- *	The buffer is freed.
- *
- *-----------------------------------------------------------------------
- */
+/* Reset the buffer.
+ * If freeData is TRUE, the data from the buffer is freed as well.
+ * Otherwise it is kept and returned. */
 Byte *
 Buf_Destroy(Buffer *buf, Boolean freeData)
 {
-    Byte *data;
-
-    data = buf->buffer;
+    Byte *data = buf->buffer;
     if (freeData) {
 	free(data);
 	data = NULL;
@@ -284,38 +204,22 @@ Buf_Destroy(Buffer *buf, Boolean freeDat
     return data;
 }
 
-
-/*-
- *-----------------------------------------------------------------------
- * Buf_DestroyCompact --
- *	Nuke a buffer and return its data.
- *
- * Input:
- *	buf		Buffer to destroy
- *
- * Results:
- *	Data buffer
- *
- * Side Effects:
- *	If the buffer size is much greater than its content,
- *	a new buffer will be allocated and the old one freed.
- *
- *-----------------------------------------------------------------------
- */
 #ifndef BUF_COMPACT_LIMIT
 # define BUF_COMPACT_LIMIT 128          /* worthwhile saving */
 #endif
 
+/* Reset the buffer and return its data.
+ *
+ * If the buffer size is much greater than its content,
+ * a new buffer will be allocated and the old one freed. */
 Byte *
 Buf_DestroyCompact(Buffer *buf)
 {
 #if BUF_COMPACT_LIMIT > 0
-    Byte *data;
-
     if (buf->size - buf->count >= BUF_COMPACT_LIMIT) {
 	/* We trust realloc to be smart */
-	data = bmake_realloc(buf->buffer, buf->count + 1);
-	if (data) {
+	Byte *data = bmake_realloc(buf->buffer, buf->count + 1);
+	if (data) {	/* XXX: can never be NULL */
 	    data[buf->count] = 0;
 	    Buf_Destroy(buf, FALSE);
 	    return data;

Index: src/usr.bin/make/buf.h
diff -u src/usr.bin/make/buf.h:1.21 src/usr.bin/make/buf.h:1.22
--- src/usr.bin/make/buf.h:1.21	Sun Jul 26 15:09:10 2020
+++ src/usr.bin/make/buf.h	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.h,v 1.21 2020/07/26 15:09:10 rillig Exp $	*/
+/*	$NetBSD: buf.h,v 1.22 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -80,23 +80,25 @@
 #ifndef MAKE_BUF_H
 #define MAKE_BUF_H
 
+#include <stddef.h>
+
 typedef char Byte;
 
 typedef struct Buffer {
-    int	    size; 	/* Current size of the buffer */
-    int     count;	/* Number of bytes in buffer */
-    Byte    *buffer;	/* The buffer itself (zero terminated) */
+    size_t size;	/* Current size of the buffer */
+    size_t count;	/* Number of bytes in buffer */
+    Byte *buffer;	/* The buffer itself (zero terminated) */
 } Buffer;
 
-/* If we aren't on netbsd, __predict_false() might not be defined. */
+/* If we aren't on NetBSD, __predict_false() might not be defined. */
 #ifndef __predict_false
 #define __predict_false(x) (x)
 #endif
 
 /* Buf_AddByte adds a single byte to a buffer. */
 #define	Buf_AddByte(bp, byte) do { \
-	int _count = ++(bp)->count; \
-	char *_ptr; \
+	size_t _count = ++(bp)->count; \
+	Byte *_ptr; \
 	if (__predict_false(_count >= (bp)->size)) \
 		Buf_Expand_1(bp); \
 	_ptr = (bp)->buffer + _count; \
@@ -109,13 +111,13 @@ typedef struct Buffer {
 #define Buf_Size(bp) ((bp)->count)
 
 void Buf_Expand_1(Buffer *);
-void Buf_AddBytes(Buffer *, int, const Byte *);
+void Buf_AddBytesZ(Buffer *, const Byte *, size_t);
 void Buf_AddBytesBetween(Buffer *, const Byte *, const Byte *);
 void Buf_AddStr(Buffer *, const char *);
 void Buf_AddInt(Buffer *, int);
-Byte *Buf_GetAll(Buffer *, int *);
+Byte *Buf_GetAllZ(Buffer *, size_t *);
 void Buf_Empty(Buffer *);
-void Buf_Init(Buffer *, int);
+void Buf_InitZ(Buffer *, size_t);
 Byte *Buf_Destroy(Buffer *, Boolean);
 Byte *Buf_DestroyCompact(Buffer *);
 

Index: src/usr.bin/make/cond.c
diff -u src/usr.bin/make/cond.c:1.86 src/usr.bin/make/cond.c:1.87
--- src/usr.bin/make/cond.c:1.86	Sat Aug  1 18:02:37 2020
+++ src/usr.bin/make/cond.c	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: cond.c,v 1.86 2020/08/01 18:02:37 rillig Exp $	*/
+/*	$NetBSD: cond.c,v 1.87 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -70,14 +70,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: cond.c,v 1.86 2020/08/01 18:02:37 rillig Exp $";
+static char rcsid[] = "$NetBSD: cond.c,v 1.87 2020/08/01 21:40:49 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)cond.c	8.2 (Berkeley) 1/2/94";
 #else
-__RCSID("$NetBSD: cond.c,v 1.86 2020/08/01 18:02:37 rillig Exp $");
+__RCSID("$NetBSD: cond.c,v 1.87 2020/08/01 21:40:49 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -238,14 +238,13 @@ static int
 CondGetArg(Boolean doEval, char **linePtr, char **argPtr, const char *func)
 {
     char	  *cp;
-    int	    	  argLen;
     Buffer	  buf;
     int           paren_depth;
     char          ch;
 
     cp = *linePtr;
     if (func != NULL)
-	/* Skip opening '(' - verfied by caller */
+	/* Skip opening '(' - verified by caller */
 	cp++;
 
     if (*cp == '\0') {
@@ -267,7 +266,7 @@ CondGetArg(Boolean doEval, char **linePt
      * Create a buffer for the argument and start it out at 16 characters
      * long. Why 16? Why not?
      */
-    Buf_Init(&buf, 16);
+    Buf_InitZ(&buf, 16);
 
     paren_depth = 0;
     for (;;) {
@@ -287,7 +286,7 @@ CondGetArg(Boolean doEval, char **linePt
 	    void	*freeIt;
 	    VarEvalFlags eflags = VARE_UNDEFERR | (doEval ? VARE_WANTRES : 0);
 	    const char *cp2 = Var_Parse(cp, VAR_CMD, eflags, &len, &freeIt);
-	    Buf_AddBytes(&buf, strlen(cp2), cp2);
+	    Buf_AddStr(&buf, cp2);
 	    free(freeIt);
 	    cp += len;
 	    continue;
@@ -301,7 +300,8 @@ CondGetArg(Boolean doEval, char **linePt
 	cp++;
     }
 
-    *argPtr = Buf_GetAll(&buf, &argLen);
+    size_t argLen;
+    *argPtr = Buf_GetAllZ(&buf, &argLen);
     Buf_Destroy(&buf, FALSE);
 
     while (*cp == ' ' || *cp == '\t') {
@@ -528,7 +528,7 @@ CondGetString(Boolean doEval, Boolean *q
     int qt;
     char *start;
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
     str = NULL;
     *freeIt = NULL;
     *quoted = qt = *condExpr == '"' ? 1 : 0;
@@ -619,7 +619,7 @@ CondGetString(Boolean doEval, Boolean *q
 	}
     }
  got_str:
-    *freeIt = Buf_GetAll(&buf, NULL);
+    *freeIt = Buf_GetAllZ(&buf, NULL);
     str = *freeIt;
  cleanup:
     Buf_Destroy(&buf, FALSE);

Index: src/usr.bin/make/for.c
diff -u src/usr.bin/make/for.c:1.58 src/usr.bin/make/for.c:1.59
--- src/usr.bin/make/for.c:1.58	Sat Aug  1 14:47:49 2020
+++ src/usr.bin/make/for.c	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: for.c,v 1.58 2020/08/01 14:47:49 rillig Exp $	*/
+/*	$NetBSD: for.c,v 1.59 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1992, The Regents of the University of California.
@@ -30,14 +30,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: for.c,v 1.58 2020/08/01 14:47:49 rillig Exp $";
+static char rcsid[] = "$NetBSD: for.c,v 1.59 2020/08/01 21:40:49 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)for.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: for.c,v 1.58 2020/08/01 14:47:49 rillig Exp $");
+__RCSID("$NetBSD: for.c,v 1.59 2020/08/01 21:40:49 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -270,7 +270,7 @@ For_Eval(char *line)
 	}
     }
 
-    Buf_Init(&new_for->buf, 0);
+    Buf_InitZ(&new_for->buf, 0);
     accumFor = new_for;
     forLevel = 1;
     return 1;
@@ -305,7 +305,7 @@ For_Accum(char *line)
 	}
     }
 
-    Buf_AddBytes(&accumFor->buf, strlen(line), line);
+    Buf_AddStr(&accumFor->buf, line);
     Buf_AddByte(&accumFor->buf, '\n');
     return 1;
 }
@@ -325,12 +325,10 @@ For_Accum(char *line)
  *-----------------------------------------------------------------------
  */
 
-static int
+static size_t
 for_var_len(const char *var)
 {
     char ch, var_start, var_end;
-    int depth;
-    int len;
 
     var_start = *var;
     if (var_start == 0)
@@ -345,7 +343,8 @@ for_var_len(const char *var)
 	/* Single char variable */
 	return 1;
 
-    depth = 1;
+    int depth = 1;
+    size_t len;
     for (len = 1; (ch = var[len++]) != 0;) {
 	if (ch == var_start)
 	    depth++;
@@ -361,23 +360,22 @@ static void
 for_substitute(Buffer *cmds, strlist_t *items, unsigned int item_no, char ech)
 {
     const char *item = strlist_str(items, item_no);
-    int len;
-    char ch;
 
     /* If there were no escapes, or the only escape is the other variable
      * terminator, then just substitute the full string */
     if (!(strlist_info(items, item_no) &
 	    (ech == ')' ? ~FOR_SUB_ESCAPE_BRACE : ~FOR_SUB_ESCAPE_PAREN))) {
-	Buf_AddBytes(cmds, strlen(item), item);
+	Buf_AddStr(cmds, item);
 	return;
     }
 
     /* Escape ':', '$', '\\' and 'ech' - removed by :U processing */
+    char ch;
     while ((ch = *item++) != 0) {
 	if (ch == '$') {
-	    len = for_var_len(item);
+	    size_t len = for_var_len(item);
 	    if (len != 0) {
-		Buf_AddBytes(cmds, len + 1, item - 1);
+		Buf_AddBytesZ(cmds, item - 1, len + 1);
 		item += len;
 		continue;
 	    }
@@ -392,7 +390,7 @@ static char *
 For_Iterate(void *v_arg, size_t *ret_len)
 {
     For *arg = v_arg;
-    int i, len;
+    int i;
     char *var;
     char *cp;
     char *cmd_cp;
@@ -421,9 +419,10 @@ For_Iterate(void *v_arg, size_t *ret_len
      * to contrive a makefile where an unwanted substitution happens.
      */
 
-    cmd_cp = Buf_GetAll(&arg->buf, &len);
-    body_end = cmd_cp + len;
-    Buf_Init(&cmds, len + 256);
+    size_t cmd_len;
+    cmd_cp = Buf_GetAllZ(&arg->buf, &cmd_len);
+    body_end = cmd_cp + cmd_len;
+    Buf_InitZ(&cmds, cmd_len + 256);
     for (cp = cmd_cp; (cp = strchr(cp, '$')) != NULL;) {
 	char ech;
 	ch = *++cp;
@@ -431,15 +430,15 @@ For_Iterate(void *v_arg, size_t *ret_len
 	    cp++;
 	    /* Check variable name against the .for loop variables */
 	    STRLIST_FOREACH(var, &arg->vars, i) {
-		len = strlist_info(&arg->vars, i);
-		if (memcmp(cp, var, len) != 0)
+		size_t vlen = strlist_info(&arg->vars, i);
+		if (memcmp(cp, var, vlen) != 0)
 		    continue;
-		if (cp[len] != ':' && cp[len] != ech && cp[len] != '\\')
+		if (cp[vlen] != ':' && cp[vlen] != ech && cp[vlen] != '\\')
 		    continue;
 		/* Found a variable match. Replace with :U<value> */
-		Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
-		Buf_AddBytes(&cmds, 2, ":U");
-		cp += len;
+		Buf_AddBytesBetween(&cmds, cmd_cp, cp);
+		Buf_AddStr(&cmds, ":U");
+		cp += vlen;
 		cmd_cp = cp;
 		for_substitute(&cmds, &arg->items, arg->sub_next + i, ech);
 		break;
@@ -457,15 +456,15 @@ For_Iterate(void *v_arg, size_t *ret_len
 	    if (var[0] != ch || var[1] != 0)
 		continue;
 	    /* Found a variable match. Replace with ${:U<value>} */
-	    Buf_AddBytes(&cmds, cp - cmd_cp, cmd_cp);
-	    Buf_AddBytes(&cmds, 3, "{:U");
+	    Buf_AddBytesBetween(&cmds, cmd_cp, cp);
+	    Buf_AddStr(&cmds, "{:U");
 	    cmd_cp = ++cp;
 	    for_substitute(&cmds, &arg->items, arg->sub_next + i, /*{*/ '}');
-	    Buf_AddBytes(&cmds, 1, "}");
+	    Buf_AddByte(&cmds, '}');
 	    break;
 	}
     }
-    Buf_AddBytes(&cmds, body_end - cmd_cp, cmd_cp);
+    Buf_AddBytesBetween(&cmds, cmd_cp, body_end);
 
     cp = Buf_Destroy(&cmds, FALSE);
     if (DEBUG(FOR))

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.293 src/usr.bin/make/main.c:1.294
--- src/usr.bin/make/main.c:1.293	Sat Aug  1 17:45:32 2020
+++ src/usr.bin/make/main.c	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.293 2020/08/01 17:45:32 rillig Exp $	*/
+/*	$NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.293 2020/08/01 17:45:32 rillig Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.293 2020/08/01 17:45:32 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.294 2020/08/01 21:40:49 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1599,7 +1599,6 @@ Cmd_Exec(const char *cmd, const char **e
     int		status;		/* command exit status */
     Buffer	buf;		/* buffer to store the result */
     char	*cp;
-    int		cc;		/* bytes read, or -1 */
     int		savederr;	/* saved errno */
 
 
@@ -1658,13 +1657,15 @@ Cmd_Exec(const char *cmd, const char **e
 	(void)close(fds[1]);
 
 	savederr = 0;
-	Buf_Init(&buf, 0);
+	Buf_InitZ(&buf, 0);
 
+	/* XXX: split variable cc into 2 */
+	ssize_t cc;		/* bytes read, or -1 */
 	do {
 	    char   result[BUFSIZ];
 	    cc = read(fds[0], result, sizeof(result));
 	    if (cc > 0)
-		Buf_AddBytes(&buf, cc, result);
+		Buf_AddBytesZ(&buf, result, (size_t)cc);
 	}
 	while (cc > 0 || (cc == -1 && errno == EINTR));
 	if (cc == -1)
@@ -1682,7 +1683,7 @@ Cmd_Exec(const char *cmd, const char **e
 	    JobReapChild(pid, status, FALSE);
 	    continue;
 	}
-	cc = Buf_Size(&buf);
+	cc = (/* XXX */ssize_t)Buf_Size(&buf);
 	res = Buf_Destroy(&buf, FALSE);
 
 	if (savederr != 0)

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.387 src/usr.bin/make/var.c:1.388
--- src/usr.bin/make/var.c:1.387	Sat Aug  1 19:19:05 2020
+++ src/usr.bin/make/var.c	Sat Aug  1 21:40:49 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.387 2020/08/01 19:19:05 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.388 2020/08/01 21:40:49 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.387 2020/08/01 19:19:05 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.388 2020/08/01 21:40:49 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.387 2020/08/01 19:19:05 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.388 2020/08/01 21:40:49 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -355,9 +355,9 @@ VarFind(const char *name, GNode *ctxt, V
 	    Var *v = bmake_malloc(sizeof(Var));
 	    v->name = bmake_strdup(name);
 
-	    int len = (int)strlen(env);
-	    Buf_Init(&v->val, len + 1);
-	    Buf_AddBytes(&v->val, len, env);
+	    size_t len = strlen(env);
+	    Buf_InitZ(&v->val, len + 1);
+	    Buf_AddBytesZ(&v->val, env, len);
 
 	    v->flags = VAR_FROM_ENV;
 	    return v;
@@ -431,9 +431,9 @@ VarAdd(const char *name, const char *val
 {
     Var *v = bmake_malloc(sizeof(Var));
 
-    int len = val != NULL ? (int)strlen(val) : 0;
-    Buf_Init(&v->val, len + 1);
-    Buf_AddBytes(&v->val, len, val);
+    size_t len = val != NULL ? strlen(val) : 0;
+    Buf_InitZ(&v->val, len + 1);
+    Buf_AddBytesZ(&v->val, val, len);
 
     v->flags = 0;
 
@@ -526,7 +526,7 @@ Var_Export1(const char *name, VarExportF
 	return 0;
     if (!parent && (v->flags & VAR_EXPORTED) && !(v->flags & VAR_REEXPORT))
 	return 0;		/* nothing to do */
-    val = Buf_GetAll(&v->val, NULL);
+    val = Buf_GetAllZ(&v->val, NULL);
     if (!(flags & VAR_EXPORT_LITERAL) && strchr(val, '$')) {
 	if (parent) {
 	    /*
@@ -952,7 +952,7 @@ Var_Append(const char *name, const char 
 
 	if (DEBUG(VAR)) {
 	    fprintf(debug_file, "%s:%s = %s\n", ctxt->name, name,
-		    Buf_GetAll(&v->val, NULL));
+		    Buf_GetAllZ(&v->val, NULL));
 	}
 
 	if (v->flags & VAR_FROM_ENV) {
@@ -1027,7 +1027,7 @@ Var_Value(const char *name, GNode *ctxt,
     if (v == NULL)
 	return NULL;
 
-    char *p = Buf_GetAll(&v->val, NULL);
+    char *p = Buf_GetAllZ(&v->val, NULL);
     if (VarFreeEnv(v, FALSE))
 	*freeIt = p;
     return p;
@@ -1044,7 +1044,7 @@ typedef struct {
 static void
 SepBuf_Init(SepBuf *buf, char sep)
 {
-    Buf_Init(&buf->buf, 32 /* bytes */);
+    Buf_InitZ(&buf->buf, 32 /* bytes */);
     buf->needSep = FALSE;
     buf->sep = sep;
 }
@@ -1064,7 +1064,7 @@ SepBuf_AddBytes(SepBuf *buf, const char 
 	Buf_AddByte(&buf->buf, buf->sep);
 	buf->needSep = FALSE;
     }
-    Buf_AddBytes(&buf->buf, mem_size, mem);
+    Buf_AddBytesZ(&buf->buf, mem, mem_size);
 }
 
 static void
@@ -1648,7 +1648,7 @@ VarOrder(const char *str, const char oty
     char *as;			/* word list memory */
     int ac, i;
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
 
     av = brk_string(str, &ac, FALSE, &as);
 
@@ -1698,7 +1698,7 @@ VarUniq(const char *str)
     char 	 *as;		/* Word list memory */
     int 	  ac, i, j;
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
     av = brk_string(str, &ac, FALSE, &as);
 
     if (ac > 1) {
@@ -1752,7 +1752,7 @@ ParseModifierPart(const char **tstr, int
     Buffer buf;
     VarEvalFlags errnum = eflags & VARE_UNDEFERR;
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
 
     /*
      * Skim through until the matching delimiter is found;
@@ -1817,7 +1817,7 @@ ParseModifierPart(const char **tstr, int
 		}
 	    }
 	} else if (subst != NULL && *cp == '&')
-	    Buf_AddBytes(&buf, subst->lhsLen, subst->lhs);
+	    Buf_AddBytesZ(&buf, subst->lhs, subst->lhsLen);
 	else
 	    Buf_AddByte(&buf, *cp);
     }
@@ -1854,7 +1854,7 @@ static char *
 VarQuote(char *str, Boolean quoteDollar)
 {
     Buffer buf;
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
 
     for (; *str != '\0'; str++) {
 	if (*str == '\n') {
@@ -1944,7 +1944,7 @@ VarHash(const char *str)
     h *= 0xc2b2ae35;
     h ^= h >> 16;
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
     for (len = 0; len < 8; ++len) {
 	Buf_AddByte(&buf, hexdigits[h & 15]);
 	h >>= 4;
@@ -2084,7 +2084,7 @@ ApplyModifier_Defined(const char *mod, A
      * the delimiter (expand the variable substitution).
      * The result is left in the Buffer buf.
      */
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
     const char *p = mod + 1;
     while (*p != st->endc && *p != ':' && *p != '\0') {
 	if (*p == '\\' &&
@@ -2249,7 +2249,7 @@ ApplyModifier_Range(const char *mod, App
     }
 
     Buffer buf;
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
 
     int i;
     for (i = 0; i < n; i++) {
@@ -2610,7 +2610,7 @@ ApplyModifier_Words(const char *mod, App
 	    free(av);
 
 	    Buffer buf;
-	    Buf_Init(&buf, 4);	/* 3 digits + '\0' */
+	    Buf_InitZ(&buf, 4);	/* 3 digits + '\0' */
 	    Buf_AddInt(&buf, ac);
 	    st->newVal = Buf_Destroy(&buf, FALSE);
 	}
@@ -3424,15 +3424,15 @@ Var_Parse(const char * const str, GNode 
 	    endc = str[1];
 	}
     } else {
-	Buffer namebuf;		/* Holds the variable name */
-	int depth = 1;
-
 	endc = startc == PROPEN ? PRCLOSE : BRCLOSE;
-	Buf_Init(&namebuf, 0);
+
+	Buffer namebuf;		/* Holds the variable name */
+	Buf_InitZ(&namebuf, 0);
 
 	/*
 	 * Skip to the end character or a colon, whichever comes first.
 	 */
+	int depth = 1;
 	for (tstr = str + 2; *tstr != '\0'; tstr++) {
 	    /* Track depth so we can spot parse errors. */
 	    if (*tstr == startc)
@@ -3461,7 +3461,7 @@ Var_Parse(const char * const str, GNode 
 	    haveModifier = FALSE;
 	} else {
 	    Parse_Error(PARSE_FATAL, "Unclosed variable \"%s\"",
-			Buf_GetAll(&namebuf, NULL));
+			Buf_GetAllZ(&namebuf, NULL));
 	    /*
 	     * If we never did find the end character, return NULL
 	     * right now, setting the length to be the distance to
@@ -3472,8 +3472,8 @@ Var_Parse(const char * const str, GNode 
 	    return var_Error;
 	}
 
-	int namelen;
-	char *varname = Buf_GetAll(&namebuf, &namelen);
+	size_t namelen;
+	char *varname = Buf_GetAllZ(&namebuf, &namelen);
 
 	/*
 	 * At this point, varname points into newly allocated memory from
@@ -3534,7 +3534,7 @@ Var_Parse(const char * const str, GNode 
 		 */
 		v = bmake_malloc(sizeof(Var));
 		v->name = varname;
-		Buf_Init(&v->val, 1);
+		Buf_InitZ(&v->val, 1);
 		v->flags = VAR_JUNK;
 		Buf_Destroy(&namebuf, FALSE);
 	    }
@@ -3557,7 +3557,7 @@ Var_Parse(const char * const str, GNode 
      * been dynamically-allocated, so it will need freeing when we
      * return.
      */
-    nstr = Buf_GetAll(&v->val, NULL);
+    nstr = Buf_GetAllZ(&v->val, NULL);
     if (strchr(nstr, '$') != NULL && (eflags & VARE_WANTRES) != 0) {
 	nstr = Var_Subst(nstr, ctxt, eflags);
 	*freePtr = nstr;
@@ -3589,7 +3589,7 @@ Var_Parse(const char * const str, GNode 
     *lengthPtr = tstr - str + (*tstr ? 1 : 0);
 
     if (v->flags & VAR_FROM_ENV) {
-	Boolean destroy = nstr != Buf_GetAll(&v->val, NULL);
+	Boolean destroy = nstr != Buf_GetAllZ(&v->val, NULL);
 	if (!destroy) {
 	    /*
 	     * Returning the value unmodified, so tell the caller to free
@@ -3616,7 +3616,7 @@ Var_Parse(const char * const str, GNode 
 		nstr = (eflags & VARE_UNDEFERR) ? var_Error : varNoError;
 	    }
 	}
-	if (nstr != Buf_GetAll(&v->val, NULL))
+	if (nstr != Buf_GetAllZ(&v->val, NULL))
 	    Buf_Destroy(&v->val, TRUE);
 	free(v->name);
 	free(v);
@@ -3658,7 +3658,7 @@ Var_Subst(const char *str, GNode *ctxt, 
 				 * been reported to prevent a plethora
 				 * of messages when recursing */
 
-    Buf_Init(&buf, 0);
+    Buf_InitZ(&buf, 0);
     errorReported = FALSE;
     trailingBslash = FALSE;
 
@@ -3730,9 +3730,9 @@ Var_Subst(const char *str, GNode *ctxt, 
 		 * Copy all the characters from the variable value straight
 		 * into the new string.
 		 */
-		length = strlen(val);
-		Buf_AddBytes(&buf, length, val);
-		trailingBslash = length > 0 && val[length - 1] == '\\';
+		size_t val_len = strlen(val);
+		Buf_AddBytesZ(&buf, val, val_len);
+		trailingBslash = val_len > 0 && val[val_len - 1] == '\\';
 	    }
 	    free(freeIt);
 	    freeIt = NULL;
@@ -3770,7 +3770,7 @@ static void
 VarPrintVar(void *vp, void *data MAKE_ATTR_UNUSED)
 {
     Var *v = (Var *)vp;
-    fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAll(&v->val, NULL));
+    fprintf(debug_file, "%-16s = %s\n", v->name, Buf_GetAllZ(&v->val, NULL));
 }
 
 /*-

Reply via email to