Skip to content

Commit

Permalink
lib-sieve: util: realpath: Improved conditional expressions in path_n…
Browse files Browse the repository at this point in the history
…ormalize() to match the earlier adjusted assertions.

Added some more assertions in the process to make sure subtractions are always
valid.
  • Loading branch information
stephanbosch committed Feb 5, 2018
1 parent 2b5b62d commit b15a498
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/lib-sieve/util/realpath.c
Expand Up @@ -75,23 +75,25 @@ static int path_normalize(const char *path, bool resolve_links,
/* a reference to this segment; nothing to do */
} else if (seglen == 2 && p[0] == '.' && p[1] == '.') {
/* a reference to parent segment; back up to previous slash */
if (npath_pos > npath + 1) {
i_assert(npath_pos >= npath);
if ((npath_pos - npath) > 1) {
if (*(npath_pos-1) == '/')
npath_pos--;
for (; *(npath_pos-1) != '/'; npath_pos--);
}
} else {
/* allocate space if necessary */
if ((npath_pos + seglen + 1) >= (npath + asize)) {
i_assert(npath_pos >= npath);
if ((size_t)((npath_pos - npath) + seglen + 1) >= asize) {
ptrdiff_t npath_offset = npath_pos - npath;
asize = nearest_power(npath_offset + seglen + 2);
npath = t_buffer_reget(npath, asize);
npath_pos = npath + npath_offset;
}

/* make sure npath now ends in slash */
i_assert(npath_pos > npath);
if (*(npath_pos-1) != '/') {
i_assert(npath_pos >= npath);
i_assert((size_t)((npath_pos - npath) + 1) < asize);
*(npath_pos++) = '/';
}
Expand Down Expand Up @@ -129,7 +131,8 @@ static int path_normalize(const char *path, bool resolve_links,
[npath][0][preserved tail][link buffer][room for tail][0]
*/
espace = ltlen + tlen + 2;
if ((npath_pos + espace + lsize) >= (npath + asize)) {
i_assert(npath_pos >= npath);
if ((size_t)((npath_pos - npath) + espace + lsize) >= asize) {
ptrdiff_t npath_offset = npath_pos - npath;
asize = nearest_power((npath_offset + espace + lsize) + 1);
lsize = asize - (npath_offset + espace);
Expand Down Expand Up @@ -171,8 +174,9 @@ static int path_normalize(const char *path, bool resolve_links,
we need to allocate more space as well if lsize == ret,
because the returned link may have gotten truncated */
espace = ltlen + tlen + 2;
if ((npath_pos + espace + lsize) >= (npath + asize) ||
lsize == (size_t)ret) {
i_assert(npath_pos >= npath);
if ((size_t)((npath_pos - npath) + espace + lsize) >= asize ||
lsize == (size_t)ret) {
ptrdiff_t npath_offset = npath_pos - npath;
asize = nearest_power((npath_offset + espace + lsize) + 1);
lsize = asize - (npath_offset + espace);
Expand Down Expand Up @@ -202,7 +206,8 @@ static int path_normalize(const char *path, bool resolve_links,
npath_pos = npath + 1;
} else {
/* relative symlink; back up to previous segment */
if (npath_pos > npath + 1) {
i_assert(npath_pos >= npath);
if ((npath_pos - npath) > 1) {
if (*(npath_pos-1) == '/')
npath_pos--;
for (; *(npath_pos-1) != '/'; npath_pos--);
Expand All @@ -223,7 +228,7 @@ static int path_normalize(const char *path, bool resolve_links,
i_assert((size_t)(npath_pos - npath) < asize);

/* remove any trailing slash */
if (npath_pos > npath + 1 && *(npath_pos-1) == '/')
if ((npath_pos - npath) > 1 && *(npath_pos-1) == '/')
npath_pos--;

*npath_pos = '\0';
Expand Down

0 comments on commit b15a498

Please sign in to comment.