diff options
author | Ali Fardan <raiz@stellarbound.space> | 2020-11-20 08:00:21 +0300 |
---|---|---|
committer | Ali Fardan <raiz@stellarbound.space> | 2020-11-20 08:00:21 +0300 |
commit | e088964229bf984dbb8767f1e0b0e81f1f4dece9 (patch) | |
tree | b47b5d96db33c6358c330848a35ef5ec51480548 | |
parent | d83bdda139fbf732ff13b7e9de98f2b0a57ee1a0 (diff) | |
download | libyuri-e088964229bf984dbb8767f1e0b0e81f1f4dece9.tar.gz |
normalize: fix double dot path normalization
-rw-r--r-- | normalize.c | 23 |
1 files 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; |