Module Name:    src
Committed By:   rillig
Date:           Sun Sep 27 16:38:32 UTC 2020

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

Log Message:
make(1): prefer positive array index in Buf_AddByte

Ideally the condition for allocating more memory would have been
(old_len + 2 > bp->cap) since that's the actually intended wording.  But
GCC 5 neglected to generate good code for that on x86_64, so be it.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/usr.bin/make/buf.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.h
diff -u src/usr.bin/make/buf.h:1.31 src/usr.bin/make/buf.h:1.32
--- src/usr.bin/make/buf.h:1.31	Sun Sep 27 16:21:06 2020
+++ src/usr.bin/make/buf.h	Sun Sep 27 16:38:32 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: buf.h,v 1.31 2020/09/27 16:21:06 rillig Exp $	*/
+/*	$NetBSD: buf.h,v 1.32 2020/09/27 16:38:32 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
@@ -97,13 +97,13 @@ void Buf_Expand_1(Buffer *);
 static inline MAKE_ATTR_UNUSED void
 Buf_AddByte(Buffer *bp, char byte)
 {
-    size_t new_len = ++bp->len;
-    char *end_plus_one;
-    if (__predict_false(new_len >= bp->cap))
+    size_t old_len = bp->len++;
+    char *end;
+    if (__predict_false(old_len + 1 >= bp->cap))
 	Buf_Expand_1(bp);
-    end_plus_one = bp->data + new_len;
-    end_plus_one[-1] = byte;
-    end_plus_one[0] = 0;
+    end = bp->data + old_len;
+    end[0] = byte;
+    end[1] = '\0';
 }
 
 static inline MAKE_ATTR_UNUSED size_t

Reply via email to