diff options
author | Noriko Hosoi <nhosoi@redhat.com> | 2007-10-03 00:55:35 +0000 |
---|---|---|
committer | Noriko Hosoi <nhosoi@redhat.com> | 2007-10-03 00:55:35 +0000 |
commit | 464b3225941e849b4283134278c76c537adc62ca (patch) | |
tree | 413983b90312ae51251102c3093dd5196cdfed20 /ldap/servers/slapd/util.c | |
parent | d3f1d6f85a0f1cc4430270cfb5c967e1bd54cb2d (diff) | |
download | ds-464b3225941e849b4283134278c76c537adc62ca.tar.gz ds-464b3225941e849b4283134278c76c537adc62ca.tar.xz ds-464b3225941e849b4283134278c76c537adc62ca.zip |
Resolves: #196523
Summary: miscellaneous memory leaks
Description: 1) fixed memory leaks
2) cleaned up normalize_path code with fixing memory leaks
Diffstat (limited to 'ldap/servers/slapd/util.c')
-rw-r--r-- | ldap/servers/slapd/util.c | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/ldap/servers/slapd/util.c b/ldap/servers/slapd/util.c index 3145e736..aaf5e061 100644 --- a/ldap/servers/slapd/util.c +++ b/ldap/servers/slapd/util.c @@ -402,61 +402,63 @@ is_abspath(const char *path) static void clean_path(char **norm_path) { - char **np; + char **np; - for (np = norm_path; np && *np; np++) - slapi_ch_free((void **)np); - slapi_ch_free((void **)&norm_path); + for (np = norm_path; np && *np; np++) + slapi_ch_free_string(np); + slapi_ch_free((void **)&norm_path); } static char ** normalize_path(char *path) { - char *dname = slapi_ch_strdup(path); - char *dnamep = dname; - char *bnamep = NULL; + char *dname = NULL; + char *dnamep = NULL; char **dirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *)); char **rdirs = (char **)slapi_ch_calloc(strlen(path), sizeof(char *)); char **dp = dirs; char **rdp; + int elimdots = 0; + + if (NULL == path || '\0' == *path) { + return NULL; + } + + dname = slapi_ch_strdup(path); do { - bnamep = strrchr(dnamep, _CSEP); - if (NULL == bnamep) { - bnamep = dnamep; + dnamep = strrchr(dname, _CSEP); + if (NULL == dnamep) { + dnamep = dname; } else { - *bnamep = '\0'; - bnamep++; + *dnamep = '\0'; + dnamep++; } - if (0 != strcmp(bnamep, ".")) { - *dp++ = slapi_ch_strdup(bnamep); /* remove "/./" in the path */ + if (0 != strcmp(dnamep, ".") && strlen(dnamep) > 0) { + *dp++ = slapi_ch_strdup(dnamep); /* rm "/./" and "//" in the path */ } - } while (NULL != dnamep && '\0' != *dnamep && /* done or relative path */ - !(0 == strcmp(dnamep, ".") && 0 == strcmp(bnamep, "."))); + } while ( dnamep > dname /* == -> no more _CSEP */ ); + slapi_ch_free_string(&dname); /* remove "xxx/.." in the path */ for (dp = dirs, rdp = rdirs; dp && *dp; dp++) { while (*dp && 0 == strcmp(*dp, "..")) { dp++; - if (rdp > rdirs) - rdp--; + elimdots++; } - if (*dp) + if (elimdots > 0) { + elimdots--; + } else if (*dp) { *rdp++ = slapi_ch_strdup(*dp); - } - for (--dp, rdp = rdirs; dp >= dirs; dp--) { - while (*dp && 0 == strcmp(*dp, "..")) { - dp--; - if (rdp > rdirs) - rdp--; } - if (*dp && strlen(*dp) > 0) - *rdp++ = slapi_ch_strdup(*dp); } - *rdp = NULL; + /* reverse */ + for (--rdp, dp = rdirs; rdp >= dp && rdp >= rdirs; --rdp, dp++) { + char *tmpp = *dp; + *dp = *rdp; + *rdp = tmpp; + } clean_path(dirs); - slapi_ch_free_string(&dname); - return rdirs; } |