summary refs log tree commit diff stats
diff options
context:
space:
mode:
-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;