Module Name:    src
Committed By:   rillig
Date:           Sat Nov 21 23:09:07 UTC 2020

Modified Files:
        src/usr.bin/make: suff.c

Log Message:
make(1): clean up Suffix_GetSuffix

This eliminates the unspecific variable names p1 and p2 and furthermore
ensures that there is never a pointer that points out of bounds.

The code could have used memcmp or strncmp as well, but since the
suffixes are usuall very short, there is no benefit over a direct loop
comparing individual characters.


To generate a diff of this commit:
cvs rdiff -u -r1.278 -r1.279 src/usr.bin/make/suff.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/suff.c
diff -u src/usr.bin/make/suff.c:1.278 src/usr.bin/make/suff.c:1.279
--- src/usr.bin/make/suff.c:1.278	Sat Nov 21 22:00:34 2020
+++ src/usr.bin/make/suff.c	Sat Nov 21 23:09:07 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.278 2020/11/21 22:00:34 rillig Exp $	*/
+/*	$NetBSD: suff.c,v 1.279 2020/11/21 23:09:07 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -114,7 +114,7 @@
 #include "dir.h"
 
 /*	"@(#)suff.c	8.4 (Berkeley) 3/21/94"	*/
-MAKE_RCSID("$NetBSD: suff.c,v 1.278 2020/11/21 22:00:34 rillig Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.279 2020/11/21 23:09:07 rillig Exp $");
 
 #define SUFF_DEBUG0(text) DEBUG0(SUFF, text)
 #define SUFF_DEBUG1(fmt, arg1) DEBUG1(SUFF, fmt, arg1)
@@ -255,22 +255,18 @@ SuffStrIsPrefix(const char *pref, const 
 static const char *
 Suffix_GetSuffix(const Suffix *suff, size_t nameLen, const char *nameEnd)
 {
-    const char *p1;		/* Pointer into suffix name */
-    const char *p2;		/* Pointer into string being examined */
+    size_t suffLen = suff->nameLen;
+    const char *suffInName;
 
-    if (nameLen < suff->nameLen)
-	return NULL;		/* this string is shorter than the suffix */
-
-    p1 = suff->name + suff->nameLen;
-    p2 = nameEnd;
+    if (nameLen < suffLen)
+	return NULL;
 
-    while (p1 >= suff->name && *p1 == *p2) {
-	p1--;
-	p2--;
+    suffInName = nameEnd - suffLen;
+    for (size_t i = 0; i < suffLen; i++) {
+	if (suff->name[i] != suffInName[i])
+	    return NULL;
     }
-
-    /* XXX: s->name - 1 invokes undefined behavior */
-    return p1 == suff->name - 1 ? p2 + 1 : NULL;
+    return suffInName;
 }
 
 static Boolean

Reply via email to