Skip to contentSkip to conversation

9p: Convert use of atoi to qemu_strtoi to allow error checking

PATCH In review Nia Alarie 3 participants 5 messages Mar 12, 2018

lore
Share & tools
lore

Nia Alarie

author
#0 lore raw

PATCH v2 1/1 9p: Convert use of atoi to qemu_strtoi to allow error checking

---
 hw/9pfs/9p.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
hw/9pfs/9p.c +9 -2
index 48fa48e720..254385dfa4 100644
@@ -15,6 +15,7 @@
 #include <glib/gprintf.h>
 #include "hw/virtio/virtio.h"
 #include "qapi/error.h"
+#include "qemu/cutils.h"
 #include "qemu/error-report.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
@@ -2213,8 +2214,14 @@ static void coroutine_fn v9fs_create(void *opaque)
         }
         v9fs_path_copy(&fidp->path, &path);
     } else if (perm & P9_STAT_MODE_LINK) {
-        int32_t ofid = atoi(extension.data);
-        V9fsFidState *ofidp = get_fid(pdu, ofid);
+        int ofid;
+        V9fsFidState *ofidp;
+
+        if (qemu_strtoi(extension.data, NULL, 10, &ofid)) {
+            err = -EINVAL;
+            goto out;
+        }
+        ofidp = get_fid(pdu, (int32_t)ofid);
         if (ofidp == NULL) {
             err = -EINVAL;
             goto out;
Signature · 1 line
2.16.2
#1 lore raw
Quoted 4 lines · Nia Alarie
---
  hw/9pfs/9p.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Helping out our CI tools:
Based-on: <[email protected]>

Quoted 14 lines
hw/9pfs/9p.c +1 -0
index 48fa48e720..254385dfa4 100644
@@ -15,6 +15,7 @@
  #include <glib/gprintf.h>
  #include "hw/virtio/virtio.h"
  #include "qapi/error.h"
+#include "qemu/cutils.h"
  #include "qemu/error-report.h"
  #include "qemu/iov.h"
  #include "qemu/sockets.h"

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2217

Quoted 6 lines
hw/9pfs/9p.c +1 -2
@@ -2213,8 +2214,14 @@ static void coroutine_fn v9fs_create(void *opaque)
          }
          v9fs_path_copy(&fidp->path, &path);
      } else if (perm & P9_STAT_MODE_LINK) {
-        int32_t ofid = atoi(extension.data);
-        V9fsFidState *ofidp = get_fid(pdu, ofid);
+        int ofid;

'unsigned int' and...

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2220

Quoted 3 lines
hw/9pfs/9p.c +3 -0
+        V9fsFidState *ofidp;
+
+        if (qemu_strtoi(extension.data, NULL, 10, &ofid)) {

qemu_strtoui() might be smarter, per Greg's comments on v1.

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2224

Quoted 4 lines
hw/9pfs/9p.c +4 -0
+            err = -EINVAL;
+            goto out;
+        }
+        ofidp = get_fid(pdu, (int32_t)ofid);

This cast is spurious.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#2 lore raw
Quoted 48 lines · Eric Blake
Quoted 5 lines · Nia Alarie
---
  hw/9pfs/9p.c | 11 +++++++++--
  1 file changed, 9 insertions(+), 2 deletions(-)

Helping out our CI tools:
Based-on: <[email protected]>

Quoted 14 lines
hw/9pfs/9p.c +1 -0
index 48fa48e720..254385dfa4 100644
@@ -15,6 +15,7 @@
  #include <glib/gprintf.h>
  #include "hw/virtio/virtio.h"
  #include "qapi/error.h"
+#include "qemu/cutils.h"
  #include "qemu/error-report.h"
  #include "qemu/iov.h"
  #include "qemu/sockets.h"

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2217

Quoted 6 lines
hw/9pfs/9p.c +1 -2
@@ -2213,8 +2214,14 @@ static void coroutine_fn v9fs_create(void *opaque)
          }
          v9fs_path_copy(&fidp->path, &path);
      } else if (perm & P9_STAT_MODE_LINK) {
-        int32_t ofid = atoi(extension.data);
-        V9fsFidState *ofidp = get_fid(pdu, ofid);
+        int ofid;

'unsigned int' and...

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2220

Quoted 3 lines
hw/9pfs/9p.c +3 -0
+        V9fsFidState *ofidp;
+
+        if (qemu_strtoi(extension.data, NULL, 10, &ofid)) {

qemu_strtoui() might be smarter, per Greg's comments on v1.

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2224

Quoted 4 lines
hw/9pfs/9p.c +4 -0
+            err = -EINVAL;
+            goto out;
+        }
+        ofidp = get_fid(pdu, (int32_t)ofid);
Quoted 6 lines

This cast is spurious.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

I did this because get_fid() takes an int32_t, not an unsigned int. The struct V9fsFidState also uses an int32_t for its fid member. Do you want me to change all these types, or just the function being used here?

#3 lore raw
Quoted 28 lines · nee
Quoted 21 lines

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2217

Quoted 4 lines
       } else if (perm & P9_STAT_MODE_LINK) {
hw/9pfs/9p.c +1 -2
-        int32_t ofid = atoi(extension.data);
-        V9fsFidState *ofidp = get_fid(pdu, ofid);
+        int ofid;

'unsigned int' and...

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2220

Quoted 3 lines
hw/9pfs/9p.c +3 -0
+        V9fsFidState *ofidp;
+
+        if (qemu_strtoi(extension.data, NULL, 10, &ofid)) {

qemu_strtoui() might be smarter, per Greg's comments on v1.

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2224

Quoted 4 lines
hw/9pfs/9p.c +4 -0
+            err = -EINVAL;
+            goto out;
+        }
+        ofidp = get_fid(pdu, (int32_t)ofid);
Quoted 6 lines

This cast is spurious.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Quoted 4 lines

I did this because get_fid() takes an int32_t, not an unsigned int. The struct V9fsFidState also uses an int32_t for its fid member. Do you want me to change all these types, or just the function being used here?

I'll let Greg answer; he's more familiar with the 9p code (I was just commenting based on his initial answer to v1).

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw
Quoted 30 lines · On Mon, 12 Mar 2018 11:00:39 -0500 Eric Blake <[email protected]> wrote:
Quoted 28 lines · nee
Quoted 27 lines

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2217

Quoted 4 lines
       } else if (perm & P9_STAT_MODE_LINK) {
hw/9pfs/9p.c +1 -2
-        int32_t ofid = atoi(extension.data);
-        V9fsFidState *ofidp = get_fid(pdu, ofid);
+        int ofid;  

'unsigned int' and...

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2220

Quoted 3 lines
hw/9pfs/9p.c +3 -0
+        V9fsFidState *ofidp;
+
+        if (qemu_strtoi(extension.data, NULL, 10, &ofid)) {  

qemu_strtoui() might be smarter, per Greg's comments on v1.

hw/9pfs/9p.c static void coroutine_fn v9fs_create(void *opaque) · line 2224

Quoted 4 lines
hw/9pfs/9p.c +4 -0
+            err = -EINVAL;
+            goto out;
+        }
+        ofidp = get_fid(pdu, (int32_t)ofid);  

This cast is spurious.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

Quoted 8 lines
Quoted 4 lines

I did this because get_fid() takes an int32_t, not an unsigned int. The struct V9fsFidState also uses an int32_t for its fid member. Do you want me to change all these types, or just the function being used here?

I'll let Greg answer; he's more familiar with the 9p code (I was just commenting based on his initial answer to v1).

Yeah... as I was saying, fids are 32-bit unsigned per spec but the code is using int32_t indeed. This is incorrect and it should be fixed.