patch to optimize away a big strdup at the end of apr_filepath_merge()

--Brian


Index: srclib/apr/file_io/unix/filepath.c =================================================================== RCS file: /home/cvspublic/apr/file_io/unix/filepath.c,v retrieving revision 1.9 diff -u -r1.9 filepath.c --- srclib/apr/file_io/unix/filepath.c 2001/09/09 16:04:59 1.9 +++ srclib/apr/file_io/unix/filepath.c 2001/11/23 04:20:09 @@ -118,8 +118,9 @@ apr_int32_t flags, apr_pool_t *p) { - char path[APR_PATH_MAX]; /* isn't null term */ + char* path; apr_size_t rootlen; /* is the length of the src rootpath */ + apr_size_t maxlen; /* maximum total path length */ apr_size_t keptlen; /* is the length of the retained rootpath */ apr_size_t pathlen; /* is the length of the result path */ apr_size_t seglen; /* is the end of the current segment */ @@ -184,6 +185,16 @@ }

rootlen = strlen(rootpath);
+ maxlen = rootlen + strlen(addpath) + 4; /* 4 for slashes at start, after
+ * root, and at end, plus trailing
+ * null*/
+ if (maxlen > APR_PATH_MAX) {
+ if (rootlen >= APR_PATH_MAX) {
+ return APR_ENAMETOOLONG;
+ }
+ maxlen = APR_PATH_MAX;
+ }
+ path = (char *)apr_palloc(p, maxlen);


if (addpath[0] == '/')
{
@@ -207,14 +218,12 @@
/* Base the result path on the rootpath
*/
keptlen = rootlen;
- if (rootlen >= sizeof(path))
- return APR_ENAMETOOLONG;
memcpy(path, rootpath, rootlen);
/* Always '/' terminate the given root path
*/
if (keptlen && path[keptlen - 1] != '/') {
- if (keptlen + 1 >= sizeof(path))
+ if (keptlen + 1 >= maxlen)
return APR_ENAMETOOLONG;
path[keptlen++] = '/';
}
@@ -262,7 +271,7 @@


/* Otherwise append another backpath.
*/
- if (pathlen + 3 >= sizeof(path))
+ if (pathlen + 3 >= maxlen)
return APR_ENAMETOOLONG;
memcpy(path + pathlen, "../", 3);
pathlen += 3;
@@ -291,7 +300,7 @@
/* An actual segment, append it to the destination path
*/
apr_size_t i = (addpath[seglen] != '\0');
- if (pathlen + seglen + i >= sizeof(path))
+ if (pathlen + seglen + i >= maxlen)
return APR_ENAMETOOLONG;
memcpy(path + pathlen, addpath, seglen + i);
pathlen += seglen + i;
@@ -320,6 +329,6 @@
return APR_EABOVEROOT;
}
- *newpath = apr_pstrdup(p, path);
+ *newpath = path;
return APR_SUCCESS;
}





Reply via email to