Skip to contentSkip to conversation

util/uri: Cleanups and a bug fix

PATCH Applied Markus Armbruster 3 participants 9 messages Jan 27, 2015 – Feb 7, 2015

lore
Share & tools
lore
#0 lore raw

cover util/uri: Cleanups and a bug fix

Note: checkpatch is unhappy with the first patch, because I refrained from cleaning up the ugly return(NULL). They're all over the place.

Markus Armbruster (3):
util/uri: uri_new() can't fail, drop dead error handling util/uri: realloc2n() can't fail, drop dead error handling util/uri: URI member path can be null, compare more carfully

 util/uri.c | 61 +++++++++++++------------------------------------------------
 1 file changed, 13 insertions(+), 48 deletions(-)
Signature · 1 line
1.9.3
#3 lore raw

PATCH 3/3 util/uri: URI member path can be null, compare more carfully

uri_resolve_relative() calls strcmp(bas->path, ref->path). However, either argument could be null! Evidence: the code checks for null after the comparison. Spotted by Coverity.

I suspect this was screwed up when we stole the code from libxml2. There the conditional reads

    xmlStrEqual((xmlChar *)bas->path, (xmlChar *)ref->path)

with

    int
    xmlStrEqual(const xmlChar *str1, const xmlChar *str2) {
	if (str1 == str2) return(1);
	if (str1 == NULL) return(0);
	if (str2 == NULL) return(0);
	do {
	    if (*str1++ != *str2) return(0);
	} while (*str2++);
	return(1);
    }

Fix by replicating libxml2's logic faithfully.

---
 util/uri.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
util/uri.c +2 -1
index b9a7b54..1cfd78b 100644
@@ -1935,7 +1935,8 @@ uri_resolve_relative (const char *uri, const char * base)
 	val = g_strdup (uri);
 	goto done;
     }
-    if (!strcmp(bas->path, ref->path)) {
+    if (bas->path == ref->path ||
+        (bas->path && ref->path && !strcmp(bas->path, ref->path))) {
 	val = g_strdup("");
 	goto done;
     }
Signature · 1 line
1.9.3
#1 lore raw

PATCH 1/3 util/uri: uri_new() can't fail, drop dead error handling

In reply to Markus Armbruster
---
 util/uri.c | 36 +++++++++++-------------------------
 1 file changed, 11 insertions(+), 25 deletions(-)
util/uri.c +11 -25
index 918d235..aa39694 100644
@@ -928,12 +928,10 @@ uri_parse(const char *str) {
     if (str == NULL)
 	return(NULL);
     uri = uri_new();
-    if (uri != NULL) {
-	ret = rfc3986_parse_uri_reference(uri, str);
-        if (ret) {
-	    uri_free(uri);
-	    return(NULL);
-	}
+    ret = rfc3986_parse_uri_reference(uri, str);
+    if (ret) {
+        uri_free(uri);
+        return(NULL);
     }
     return(uri);
 }
@@ -974,15 +972,13 @@ uri_parse_raw(const char *str, int raw) {
     if (str == NULL)
 	return(NULL);
     uri = uri_new();
-    if (uri != NULL) {
-        if (raw) {
-	    uri->cleanup |= 2;
-	}
-	ret = uri_parse_into(uri, str);
-        if (ret) {
-	    uri_free(uri);
-	    return(NULL);
-	}
+    if (raw) {
+        uri->cleanup |= 2;
+    }
+    ret = uri_parse_into(uri, str);
+    if (ret) {
+        uri_free(uri);
+        return(NULL);
     }
     return(uri);
 }
@@ -1675,8 +1671,6 @@ uri_resolve(const char *uri, const char *base) {
     else {
 	if (*uri) {
 	    ref = uri_new();
-	    if (ref == NULL)
-		goto done;
 	    ret = uri_parse_into(ref, uri);
 	}
 	else
@@ -1695,8 +1689,6 @@ uri_resolve(const char *uri, const char *base) {
 	ret = -1;
     else {
 	bas = uri_new();
-	if (bas == NULL)
-	    goto done;
 	ret = uri_parse_into(bas, base);
     }
     if (ret != 0) {
@@ -1727,8 +1719,6 @@ uri_resolve(const char *uri, const char *base) {
      *    document.
      */
     res = uri_new();
-    if (res == NULL)
-	goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
 	((ref->authority == NULL) && (ref->server == NULL))) {
         res->scheme = g_strdup(bas->scheme);
@@ -1933,8 +1923,6 @@ uri_resolve_relative (const char *uri, const char * base)
      * First parse URI into a standard form
      */
     ref = uri_new ();
-    if (ref == NULL)
-	return NULL;
     /* If URI not already in "relative" form */
     if (uri[0] != '.') {
 	ret = uri_parse_into (ref, uri);
@@ -1951,8 +1939,6 @@ uri_resolve_relative (const char *uri, const char * base)
 	goto done;
     }
     bas = uri_new ();
-    if (bas == NULL)
-	goto done;
     if (base[0] != '.') {
 	ret = uri_parse_into (bas, base);
 	if (ret != 0)
Signature · 1 line
1.9.3
#2 lore raw

PATCH 2/3 util/uri: realloc2n() can't fail, drop dead error handling

In reply to Markus Armbruster
---
 util/uri.c | 22 ----------------------
 1 file changed, 22 deletions(-)
util/uri.c +0 -22
index aa39694..b9a7b54 100644
@@ -1049,14 +1049,12 @@ uri_to_string(URI *uri) {
 	while (*p != 0) {
 	    if (len >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
 		ret = temp;
 	    }
 	    ret[len++] = *p++;
 	}
 	if (len >= max) {
             temp = realloc2n(ret, &max);
-            if (temp == NULL) goto mem_error;
             ret = temp;
 	}
 	ret[len++] = ':';
@@ -1066,7 +1064,6 @@ uri_to_string(URI *uri) {
 	while (*p != 0) {
 	    if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    if (IS_RESERVED(*(p)) || IS_UNRESERVED(*(p)))
@@ -1083,7 +1080,6 @@ uri_to_string(URI *uri) {
 	if (uri->server != NULL) {
 	    if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    ret[len++] = '/';
@@ -1093,7 +1089,6 @@ uri_to_string(URI *uri) {
 		while (*p != 0) {
 		    if (len + 3 >= max) {
                         temp = realloc2n(ret, &max);
-                        if (temp == NULL) goto mem_error;
                         ret = temp;
 		    }
 		    if ((IS_UNRESERVED(*(p))) ||
@@ -1112,7 +1107,6 @@ uri_to_string(URI *uri) {
 		}
 		if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		ret[len++] = '@';
@@ -1121,7 +1115,6 @@ uri_to_string(URI *uri) {
 	    while (*p != 0) {
 		if (len >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		ret[len++] = *p++;
@@ -1129,7 +1122,6 @@ uri_to_string(URI *uri) {
 	    if (uri->port > 0) {
 		if (len + 10 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		len += snprintf(&ret[len], max - len, ":%d", uri->port);
@@ -1137,7 +1129,6 @@ uri_to_string(URI *uri) {
 	} else if (uri->authority != NULL) {
 	    if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    ret[len++] = '/';
@@ -1146,7 +1137,6 @@ uri_to_string(URI *uri) {
 	    while (*p != 0) {
 		if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		if ((IS_UNRESERVED(*(p))) ||
@@ -1165,7 +1155,6 @@ uri_to_string(URI *uri) {
 	} else if (uri->scheme != NULL) {
 	    if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    ret[len++] = '/';
@@ -1185,7 +1174,6 @@ uri_to_string(URI *uri) {
 	        (!strcmp(uri->scheme, "file"))) {
 		if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		ret[len++] = *p++;
@@ -1195,7 +1183,6 @@ uri_to_string(URI *uri) {
 	    while (*p != 0) {
 		if (len + 3 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		if ((IS_UNRESERVED(*(p))) || ((*(p) == '/')) ||
@@ -1215,7 +1202,6 @@ uri_to_string(URI *uri) {
 	if (uri->query != NULL) {
 	    if (len + 1 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    ret[len++] = '?';
@@ -1223,7 +1209,6 @@ uri_to_string(URI *uri) {
 	    while (*p != 0) {
 		if (len + 1 >= max) {
                     temp = realloc2n(ret, &max);
-                    if (temp == NULL) goto mem_error;
                     ret = temp;
 		}
 		ret[len++] = *p++;
@@ -1233,7 +1218,6 @@ uri_to_string(URI *uri) {
     if (uri->fragment != NULL) {
 	if (len + 3 >= max) {
             temp = realloc2n(ret, &max);
-            if (temp == NULL) goto mem_error;
             ret = temp;
 	}
 	ret[len++] = '#';
@@ -1241,7 +1225,6 @@ uri_to_string(URI *uri) {
 	while (*p != 0) {
 	    if (len + 3 >= max) {
                 temp = realloc2n(ret, &max);
-                if (temp == NULL) goto mem_error;
                 ret = temp;
 	    }
 	    if ((IS_UNRESERVED(*(p))) || (IS_RESERVED(*(p))))
@@ -1257,15 +1240,10 @@ uri_to_string(URI *uri) {
     }
     if (len >= max) {
         temp = realloc2n(ret, &max);
-        if (temp == NULL) goto mem_error;
         ret = temp;
     }
     ret[len] = 0;
     return(ret);
-
-mem_error:
-    g_free(ret);
-    return(NULL);
 }
 
 /**
Signature · 1 line
1.9.3
#4 lore raw
In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

Note: checkpatch is unhappy with the first patch, because I refrained from cleaning up the ugly return(NULL). They're all over the place.

Markus Armbruster (3):
util/uri: uri_new() can't fail, drop dead error handling util/uri: realloc2n() can't fail, drop dead error handling util/uri: URI member path can be null, compare more carfully

 util/uri.c | 61 +++++++++++++------------------------------------------------
 1 file changed, 13 insertions(+), 48 deletions(-)

Patches 1-2 okay. For patch 3 a very similar patch was posted yesterday.

Paolo

#5 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Markus Armbruster

Note: checkpatch is unhappy with the first patch, because I refrained from cleaning up the ugly return(NULL). They're all over the place.

Markus Armbruster (3):
util/uri: uri_new() can't fail, drop dead error handling util/uri: realloc2n() can't fail, drop dead error handling util/uri: URI member path can be null, compare more carfully

Quoted 5 lines
Quoted 3 lines
 util/uri.c | 61 +++++++++++++------------------------------------------------
 1 file changed, 13 insertions(+), 48 deletions(-)

Patches 1-2 okay. For patch 3 a very similar patch was posted yesterday.

Missed it until now. Yes, it's functionally identical.

Dear -trivial maintainer, pick whichever you like better :)

#6 lore raw
Quoted 4 lines · Markus Armbruster
Quoted 1 line
Quoted 1 line

Patches 1-2 okay. For patch 3 a very similar patch was posted yesterday.

Missed it until now. Yes, it's functionally identical.

Dear -trivial maintainer, pick whichever you like better :)

Sure -- Markus's is nicer indeed.

Paolo

#7 lore raw
In reply to Markus Armbruster

Ping?

Quoted 10 lines · Markus Armbruster

Note: checkpatch is unhappy with the first patch, because I refrained from cleaning up the ugly return(NULL). They're all over the place.

Markus Armbruster (3):
util/uri: uri_new() can't fail, drop dead error handling util/uri: realloc2n() can't fail, drop dead error handling util/uri: URI member path can be null, compare more carfully

 util/uri.c | 61 +++++++++++++------------------------------------------------
 1 file changed, 13 insertions(+), 48 deletions(-)
#8 lore raw
In reply to Markus Armbruster
Quoted 7 lines · Markus Armbruster

Note: checkpatch is unhappy with the first patch, because I refrained from cleaning up the ugly return(NULL). They're all over the place.

Markus Armbruster (3):
util/uri: uri_new() can't fail, drop dead error handling util/uri: realloc2n() can't fail, drop dead error handling util/uri: URI member path can be null, compare more carfully

Applied all to -trivial, with patch 3 replacing similar patch by Paolo.

Thanks,

/mjt