Skip to contentSkip to conversation

nbd/trivial: fix type cast for ioctl

PATCH Superseded Bogdan Purcareata 4 participants 4 messages Apr 3, 2015 – Apr 6, 2015

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nbd/trivial: fix type cast for ioctl

This fixes ioctl behavior on powerpc e6500 platforms with 64bit kernel and 32bit userspace. The current type cast has no effect there and the value passed to the kernel is still 0. Probably an issue related to the compiler, since I'm assuming the same configuration works on a similar setup on x86.

Also ensure consistency with previous type cast in TRACE message.

---
 nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
nbd.c +1 -1
index 91b7d56..da81a5b 100644
@@ -681,7 +681,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size)
 
     TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
 
-    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / (size_t)BDRV_SECTOR_SIZE) < 0) {
+    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0)) {
         int serrno = errno;
         LOG("Failed setting size (in blocks)");
         return -serrno;
Signature · 1 line
2.1.4
#1 lore raw
Quoted 18 lines · Bogdan Purcareata

This fixes ioctl behavior on powerpc e6500 platforms with 64bit kernel and 32bit userspace. The current type cast has no effect there and the value passed to the kernel is still 0. Probably an issue related to the compiler, since I'm assuming the same configuration works on a similar setup on x86.

Also ensure consistency with previous type cast in TRACE message.

---
 nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
nbd.c +0 -0
index 91b7d56..da81a5b 100644

nbd.c int nbd_init(int fd, int csock, uint32_t flags, off_t size) · line 687

Quoted 8 lines
nbd.c +1 -1
@@ -681,7 +681,7 @@ int nbd_init(int fd, int csock, uint32_t flags, off_t size)
 
     TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
 
-    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / (size_t)BDRV_SECTOR_SIZE) < 0) {
+    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0)) {
         int serrno = errno;
         LOG("Failed setting size (in blocks)");
         return -serrno;

Thanks, queued for 2.4.

#2 lore raw
Quoted 1 line · Paolo Bonzini

On 03/04/2015 13:01, Bogdan Purcareata wrote:

...

Quoted 3 lines

nbd.c int nbd_init(int fd, int csock, uint32_t flags, off_t size) · line 685

Quoted 3 lines
nbd.c +1 -1
-    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / (size_t)BDRV_SECTOR_SIZE) < 0) {
+    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0)) {
         int serrno = errno;

Hmm.. I don't think this is right at all. Now we compare size_t with zero, the result is always false, and set error if ioctl return anything other than 0, including any positive value.

Compare:

if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0)) if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0)

I think the latter is the right version.

So much for trivial... ;)

Thanks,

/mjt

#3 lore raw
Quoted 11 lines · Michael Tokarev
Quoted 1 line · Paolo Bonzini

On 03/04/2015 13:01, Bogdan Purcareata wrote:

...

Quoted 3 lines

nbd.c int nbd_init(int fd, int csock, uint32_t flags, off_t size) · line 685

Quoted 3 lines
nbd.c +1 -1
-    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / (size_t)BDRV_SECTOR_SIZE) < 0) {
+    if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0)) {
          int serrno = errno;

Hmm.. I don't think this is right at all. Now we compare size_t with zero, the result is always false, and set error if ioctl return anything other than 0, including any positive value.

Quoted 6 lines

Compare:

   if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE) < 0))
   if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0)

I think the latter is the right version.

Yes, the latter is indeed the right version. Great catch!

Quoted 1 line

So much for trivial... ;)

Me and my slippery fingers. Sorry for the mishap.

Bogdan P.

Quoted 4 lines

Thanks,

/mjt