Skip to contentSkip to conversation

util/compatfd.c: Fixed styling and replaced malloc

PATCH In review Mahmoud Mandour 4 participants 9 messages Mar 15, 2021

lore
Share & tools
lore
#0 lore raw

cover util/compatfd.c: Fixed styling and replaced malloc

In this small series, I fix two styling issues that were causing errors when the checkpatch.pl scritp was run against the file util/compatfd.c.
Also, I replace a call to malloc() and its respective call to free() with GLib's allocation and deallocation.

Used g_malloc() because the allocation is already small and its better if the system aborts on failure of such small allocations.

Mahmoud Mandour (2):
util/compatfd.c: Fixed style issues util/compatfd.c: Replaced a malloc call with g_malloc.

 util/compatfd.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/2 util/compatfd.c: Fixed style issues

Fixed two styling issues that caused checkpatch.pl errors.

---
 util/compatfd.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
util/compatfd.c +3 -3
index ee47dd8089..174f394533 100644
@@ -20,8 +20,7 @@
 #include <sys/syscall.h>
 #endif
 
-struct sigfd_compat_info
-{
+struct sigfd_compat_info {
     sigset_t mask;
     int fd;
 };
@@ -53,8 +52,9 @@ static void *sigwait_compat(void *opaque)
 
                 len = write(info->fd, (char *)&buffer + offset,
                             sizeof(buffer) - offset);
-                if (len == -1 && errno == EINTR)
+                if (len == -1 && errno == EINTR) {
                     continue;
+                }
 
                 if (len <= 0) {
                     return NULL;
Signature · 1 line
2.25.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] util/compatfd.c: Fixed style issues

Quoted 23 lines · Mahmoud Mandour

Fixed two styling issues that caused checkpatch.pl errors.

---
  util/compatfd.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
util/compatfd.c +1 -2
index ee47dd8089..174f394533 100644
@@ -20,8 +20,7 @@
  #include <sys/syscall.h>
  #endif
 
-struct sigfd_compat_info
-{
+struct sigfd_compat_info {
      sigset_t mask;
      int fd;
  };

util/compatfd.c static void *sigwait_compat(void *opaque) · line 60

Quoted 10 lines
util/compatfd.c +2 -1
@@ -53,8 +52,9 @@ static void *sigwait_compat(void *opaque)
 
                  len = write(info->fd, (char *)&buffer + offset,
                              sizeof(buffer) - offset);
-                if (len == -1 && errno == EINTR)
+                if (len == -1 && errno == EINTR) {
                      continue;
+                }
 
                  if (len <= 0) {
                      return NULL;
#3 lore raw

PATCH 2/2 util/compatfd.c: Replaced a malloc call with g_malloc.

In reply to Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
 util/compatfd.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +2 -6
index 174f394533..a8ec525c6c 100644
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t *mask)
     QemuThread thread;
     int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));
 
     if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
         return -1;
     }
 
Signature · 1 line
2.25.1
#4 lore raw

Re: [PATCH 2/2] util/compatfd.c: Replaced a malloc call with g_malloc.

Hi Mahmoud,

Quoted 21 lines · Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
 util/compatfd.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +0 -0
index 174f394533..a8ec525c6c 100644

util/compatfd.c static int qemu_signalfd_compat(const sigset_t *mask) · line 75

Quoted 9 lines
util/compatfd.c +1 -5
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t *mask)
     QemuThread thread;
     int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));

Watch out...

https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html

If any call to allocate memory using functions g_new(), g_new0(), g_renew(), g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n() fails, the application is terminated.

So with your change instead of handling ENOMEM the QEMU process is simply killed.

Don't you want to use g_try_new(struct sigfd_compat_info, 1) here instead?

Quoted 8 lines
util/compatfd.c +1 -1
     if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
         return -1;
     }
 
 
#5 lore raw

Re: [PATCH 2/2] util/compatfd.c: Replaced a malloc call with g_malloc.

Quoted 42 lines · Philippe Mathieu-Daudé

Hi Mahmoud,

Quoted 21 lines · Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
 util/compatfd.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +0 -0
index 174f394533..a8ec525c6c 100644

util/compatfd.c static int qemu_signalfd_compat(const sigset_t *mask) · line 75

Quoted 9 lines
util/compatfd.c +1 -5
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t *mask)
     QemuThread thread;
     int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));

Watch out...

https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html

If any call to allocate memory using functions g_new(), g_new0(), g_renew(), g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n() fails, the application is terminated.

Quoted 16 lines

So with your change instead of handling ENOMEM the QEMU process is simply killed.

Don't you want to use g_try_new(struct sigfd_compat_info, 1) here instead?

Quoted 8 lines
util/compatfd.c +1 -1
     if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
         return -1;
     }
 
 

Hello Mr. Philippe,

That's originally what I did and I sent a patch that uses a g_try_* variant, and was
instructed by Mr. Thomas Huth that it was better to use g_malloc instead because this is a small allocation and the process is better killed if such an allocation fails because the system is already in a very critical state if it does not handle a small allocation well.

You can find Mr. Thomas reply to my previous patch here:
Re: [PATCH 5/8] util/compatfd.c: Replaced a malloc with GLib's variant (gnu.org)
<https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg05067.html>

You can instruct me on what to do further.

Thanks
Mahmoud

#6 lore raw

Re: [PATCH 2/2] util/compatfd.c: Replaced a malloc call with g_malloc.

Mahmoud, it's generally a good idea to cc: people who commented on a previous iteration of the same patch. In this case, Thomas. I'm doing that for you now.

Quoted 61 lines · Mahmoud Mandour
Quoted 42 lines · Philippe Mathieu-Daudé

Hi Mahmoud,

Quoted 21 lines · Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
 util/compatfd.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +0 -0
index 174f394533..a8ec525c6c 100644

util/compatfd.c static int qemu_signalfd_compat(const sigset_t *mask) · line 75

Quoted 9 lines
util/compatfd.c +1 -5
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t *mask)
     QemuThread thread;
     int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));

Watch out...

https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html

If any call to allocate memory using functions g_new(), g_new0(), g_renew(), g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n() fails, the application is terminated.

Quoted 16 lines

So with your change instead of handling ENOMEM the QEMU process is simply killed.

Don't you want to use g_try_new(struct sigfd_compat_info, 1) here instead?

Quoted 8 lines
util/compatfd.c +1 -1
     if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
         return -1;
     }
 
 
Quoted 8 lines

Hello Mr. Philippe,

That's originally what I did and I sent a patch that uses a g_try_* variant, and was
instructed by Mr. Thomas Huth that it was better to use g_malloc instead because this is a small allocation and the process is better killed if such an allocation fails because the system is already in a very critical state if it does not handle a small allocation well.

You even explained this in the commit message. Appreciated.

Quoted 6 lines

You can find Mr. Thomas reply to my previous patch here:
Re: [PATCH 5/8] util/compatfd.c: Replaced a malloc with GLib's variant (gnu.org)
<https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg05067.html>

You can instruct me on what to do further.

I figure this patch is fine. Thomas?

Reviewed-by
#7 lore raw

Re: [PATCH 2/2] util/compatfd.c: Replaced a malloc call with g_malloc.

Quoted 4 lines · Markus Armbruster

Mahmoud, it's generally a good idea to cc: people who commented on a previous iteration of the same patch. In this case, Thomas. I'm doing that for you now.

Quoted 68 lines
Quoted 66 lines · Mahmoud Mandour
Quoted 42 lines · Philippe Mathieu-Daudé

Hi Mahmoud,

Quoted 21 lines · Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
  util/compatfd.c | 8 ++------
  1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +0 -0
index 174f394533..a8ec525c6c 100644

util/compatfd.c static int qemu_signalfd_compat(const sigset_t *mask) · line 75

Quoted 9 lines
util/compatfd.c +1 -5
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t *mask)
      QemuThread thread;
      int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));

Watch out...

https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html

If any call to allocate memory using functions g_new(), g_new0(), g_renew(), g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n() fails, the application is terminated.

Quoted 16 lines

So with your change instead of handling ENOMEM the QEMU process is simply killed.

Don't you want to use g_try_new(struct sigfd_compat_info, 1) here instead?

Quoted 8 lines
util/compatfd.c +1 -1
      if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
          return -1;
      }
 
 

Hello Mr. Philippe,

That's originally what I did and I sent a patch that uses a g_try_* variant, and was
instructed by Mr. Thomas Huth that it was better to use g_malloc instead

No need to say "Mr." here ... we're not that formal on this mailing list here :-)

Quoted 6 lines
Quoted 3 lines

because this is a small allocation and the process is better killed if such an allocation fails because the system is already in a very critical state if it does not handle a small allocation well.

You even explained this in the commit message. Appreciated.

Quoted 8 lines
Quoted 6 lines

You can find Mr. Thomas reply to my previous patch here:
Re: [PATCH 5/8] util/compatfd.c: Replaced a malloc with GLib's variant (gnu.org)
<https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg05067.html>

You can instruct me on what to do further.

I figure this patch is fine. Thomas?

Yes, looks good now, thanks for the update, Mahmoud!

#8 lore raw

Re: [PATCH 2/2] util/compatfd.c: Replaced a malloc call with g_malloc.

Quoted 92 lines · Thomas Huth
Quoted 4 lines · Markus Armbruster

Mahmoud, it's generally a good idea to cc: people who commented on a previous iteration of the same patch.  In this case, Thomas.  I'm doing that for you now.

Quoted 70 lines
Quoted 68 lines · Mahmoud Mandour
Quoted 43 lines · Philippe Mathieu-Daudé

Hi Mahmoud,

Quoted 21 lines · Mahmoud Mandour

Replaced a call to malloc() and its respective call to free() with g_malloc() and g_free().

g_malloc() is preferred more than g_try_* functions, which return NULL on error, when the size of the requested allocation  is small. This is because allocating few bytes should not be a problem in a healthy system. Otherwise, the system is already in a critical state.

Subsequently, removed NULL-checking after g_malloc().

---
  util/compatfd.c | 8 ++------

  1 file changed, 2 insertions(+), 6 deletions(-)
util/compatfd.c +0 -0
index 174f394533..a8ec525c6c 100644

util/compatfd.c static int qemu_signalfd_compat(const sigset_t · line 76

Quoted 10 lines
util/compatfd.c +1 -5
@@ -72,14 +72,10 @@ static int qemu_signalfd_compat(const sigset_t
 *mask)
       QemuThread thread;
       int fds[2];
 
-    info = malloc(sizeof(*info));
-    if (info == NULL) {
-        errno = ENOMEM;
-        return -1;
-    }
+    info = g_malloc(sizeof(*info));

Watch out...

https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html

   If any call to allocate memory using functions g_new(), g_new0(), g_renew(), g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n() fails, the application is terminated.

Quoted 16 lines

So with your change instead of handling ENOMEM the QEMU process is simply killed.

Don't you want to use g_try_new(struct sigfd_compat_info, 1) here instead?

Quoted 8 lines
util/compatfd.c +1 -1
       if (pipe(fds) == -1) {
-        free(info);
+        g_free(info);
           return -1;
       }
 

Hello Mr. Philippe,

That's originally what I did and I sent a patch that uses a g_try_* variant, and was
instructed by Mr. Thomas Huth that it was better to use g_malloc instead

No need to say "Mr." here ... we're not that formal on this mailing list here :-)

Quoted 13 lines
Quoted 5 lines

because this is a small allocation and the process is better killed if such
an allocation fails because the system is already in a very critical state
if it does not handle a small allocation well.

You even explained this in the commit message.  Appreciated.

Quoted 5 lines

You can find Mr. Thomas reply to my previous patch here:
Re: [PATCH 5/8] util/compatfd.c: Replaced a malloc with GLib's variant (gnu.org)
<https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg05067.html>

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

You can instruct me on what to do further.

I figure this patch is fine.  Thomas?

Yes, looks good now, thanks for the update, Mahmoud!

I guess I misunderstood the patch description when :)