X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/37f9805dabba76b7a00bd2a0227762f6f239b598..716baed7ff23f949861f0eb369c013408e17f984:/util.c diff --git a/util.c b/util.c index c5bb4a70..cadf3ebf 100644 --- a/util.c +++ b/util.c @@ -358,37 +358,6 @@ void kill_all(int sig) } } -/* like strncpy but does not 0 fill the buffer and always null - terminates. bufsize is the size of the destination buffer */ -size_t strlcpy(char *d, const char *s, size_t bufsize) -{ - size_t len = strlen(s); - size_t ret = len; - if (len >= bufsize) len = bufsize-1; - memcpy(d, s, len); - d[len] = 0; - return ret; -} - -/* like strncat but does not 0 fill the buffer and always null - terminates. bufsize is the length of the buffer, which should - be one more than the maximum resulting string length */ -size_t strlcat(char *d, const char *s, size_t bufsize) -{ - size_t len1 = strlen(d); - size_t len2 = strlen(s); - size_t ret = len1 + len2; - - if (len1+len2 >= bufsize) { - len2 = bufsize - (len1+1); - } - if (len2 > 0) { - memcpy(d+len1, s, len2); - d[len1+len2] = 0; - } - return ret; -} - /* turn a user name into a uid */ int name_to_uid(char *name, uid_t *uid) { @@ -431,7 +400,7 @@ int lock_range(int fd, int offset, int len) } -static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) +static void glob_expand_one(char *s, char **argv, int *argc, int maxargs, int sanitize_paths) { #if !(defined(HAVE_GLOB) && defined(HAVE_GLOB_H)) if (!*s) s = "."; @@ -444,7 +413,9 @@ static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) if (!*s) s = "."; - argv[*argc] = strdup(s); + s = strdup(s); + sanitize_path(s); + argv[*argc] = s; memset(&globbuf, 0, sizeof(globbuf)); glob(argv[*argc], 0, NULL, &globbuf); @@ -463,7 +434,7 @@ static void glob_expand_one(char *s, char **argv, int *argc, int maxargs) #endif } -void glob_expand(char *base1, char **argv, int *argc, int maxargs) +void glob_expand(char *base1, char **argv, int *argc, int maxargs, int sanitize_paths) { char *s = argv[*argc]; char *p, *q; @@ -487,11 +458,11 @@ void glob_expand(char *base1, char **argv, int *argc, int maxargs) while ((p = strstr(q,base)) && ((*argc) < maxargs)) { /* split it at this point */ *p = 0; - glob_expand_one(q, argv, argc, maxargs); + glob_expand_one(q, argv, argc, maxargs, sanitize_paths); q = p+strlen(base); } - if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs); + if (*q && (*argc < maxargs)) glob_expand_one(q, argv, argc, maxargs, sanitize_paths); free(s); free(base); @@ -592,23 +563,23 @@ void clean_fname(char *name) * While we're at it, remove double slashes and "." components like * clean_fname does(), but DON'T remove a trailing slash because that * is sometimes significant on command line arguments. - * Return a malloc'ed copy. + * Can only shrink paths, so sanitizes in place. * Contributed by Dave Dykstra */ -char *sanitize_path(char *p) +void sanitize_path(char *p) { - char *copy, *copyp; + char *start, *sanp; - copy = (char *) malloc(strlen(p)+1); - copyp = copy; + start = p; + sanp = p; while (*p == '/') { /* remove leading slashes */ p++; } while (*p != '\0') { /* this loop iterates once per filename component in p. - * both p (and copyp if the original had a slash) should + * both p (and sanp if the original had a slash) should * always be left pointing after a slash */ if ((*p == '.') && ((*(p+1) == '/') || (*(p+1) == '\0'))) { @@ -623,18 +594,18 @@ char *sanitize_path(char *p) p += 2; if (*p == '/') p++; - if (copyp != copy) { - /* back up the copy one level */ - --copyp; /* now pointing at slash */ - while ((copyp > copy) && (*(copyp - 1) != '/')) { + if (sanp != start) { + /* back up sanp one level */ + --sanp; /* now pointing at slash */ + while ((sanp > start) && (*(sanp - 1) != '/')) { /* skip back up to slash */ - copyp--; + sanp--; } } } else { while (1) { /* copy one component through next slash */ - *copyp++ = *p++; + *sanp++ = *p++; if ((*p == '\0') || (*(p-1) == '/')) { while (*p == '/') { /* skip multiple slashes */ @@ -645,12 +616,11 @@ char *sanitize_path(char *p) } } } - if (copyp == copy) { + if (sanp == start) { /* ended up with nothing, so put in "." component */ - *copyp++ = '.'; + *sanp++ = '.'; } - *copyp = '\0'; - return(copy); + *sanp = '\0'; } @@ -708,8 +678,8 @@ int pop_dir(char *dir) to ensure that signed/unsigned usage is consistent between machines. */ int u_strcmp(const char *cs1, const char *cs2) { - const uchar *s1 = (uchar *)cs1; - const uchar *s2 = (uchar *)cs2; + const uchar *s1 = (const uchar *)cs1; + const uchar *s2 = (const uchar *)cs2; while (*s1 && *s2 && (*s1 == *s2)) { s1++; s2++;