[PATCH 4/7] cifs.idmap: clean up strget and avoid memory allocation

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

 



Don't do a strlen() call if strstr() isn't going to match anyway. Just
use strdup instead of open-coding its equivalent and reduce the amount
of string-walking involved.

Finally, there's no need to duplicate the string here. None of the
callers modify it, so just return a pointer into the original string.

Signed-off-by: Jeff Layton <jlayton@xxxxxxxxx>
---
 cifs.idmap.c | 34 ++++++++++++++--------------------
 1 file changed, 14 insertions(+), 20 deletions(-)

diff --git a/cifs.idmap.c b/cifs.idmap.c
index ea22300..457d307 100644
--- a/cifs.idmap.c
+++ b/cifs.idmap.c
@@ -54,28 +54,25 @@ static void usage(void)
 	fprintf(stderr, "Usage: %s key_serial\n", prog);
 }
 
-char *strget(const char *str, char *substr)
+char *strget(const char *str, const char *substr)
 {
 	int len, sublen, retlen;
-	char *retstr, *substrptr;
+	char *substrptr;
 
-	sublen = strlen(substr);
+	/* find the prefix */
 	substrptr = strstr(str, substr);
-	if (substrptr) {
-		len = strlen(substrptr);
-		substrptr += sublen;
-
-		retlen = len - sublen;
-		if (retlen > 0) {
-			retstr = malloc(retlen + 1);
-			if (retstr) {
-				strncpy(retstr, substrptr, retlen);
-				return retstr;
-			}
-		}
-	}
+	if (!substrptr)
+		return substrptr;
 
-	return NULL;
+	/* skip over it */
+	sublen = strlen(substr);
+	substrptr += sublen;
+
+	/* if there's nothing after the prefix, return NULL */
+	if (*substrptr == '\0')
+		return NULL;
+
+	return substrptr;
 }
 
 static int
@@ -179,9 +176,6 @@ cifs_idmap(const key_serial_t key, const char *key_descr)
 	syslog(LOG_DEBUG, "Invalid key: %s", key_descr);
 
 cifs_idmap_ret:
-	if (sidstr)
-		free(sidstr);
-
 	return rc;
 }
 
-- 
1.7.11.7

--
To unsubscribe from this list: send the line "unsubscribe linux-cifs" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux