Skip to contentSkip to conversation

Drop superfluous conditionals around g_strdup()

PATCH Applied Markus Armbruster 4 participants 8 messages Dec 4, 2014 – Dec 10, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Drop superfluous conditionals around g_strdup()

---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
backends/rng-random.c +1 -5
index 601d9dc..4f85a8e 100644
@@ -88,11 +88,7 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    if (s->filename) {
-        return g_strdup(s->filename);
-    }
-
-    return NULL;
+    return g_strdup(s->filename);
 }
 
 static void rng_random_set_filename(Object *obj, const char *filename,
hw/tpm/tpm_passthrough.c +1 -3
index 56e9e0f..2bf3c6f 100644
@@ -400,9 +400,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
     const char *value;
 
     value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tb->cancel_path = g_strdup(value);
-    }
+    tb->cancel_path = g_strdup(value);
 
     value = qemu_opt_get(opts, "path");
     if (!value) {
util/uri.c +17 -26

74 lines not on this page · raw message

Signature · 1 line
1.9.3
#1 lore raw
Quoted 124 lines · Markus Armbruster
---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
backends/rng-random.c +1 -5
index 601d9dc..4f85a8e 100644
@@ -88,11 +88,7 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    if (s->filename) {
-        return g_strdup(s->filename);
-    }
-
-    return NULL;
+    return g_strdup(s->filename);
 }
 
 static void rng_random_set_filename(Object *obj, const char *filename,
hw/tpm/tpm_passthrough.c +1 -3
index 56e9e0f..2bf3c6f 100644
@@ -400,9 +400,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
     const char *value;
 
     value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tb->cancel_path = g_strdup(value);
-    }
+    tb->cancel_path = g_strdup(value);
 
     value = qemu_opt_get(opts, "path");
     if (!value) {
util/uri.c +17 -26
index e348c17..bbf2832 100644
@@ -1736,24 +1736,21 @@ uri_resolve(const char *uri, const char *base) {
 	goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
 	((ref->authority == NULL) && (ref->server == NULL))) {
-	if (bas->scheme != NULL)
-	    res->scheme = g_strdup(bas->scheme);
+        res->scheme = g_strdup(bas->scheme);
 	if (bas->authority != NULL)
 	    res->authority = g_strdup(bas->authority);
 	else if (bas->server != NULL) {
-	    res->server = g_strdup(bas->server);
-	    if (bas->user != NULL)
-		res->user = g_strdup(bas->user);
-	    res->port = bas->port;
+            res->server = g_strdup(bas->server);
+            res->user = g_strdup(bas->user);
+            res->port = bas->port;
 	}
-	if (bas->path != NULL)
-	    res->path = g_strdup(bas->path);
-	if (ref->query != NULL)
+        res->path = g_strdup(bas->path);
+        if (ref->query != NULL) {
 	    res->query = g_strdup (ref->query);
-	else if (bas->query != NULL)
-	    res->query = g_strdup(bas->query);
-	if (ref->fragment != NULL)
-	    res->fragment = g_strdup(ref->fragment);
+        } else {
+            res->query = g_strdup(bas->query);
+        }
+        res->fragment = g_strdup(ref->fragment);
 	goto step_7;
     }
 
@@ -1767,13 +1764,10 @@ uri_resolve(const char *uri, const char *base) {
 	val = uri_to_string(ref);
 	goto done;
     }
-    if (bas->scheme != NULL)
-	res->scheme = g_strdup(bas->scheme);
+    res->scheme = g_strdup(bas->scheme);
 
-    if (ref->query != NULL)
-	res->query = g_strdup(ref->query);
-    if (ref->fragment != NULL)
-	res->fragment = g_strdup(ref->fragment);
+    res->query = g_strdup(ref->query);
+    res->fragment = g_strdup(ref->fragment);
 
     /*
      * 4) If the authority component is defined, then the reference is a
@@ -1787,20 +1781,17 @@ uri_resolve(const char *uri, const char *base) {
 	    res->authority = g_strdup(ref->authority);
 	else {
 	    res->server = g_strdup(ref->server);
-	    if (ref->user != NULL)
-		res->user = g_strdup(ref->user);
+            res->user = g_strdup(ref->user);
             res->port = ref->port;
 	}
-	if (ref->path != NULL)
-	    res->path = g_strdup(ref->path);
+        res->path = g_strdup(ref->path);
 	goto step_7;
     }
     if (bas->authority != NULL)
 	res->authority = g_strdup(bas->authority);
     else if (bas->server != NULL) {
-	res->server = g_strdup(bas->server);
-	if (bas->user != NULL)
-	    res->user = g_strdup(bas->user);
+        res->server = g_strdup(bas->server);
+        res->user = g_strdup(bas->user);
 	res->port = bas->port;
     }
 
Signature · 1 line
1.9.3

Very confusing tab/whitespace mixture. Code is better, format is worse. I'm not sure it's a win. :)

Fam

#2 lore raw
Quoted 121 lines · Fam Zheng
Quoted 120 lines · Markus Armbruster
---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
backends/rng-random.c +1 -5
index 601d9dc..4f85a8e 100644
@@ -88,11 +88,7 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    if (s->filename) {
-        return g_strdup(s->filename);
-    }
-
-    return NULL;
+    return g_strdup(s->filename);
 }
 
 static void rng_random_set_filename(Object *obj, const char *filename,
hw/tpm/tpm_passthrough.c +1 -3
index 56e9e0f..2bf3c6f 100644
@@ -400,9 +400,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
     const char *value;
 
     value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tb->cancel_path = g_strdup(value);
-    }
+    tb->cancel_path = g_strdup(value);
 
     value = qemu_opt_get(opts, "path");
     if (!value) {
util/uri.c +17 -26
index e348c17..bbf2832 100644
@@ -1736,24 +1736,21 @@ uri_resolve(const char *uri, const char *base) {
 	goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
 	((ref->authority == NULL) && (ref->server == NULL))) {
-	if (bas->scheme != NULL)
-	    res->scheme = g_strdup(bas->scheme);
+        res->scheme = g_strdup(bas->scheme);
 	if (bas->authority != NULL)
 	    res->authority = g_strdup(bas->authority);
 	else if (bas->server != NULL) {
-	    res->server = g_strdup(bas->server);
-	    if (bas->user != NULL)
-		res->user = g_strdup(bas->user);
-	    res->port = bas->port;
+            res->server = g_strdup(bas->server);
+            res->user = g_strdup(bas->user);
+            res->port = bas->port;
 	}
-	if (bas->path != NULL)
-	    res->path = g_strdup(bas->path);
-	if (ref->query != NULL)
+        res->path = g_strdup(bas->path);
+        if (ref->query != NULL) {
 	    res->query = g_strdup (ref->query);
-	else if (bas->query != NULL)
-	    res->query = g_strdup(bas->query);
-	if (ref->fragment != NULL)
-	    res->fragment = g_strdup(ref->fragment);
+        } else {
+            res->query = g_strdup(bas->query);
+        }
+        res->fragment = g_strdup(ref->fragment);
 	goto step_7;
     }
 
@@ -1767,13 +1764,10 @@ uri_resolve(const char *uri, const char *base) {
 	val = uri_to_string(ref);
 	goto done;
     }
-    if (bas->scheme != NULL)
-	res->scheme = g_strdup(bas->scheme);
+    res->scheme = g_strdup(bas->scheme);
 
-    if (ref->query != NULL)
-	res->query = g_strdup(ref->query);
-    if (ref->fragment != NULL)
-	res->fragment = g_strdup(ref->fragment);
+    res->query = g_strdup(ref->query);
+    res->fragment = g_strdup(ref->fragment);
 
     /*
      * 4) If the authority component is defined, then the reference is a
@@ -1787,20 +1781,17 @@ uri_resolve(const char *uri, const char *base) {
 	    res->authority = g_strdup(ref->authority);
 	else {
 	    res->server = g_strdup(ref->server);
-	    if (ref->user != NULL)
-		res->user = g_strdup(ref->user);
+            res->user = g_strdup(ref->user);
             res->port = ref->port;
 	}
-	if (ref->path != NULL)
-	    res->path = g_strdup(ref->path);
+        res->path = g_strdup(ref->path);
 	goto step_7;
     }
     if (bas->authority != NULL)
 	res->authority = g_strdup(bas->authority);
     else if (bas->server != NULL) {
-	res->server = g_strdup(bas->server);
-	if (bas->user != NULL)
-	    res->user = g_strdup(bas->user);
+        res->server = g_strdup(bas->server);
+        res->user = g_strdup(bas->user);
 	res->port = bas->port;
     }
 
Quoted 7 lines
Quoted 4 lines
Signature · 1 line
1.9.3

Very confusing tab/whitespace mixture. Code is better, format is worse. I'm not sure it's a win. :)

As per standard operating procedure, I expanded tabs in the lines I touched. No visual difference, except in patches.

What do you want me to do?

1. Don't expand tabs, ignore checkpatch.pl whining

2. Expand tabs in touched lines (current patch)

3. Expand all tabs in uri_resolve() (in a separate patch, of course)

4. Expand all tabs in util/uri.c (in a separate patch, of course)

Reviewed-by
#3 lore raw
Quoted 55 lines · Markus Armbruster
Quoted 53 lines · Fam Zheng
Quoted 52 lines · Markus Armbruster
---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
backends/rng-random.c +1 -5
index 601d9dc..4f85a8e 100644
@@ -88,11 +88,7 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    if (s->filename) {
-        return g_strdup(s->filename);
-    }
-
-    return NULL;
+    return g_strdup(s->filename);
 }
 
 static void rng_random_set_filename(Object *obj, const char *filename,
hw/tpm/tpm_passthrough.c +1 -3
index 56e9e0f..2bf3c6f 100644
@@ -400,9 +400,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
     const char *value;
 
     value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tb->cancel_path = g_strdup(value);
-    }
+    tb->cancel_path = g_strdup(value);
 
     value = qemu_opt_get(opts, "path");
     if (!value) {
util/uri.c +1 -2
index e348c17..bbf2832 100644
@@ -1736,24 +1736,21 @@ uri_resolve(const char *uri, const char *base) {
 	goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
 	((ref->authority == NULL) && (ref->server == NULL))) {
-	if (bas->scheme != NULL)
-	    res->scheme = g_strdup(bas->scheme);
+        res->scheme = g_strdup(bas->scheme);
 	if (bas->authority != NULL)
 	    res->authority = g_strdup(bas->authority);
Quoted 8 lines
Quoted 8 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1745

Quoted 8 lines
util/uri.c +3 -4
 	else if (bas->server != NULL) {
-	    res->server = g_strdup(bas->server);
-	    if (bas->user != NULL)
-		res->user = g_strdup(bas->user);
-	    res->port = bas->port;
+            res->server = g_strdup(bas->server);
+            res->user = g_strdup(bas->user);
+            res->port = bas->port;

You fixed indentation for res->server and res->port ...

Quoted 7 lines
Quoted 7 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1749

Quoted 7 lines
util/uri.c +2 -3
 	}
-	if (bas->path != NULL)
-	    res->path = g_strdup(bas->path);
-	if (ref->query != NULL)
+        res->path = g_strdup(bas->path);
+        if (ref->query != NULL) {
 	    res->query = g_strdup (ref->query);

... but not for res->query.

Quoted 39 lines
Quoted 39 lines
Quoted 39 lines
util/uri.c +9 -10
+        } else {
+            res->query = g_strdup(bas->query);
+        }
+        res->fragment = g_strdup(ref->fragment);
 	goto step_7;
     }
 
@@ -1767,13 +1764,10 @@ uri_resolve(const char *uri, const char *base) {
 	val = uri_to_string(ref);
 	goto done;
     }
-    if (bas->scheme != NULL)
-	res->scheme = g_strdup(bas->scheme);
+    res->scheme = g_strdup(bas->scheme);
 
-    if (ref->query != NULL)
-	res->query = g_strdup(ref->query);
-    if (ref->fragment != NULL)
-	res->fragment = g_strdup(ref->fragment);
+    res->query = g_strdup(ref->query);
+    res->fragment = g_strdup(ref->fragment);
 
     /*
      * 4) If the authority component is defined, then the reference is a
@@ -1787,20 +1781,17 @@ uri_resolve(const char *uri, const char *base) {
 	    res->authority = g_strdup(ref->authority);
 	else {
 	    res->server = g_strdup(ref->server);
-	    if (ref->user != NULL)
-		res->user = g_strdup(ref->user);
+            res->user = g_strdup(ref->user);
             res->port = ref->port;
 	}
-	if (ref->path != NULL)
-	    res->path = g_strdup(ref->path);
+        res->path = g_strdup(ref->path);
 	goto step_7;
     }
     if (bas->authority != NULL)
Quoted 8 lines
Quoted 8 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1795

Quoted 8 lines
util/uri.c +2 -3
 	res->authority = g_strdup(bas->authority);
     else if (bas->server != NULL) {
-	res->server = g_strdup(bas->server);
-	if (bas->user != NULL)
-	    res->user = g_strdup(bas->user);
+        res->server = g_strdup(bas->server);
+        res->user = g_strdup(bas->user);
 	res->port = bas->port;

and not for res->port.

Quoted 15 lines
Quoted 9 lines
Quoted 6 lines
util/uri.c +0 -0
     }
 
Signature · 1 line
1.9.3

Very confusing tab/whitespace mixture. Code is better, format is worse. I'm not sure it's a win. :)

As per standard operating procedure, I expanded tabs in the lines I touched. No visual difference, except in patches.

What do you want me to do?

Quoted 8 lines

1. Don't expand tabs, ignore checkpatch.pl whining

2. Expand tabs in touched lines (current patch)

3. Expand all tabs in uri_resolve() (in a separate patch, of course)

4. Expand all tabs in util/uri.c (in a separate patch, of course)

Well, I never know what to do with legacy tabs, perhaps it's not worth polluting git blame. The code looks good, as a reviewer I'm happy to add my

if maintainers are happy with the indentation.

Fam

#4 lore raw
Quoted 58 lines · Fam Zheng
Quoted 57 lines · Markus Armbruster
Quoted 55 lines · Fam Zheng
Quoted 54 lines · Markus Armbruster
---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
backends/rng-random.c +1 -5
index 601d9dc..4f85a8e 100644
@@ -88,11 +88,7 @@ static char *rng_random_get_filename(Object *obj, Error **errp)
 {
     RndRandom *s = RNG_RANDOM(obj);
 
-    if (s->filename) {
-        return g_strdup(s->filename);
-    }
-
-    return NULL;
+    return g_strdup(s->filename);
 }
 
 static void rng_random_set_filename(Object *obj, const char *filename,
hw/tpm/tpm_passthrough.c +1 -3
index 56e9e0f..2bf3c6f 100644
@@ -400,9 +400,7 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
     const char *value;
 
     value = qemu_opt_get(opts, "cancel-path");
-    if (value) {
-        tb->cancel_path = g_strdup(value);
-    }
+    tb->cancel_path = g_strdup(value);
 
     value = qemu_opt_get(opts, "path");
     if (!value) {
util/uri.c +1 -3
index e348c17..bbf2832 100644
@@ -1736,24 +1736,21 @@ uri_resolve(const char *uri, const char *base) {
 	goto done;
     if ((ref->scheme == NULL) && (ref->path == NULL) &&
 	((ref->authority == NULL) && (ref->server == NULL))) {
-	if (bas->scheme != NULL)
-	    res->scheme = g_strdup(bas->scheme);
+        res->scheme = g_strdup(bas->scheme);
 	if (bas->authority != NULL)
 	    res->authority = g_strdup(bas->authority);
 	else if (bas->server != NULL) {
-	    res->server = g_strdup(bas->server);
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1745

Quoted 6 lines
util/uri.c +3 -3
-	    if (bas->user != NULL)
-		res->user = g_strdup(bas->user);
-	    res->port = bas->port;
+            res->server = g_strdup(bas->server);
+            res->user = g_strdup(bas->user);
+            res->port = bas->port;

You fixed indentation for res->server and res->port ...

... because I touched these lines...

Quoted 9 lines
Quoted 7 lines
Quoted 7 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1749

Quoted 7 lines
util/uri.c +2 -3
 	}
-	if (bas->path != NULL)
-	    res->path = g_strdup(bas->path);
-	if (ref->query != NULL)
+        res->path = g_strdup(bas->path);
+        if (ref->query != NULL) {
 	    res->query = g_strdup (ref->query);

... but not for res->query.

... because I didn't touch this line.

Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
Quoted 41 lines
util/uri.c +9 -10
+        } else {
+            res->query = g_strdup(bas->query);
+        }
+        res->fragment = g_strdup(ref->fragment);
 	goto step_7;
     }
 
@@ -1767,13 +1764,10 @@ uri_resolve(const char *uri, const char *base) {
 	val = uri_to_string(ref);
 	goto done;
     }
-    if (bas->scheme != NULL)
-	res->scheme = g_strdup(bas->scheme);
+    res->scheme = g_strdup(bas->scheme);
 
-    if (ref->query != NULL)
-	res->query = g_strdup(ref->query);
-    if (ref->fragment != NULL)
-	res->fragment = g_strdup(ref->fragment);
+    res->query = g_strdup(ref->query);
+    res->fragment = g_strdup(ref->fragment);
 
     /*
      * 4) If the authority component is defined, then the reference is a
@@ -1787,20 +1781,17 @@ uri_resolve(const char *uri, const char *base) {
 	    res->authority = g_strdup(ref->authority);
 	else {
 	    res->server = g_strdup(ref->server);
-	    if (ref->user != NULL)
-		res->user = g_strdup(ref->user);
+            res->user = g_strdup(ref->user);
             res->port = ref->port;
 	}
-	if (ref->path != NULL)
-	    res->path = g_strdup(ref->path);
+        res->path = g_strdup(ref->path);
 	goto step_7;
     }
     if (bas->authority != NULL)
 	res->authority = g_strdup(bas->authority);
     else if (bas->server != NULL) {
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

util/uri.c uri_resolve(const char *uri, const char *base) { · line 1795

Quoted 6 lines
util/uri.c +2 -3
-	res->server = g_strdup(bas->server);
-	if (bas->user != NULL)
-	    res->user = g_strdup(bas->user);
+        res->server = g_strdup(bas->server);
+        res->user = g_strdup(bas->user);
 	res->port = bas->port;

and not for res->port.

Likewise.

Quoted 25 lines
Quoted 16 lines
Quoted 10 lines
Quoted 6 lines
util/uri.c +0 -0
     }
 
Signature · 1 line
1.9.3

Very confusing tab/whitespace mixture. Code is better, format is worse. I'm not
sure it's a win. :)

As per standard operating procedure, I expanded tabs in the lines I touched. No visual difference, except in patches.

What do you want me to do?

Quoted 8 lines

1. Don't expand tabs, ignore checkpatch.pl whining

2. Expand tabs in touched lines (current patch)

3. Expand all tabs in uri_resolve() (in a separate patch, of course)

4. Expand all tabs in util/uri.c (in a separate patch, of course)

Quoted 6 lines

Well, I never know what to do with legacy tabs, perhaps it's not worth polluting git blame. The code looks good, as a reviewer I'm happy to add my

if maintainers are happy with the indentation.

In my private opinion, we should either not bother expanding tabs at all, or we should've expanded them wholesale long ago. Not worth rearguing now.

#5 lore raw
In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

As per standard operating procedure, I expanded tabs in the lines I touched. No visual difference, except in patches.

What do you want me to do?

Quoted 7 lines

1. Don't expand tabs, ignore checkpatch.pl whining

2. Expand tabs in touched lines (current patch)

3. Expand all tabs in uri_resolve() (in a separate patch, of course)

4. Expand all tabs in util/uri.c (in a separate patch, of course)

My preferred choice first: 2, 4, 3, 1

That is, I'm fine with how you did it. If you are going to clean up tabs as a separate patch, I'd prefer you do it for the whole file rather than just one function. And I'd rather a tab cleanup than ignoring checkpatch.pl, but not at the expense of favoring a tab cleanup ahead of the current proposed patch.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
Reviewed-by on v1 1/1
#6 lore raw
In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster
---
 backends/rng-random.c    |  6 +-----
 hw/tpm/tpm_passthrough.c |  4 +---
 util/uri.c               | 43 +++++++++++++++++--------------------------
 3 files changed, 19 insertions(+), 34 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org