Module Name:    othersrc
Committed By:   dholland
Date:           Sat Mar 23 18:12:12 UTC 2013

Modified Files:
        othersrc/usr.bin/dholland-make2: suff.c

Log Message:
Rewrite SuffSuffIsSuffix to avoid potential undefined behavior.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 othersrc/usr.bin/dholland-make2/suff.c

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

Modified files:

Index: othersrc/usr.bin/dholland-make2/suff.c
diff -u othersrc/usr.bin/dholland-make2/suff.c:1.15 othersrc/usr.bin/dholland-make2/suff.c:1.16
--- othersrc/usr.bin/dholland-make2/suff.c:1.15	Sat Mar 23 18:02:58 2013
+++ othersrc/usr.bin/dholland-make2/suff.c	Sat Mar 23 18:12:12 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: suff.c,v 1.15 2013/03/23 18:02:58 dholland Exp $	*/
+/*	$NetBSD: suff.c,v 1.16 2013/03/23 18:12:12 dholland Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -131,7 +131,7 @@
 #include	  "hash.h"
 #include	  "dir.h"
 
-MAKE_RCSID("$NetBSD: suff.c,v 1.15 2013/03/23 18:02:58 dholland Exp $");
+MAKE_RCSID("$NetBSD: suff.c,v 1.16 2013/03/23 18:12:12 dholland Exp $");
 
 #define CLEANUP
 
@@ -257,44 +257,27 @@ SuffStrIsPrefix(const char *pref, const 
     return (*pref ? NULL : str);
 }
 
-/*-
- *-----------------------------------------------------------------------
- * SuffSuffIsSuffix  --
- *	See if suff is a suffix of str. sd->ename should point to THE END
- *	of the string to check. (THE END == the null byte)
- *
- * Input:
- *	s		possible suffix
- *	sd		string to examine
- *
- * Results:
- *	NULL if it ain't, pointer to character in str before suffix if
- *	it is.
- *
- * Side Effects:
- *	None
- *-----------------------------------------------------------------------
+/*
+ * Check if SUFF is a suffix of NAME. If so, return a pointer to
+ * the position of the suffix in NAME; otherwise, NULL.
  */
 static const char *
-SuffSuffIsSuffix(const Suff *s, const char *name)
+SuffSuffIsSuffix(const Suff *suff, const char *name)
 {
     size_t namelen;
-    const char  *p1;	    	/* Pointer into suffix name */
-    const char  *p2;	    	/* Pointer into string being examined */
+    const char *candidate;
 
     namelen = strlen(name);
-    if (namelen < s->nameLen)
-	return NULL;		/* this string is shorter than the suffix */
-
-    p1 = s->name + s->nameLen;
-    p2 = name + namelen;
-
-    while (p1 >= s->name && *p1 == *p2) {
-	p1--;
-	p2--;
+    if (namelen < suff->nameLen) {
+	/* this string is shorter than the suffix */
+	return NULL;
     }
 
-    return (p1 == s->name - 1 ? p2 : NULL);
+    candidate = name + namelen - suff->nameLen;
+    if (!strcmp(candidate, suff->name)) {
+	    return candidate;
+    }
+    return NULL;
 }
 
 /*-
@@ -789,7 +772,7 @@ SuffRebuildGraph(GNode *transform, Suff 
     const char 	*cp;
     Suff  	*s2;
     unsigned i, numsuffixes;
-    size_t matchlen;
+    size_t prefixlen;
 
     numsuffixes = sufflist_num(&sufflist);
 
@@ -817,19 +800,11 @@ SuffRebuildGraph(GNode *transform, Suff 
      */
     cp = SuffSuffIsSuffix(s, transform->name);
     if (cp != NULL) {
-	/*
-	 * For reasons best known to whoever wrote it,
-	 * SuffSuffIsSuffix returns one less than the pointer to
-	 * the suffix inside transform->name, instead of the
-	 * pointer to the suffix.
-	 */
-	cp++;
-
-	matchlen = cp - transform->name;
+	prefixlen = cp - transform->name;
 	for (i=0; i<numsuffixes; i++) {
 	    s2 = sufflist_get(&sufflist, i);
-	    if (s2->nameLen == matchlen &&
-		!memcmp(s2->name, transform->name, matchlen)) {
+	    if (s2->nameLen == prefixlen &&
+		!memcmp(s2->name, transform->name, prefixlen)) {
 		/*
 		 * Found it -- establish the proper relationship
 		 */
@@ -2102,7 +2077,7 @@ SuffFindNormalDeps(GNode *gn, struct src
 
     for (pos = 0; pos < num; pos++) {
 	suff = sufflist_get(&sufflist, pos);
-	if (SuffSuffIsSuffix(suff, gn->name)) {
+	if (SuffSuffIsSuffix(suff, gn->name) != NULL) {
 
 	    /*
 	     * Next possible suffix...

Reply via email to