summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAli Fardan <raiz@stellarbound.space>2020-11-19 16:04:59 +0300
committerAli Fardan <raiz@stellarbound.space>2020-11-19 16:04:59 +0300
commitd83bdda139fbf732ff13b7e9de98f2b0a57ee1a0 (patch)
tree7fdc16071c4ddc9b11446b73d8b67dd09630a04b
parent1ae386c27797bcfb11d40a621f502cc120c884dd (diff)
downloadlibyuri-d83bdda139fbf732ff13b7e9de98f2b0a57ee1a0.tar.gz
fix encode uninitialized strlcat() on string && introduce new uri_string_path() function
-rw-r--r--encode.c14
-rw-r--r--yuri.c23
-rw-r--r--yuri.h1
3 files changed, 34 insertions, 4 deletions
diff --git a/encode.c b/encode.c
index daaef32..ae2d05e 100644
--- a/encode.c
+++ b/encode.c
@@ -17,14 +17,20 @@ uri_encode(struct uri *u)
 	size_t len;
 
 	len = 1; /* \0 */
-	ret = NULL;
+	ret = realloc(NULL, len);
+	if (ret == NULL)
+		return NULL;
+	ret[0] = '\0';
 
 	if (u->scheme) {
 		len += strlen(u->scheme)+1; /* scheme: */
-		ret = realloc(NULL, len);
-		if (ret == NULL)
+		dup = realloc(ret, len);
+		if (ret == NULL) {
+			free(ret);
 			return NULL;
-		strlcpy(ret, u->scheme, len);
+		}
+		ret = dup;
+		strlcat(ret, u->scheme, len);
 		strlcat(ret, ":", len);
 	}
 
diff --git a/yuri.c b/yuri.c
index 3ffcc53..89c2a2f 100644
--- a/yuri.c
+++ b/yuri.c
@@ -257,6 +257,29 @@ uri_get_path(struct uri *u)
 	return u->path;
 }
 
+/*
+ * TODO: cleanup
+ */
+char *
+uri_string_path(struct uri *u)
+{
+	struct uri *temp;
+	char *ret;
+
+	temp = uri_new();
+	if (temp == NULL)
+		return NULL;
+	temp->path = u->path;
+	temp->npath = u->npath;
+
+	ret = uri_encode(temp);
+	free(temp);
+	if (ret == NULL)
+		return NULL;
+
+	return ret;
+}
+
 int
 uri_get_npath(struct uri *u)
 {
diff --git a/yuri.h b/yuri.h
index 1a3fa63..3e33b43 100644
--- a/yuri.h
+++ b/yuri.h
@@ -43,6 +43,7 @@ const char *uri_get_authority_user(struct uri *);
 const char *uri_get_authority_host(struct uri *);
 int uri_get_authority_port(struct uri *);
 const char **uri_get_path(struct uri *);
+char *uri_string_path(struct uri *);
 int uri_get_npath(struct uri *);
 const char *uri_get_query(struct uri *);
 const char *uri_get_fragment(struct uri *);