Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): clean up Suffix_GetSuffix



details:   https://anonhg.NetBSD.org/src/rev/a9b44ee9af9e
branches:  trunk
changeset: 957238:a9b44ee9af9e
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sat Nov 21 23:09:07 2020 +0000

description:
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.

diffstat:

 usr.bin/make/suff.c |  26 +++++++++++---------------
 1 files changed, 11 insertions(+), 15 deletions(-)

diffs (50 lines):

diff -r e1ad6801a868 -r a9b44ee9af9e usr.bin/make/suff.c
--- a/usr.bin/make/suff.c       Sat Nov 21 23:00:02 2020 +0000
+++ b/usr.bin/make/suff.c       Sat Nov 21 23:09:07 2020 +0000
@@ -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 @@
 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 */
+    if (nameLen < suffLen)
+       return NULL;
 
-    p1 = suff->name + suff->nameLen;
-    p2 = nameEnd;
-
-    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



Home | Main Index | Thread Index | Old Index