Skip to contentSkip to conversation

util/async: avoid useless cast

PATCH In review Frediano Ziglio 2 participants 9 messages Sep 27, 2019 – Oct 21, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 util/async: avoid useless cast

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -1
index 4e4c7af51e..3cdbf44503 100644
@@ -425,7 +425,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO
Signature · 1 line
2.21.0
#1 lore raw

PATCH 2/3 event_notifier: avoid dandling file descriptor in event_notifier_cleanup

If rfd is equal to wfd the file descriptor is closed but rfd will still have the closed value.
The EventNotifier structure should not be used again after calling event_notifier_cleanup or should be initialized again but make sure to not have dandling file descriptors around.

---
 util/event_notifier-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/event_notifier-posix.c +1 -1
index 73c4046b58..00d93204f9 100644
@@ -80,8 +80,8 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
-        e->rfd = -1;
     }
+    e->rfd = -1;
     close(e->wfd);
     e->wfd = -1;
 }
Signature · 1 line
2.21.0
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] event_notifier: avoid dandling file descriptor in event_notifier_cleanup

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 17 lines

If rfd is equal to wfd the file descriptor is closed but rfd will still have the closed value.
The EventNotifier structure should not be used again after calling event_notifier_cleanup or should be initialized again but make sure to not have dandling file descriptors around.

---
 util/event_notifier-posix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
util/event_notifier-posix.c +0 -0
index 73c4046b58..00d93204f9 100644

util/event_notifier-posix.c void event_notifier_cleanup(EventNotifier *e) · line 87

Quoted 9 lines
util/event_notifier-posix.c +1 -1
@@ -80,8 +80,8 @@ void event_notifier_cleanup(EventNotifier *e)
 {
     if (e->rfd != e->wfd) {
         close(e->rfd);
-        e->rfd = -1;
     }
+    e->rfd = -1;
     close(e->wfd);
     e->wfd = -1;
 }
Reviewed-by on v1 1/3
#3 lore raw
In reply to Frediano Ziglio

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 13 lines

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -0
index 4e4c7af51e..3cdbf44503 100644

util/async.c AioContext *aio_context_new(Error **errp) · line 430

Quoted 7 lines
util/async.c +0 -1
@@ -425,7 +425,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO
#4 lore raw

PATCH 3/3 qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

In reply to Frediano Ziglio
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +1 -5
index d428fec567..9bd173ecda 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, INT32_MAX);
 }
 
 
Signature · 1 line
2.21.0
Reviewed-by on v1 3/3
#5 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 11 lines
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..9bd173ecda 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 327

Quoted 10 lines
util/qemu-timer.c +1 -5
 
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, INT32_MAX);
 }
 

Perhaps it would be cleaner to have always the same return type for MIN() with:

    MIN(ms, (int64_t) INT32_MAX)

Anyway:

Thanks,
Laurent

#6 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Quoted 28 lines

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 12 lines
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..9bd173ecda 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 328

Quoted 10 lines
util/qemu-timer.c +1 -5
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days
     */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, INT32_MAX);
 }
 

Perhaps it would be cleaner to have always the same return type for MIN() with:

Quoted 2 lines
    MIN(ms, (int64_t) INT32_MAX)

The (int64_t) cast here is useless, ms is signed, C compilers knows how to convert and compare properly.
The function returns int and it's used in some cases to fill 32bit integer, changing the return type to int64_t is not trivial.
But I suppose that here you mean instead that the 2 sides of the ternary operator used by MIN macro are int64_t and (probably) int32_t. In this case feel free to add the cast with a

   return (int) MIN(ms, (int64_t) INT32_MAX);
Quoted 7 lines

Anyway:

Thanks,
Laurent

Frediano

#7 lore raw

Re: [PATCH 3/3] qemu-timer: reuse MIN macro in qemu_timeout_ns_to_ms

Le 27/09/2019 à 13:57, Frediano Ziglio a écrit :

Quoted 28 lines
Quoted 28 lines

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 12 lines
---
 util/qemu-timer.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
util/qemu-timer.c +0 -0
index d428fec567..9bd173ecda 100644
@@ -322,11 +322,7 @@ int qemu_timeout_ns_to_ms(int64_t ns)
     ms = DIV_ROUND_UP(ns, SCALE_MS);
 

util/qemu-timer.c int qemu_timeout_ns_to_ms(int64_t ns) · line 328

Quoted 10 lines
util/qemu-timer.c +1 -5
     /* To avoid overflow problems, limit this to 2^31, i.e. approx 25 days
     */
-    if (ms > (int64_t) INT32_MAX) {
-        ms = INT32_MAX;
-    }
-
-    return (int) ms;
+    return (int) MIN(ms, INT32_MAX);
 }
 

Perhaps it would be cleaner to have always the same return type for MIN() with:

Quoted 5 lines
Quoted 2 lines
    MIN(ms, (int64_t) INT32_MAX)

The (int64_t) cast here is useless, ms is signed, C compilers knows how to convert and compare properly.

I agree.

Quoted 4 lines

The function returns int and it's used in some cases to fill 32bit integer, changing the return type to int64_t is not trivial.
But I suppose that here you mean instead that the 2 sides of the ternary operator used by MIN macro are int64_t and (probably) int32_t. In this case

Yes, it's what I mean.

Thanks,
Laurent

#8 lore raw
In reply to Frediano Ziglio

Please re-send your series to [email protected] if you want to have it merged.

Thanks,
Laurent

Le 27/09/2019 à 12:32, Frediano Ziglio a écrit :

Quoted 20 lines

event_notifier_dummy_cb is already compatible with EventNotifierHandler.

---
 util/async.c | 1 -
 1 file changed, 1 deletion(-)
util/async.c +0 -1
index 4e4c7af51e..3cdbf44503 100644
@@ -425,7 +425,6 @@ AioContext *aio_context_new(Error **errp)
 
     aio_set_event_notifier(ctx, &ctx->notifier,
                            false,
-                           (EventNotifierHandler *)
                            event_notifier_dummy_cb,
                            event_notifier_poll);
 #ifdef CONFIG_LINUX_AIO