From e088964229bf984dbb8767f1e0b0e81f1f4dece9 Mon Sep 17 00:00:00 2001 From: Ali Fardan Date: Fri, 20 Nov 2020 08:00:21 +0300 Subject: normalize: fix double dot path normalization --- normalize.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/normalize.c b/normalize.c index 835afa9..ec8fd9e 100644 --- a/normalize.c +++ b/normalize.c @@ -15,10 +15,15 @@ _eat(struct uri *u, int index) u->path[i-1] = u->path[i]; u->npath--; - path = realloc(u->path, sizeof(*u->path)*u->npath); - if (path == NULL) - return -1; - u->path = path; + if (u->npath == 0) { + free(u->path); + u->path = NULL; + } else { + path = realloc(u->path, sizeof(*u->path)*u->npath); + if (path == NULL) + return -1; + u->path = path; + } return 0; } @@ -41,10 +46,18 @@ uri_normalize(struct uri *u) } for (i = 0; i < u->npath; i++) { - if (strcmp(u->path[i], ".") == 0 || strcmp(u->path[i], "..") == 0) { + if (strcmp(u->path[i], ".") == 0) { if (_eat(u, i) == -1) return -1; } + if (strcmp(u->path[i], "..") == 0) { + if (u->npath >= 2 && i-1 >= 0) { + if (_eat(u, i-1) == -1) + return -1; + if (_eat(u, i-1) == -1) + return -1; + } + } } return 0; -- cgit 1.4.1-2-gfad0