cifs.idmap: clean up strget and avoid memory allocation
authorJeff Layton <jlayton@samba.org>
Mon, 29 Oct 2012 19:45:37 +0000 (15:45 -0400)
committerJeff Layton <jlayton@samba.org>
Mon, 29 Oct 2012 19:45:37 +0000 (15:45 -0400)
Don't do a strlen() call if strstr() isn't going to match anyway.

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

Reviewed-by: Shirish Pargaonkar <shirishpargaonkar@gmail.com>
Signed-off-by: Jeff Layton <jlayton@samba.org>
cifs.idmap.c

index ea223009a28a51efc3bb0d52f1729e3b60018339..457d307c6c14b1dd8521cbdf8f66bd80b0edece5 100644 (file)
@@ -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;
 }