summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-20 08:00:21 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-20 08:00:21 +0300
commite088964229bf984dbb8767f1e0b0e81f1f4dece9 (patch)
treeb47b5d96db33c6358c330848a35ef5ec51480548
parentd83bdda139fbf732ff13b7e9de98f2b0a57ee1a0 (diff)
downloadlibyuri-e088964229bf984dbb8767f1e0b0e81f1f4dece9.tar.gz
normalize: fix double dot path normalization
-rw-r--r--normalize.c23
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;