Skip to contentSkip to conversation

Remove useless casts

PATCH Applied Laurent Vivier 7 participants 16 messages Jun 15, 2016 – Sep 14, 2016

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover Remove useless casts

This series is the result of the following coccinelle script:

    @@
    type T;
    T v;
    @@
    -       (T *)&v
    +       &v

This script removes the cast of a variable when the variable is already of the casting type.

The script is added in scripts/coccinelle by the first patch.

Laurent Vivier (7):
coccinelle: add a script to remove useless casts linux-user,s390x: remove useless cast
s390x: remove useless cast
tricore: remove useless cast
fw_cfg: remove useless casts
crypto: remove useless casts
sheepdog: remove useless casts

 block/sheepdog.c                  | 4 ++--
 crypto/cipher-builtin.c           | 4 ++--
 hw/nvram/fw_cfg.c                 | 4 ++--
 hw/s390x/sclp.c                   | 2 +-
 hw/tricore/tricore_testboard.c    | 2 +-
 linux-user/signal.c               | 2 +-
 scripts/coccinelle/typecast.cocci | 7 +++++++
 7 files changed, 16 insertions(+), 9 deletions(-)
 create mode 100644 scripts/coccinelle/typecast.cocci
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/7 coccinelle: add a script to remove useless casts

Script from LKML.

---
 scripts/coccinelle/typecast.cocci | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 scripts/coccinelle/typecast.cocci
scripts/coccinelle/typecast.cocci new file +7 -0
new file mode 100644
index 0000000..be2183e
@@ -0,0 +1,7 @@
+// Remove useless casts
+@@
+type T;
+T v;
+@@
+-	(T *)&v
++	&v
Signature · 1 line
2.5.5
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] coccinelle: add a script to remove useless casts

Quoted 1 line · Laurent Vivier

Script from LKML.

Link to a particular message?

Quoted 13 lines
---
 scripts/coccinelle/typecast.cocci | 7 +++++++
 1 file changed, 7 insertions(+)
 create mode 100644 scripts/coccinelle/typecast.cocci
scripts/coccinelle/typecast.cocci new file +0 -0
new file mode 100644
index 0000000..be2183e

scripts/coccinelle/typecast.cocci line 7

Quoted 7 lines
scripts/coccinelle/typecast.cocci new file +7 -0
@@ -0,0 +1,7 @@
+// Remove useless casts
+@@
+type T;
+T v;
+@@
+-	(T *)&v
++	&v

Since we're using C for the most part, do we also want to remove useless casts in and out of void*?

But even if we do that, it could be separate. This patch looks good on its own:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

PATCH 2/7 linux-user,s390x: remove useless cast

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

CC Riku Voipio <[email protected]>
CC Alexander Graf <[email protected]>
---
 linux-user/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/signal.c +1 -1
index 61c1145..df3d584 100644
@@ -4232,7 +4232,7 @@ static void setup_frame(int sig, struct target_sigaction *ka,
     env->regs[5] = 0; // FIXME: no clue... current->thread.prot_addr;
 
     /* Place signal number on stack to allow backtrace from handler.  */
-    __put_user(env->regs[2], (int *) &frame->signo);
+    __put_user(env->regs[2], &frame->signo);
     unlock_user_struct(frame, frame_addr, 1);
     return;
 
Signature · 1 line
2.5.5
#4 lore raw

PATCH 3/7 s390x: remove useless cast

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

---
 hw/s390x/sclp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/s390x/sclp.c +1 -1
index fca37f5..feeb0da 100644
@@ -406,7 +406,7 @@ int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
         goto out;
     }
 
-    sclp_c->execute(sclp, (SCCB *)&work_sccb, code);
+    sclp_c->execute(sclp, &work_sccb, code);
 
     cpu_physical_memory_write(sccb, &work_sccb,
                               be16_to_cpu(work_sccb.h.length));
Signature · 1 line
2.5.5
#5 lore raw

PATCH 4/7 tricore: remove useless cast

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

CC Bastian Koppelmann <[email protected]>
---
 hw/tricore/tricore_testboard.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/tricore/tricore_testboard.c +1 -1
index 8d3520f..19dd587 100644
@@ -46,7 +46,7 @@ static void tricore_load_kernel(CPUTriCoreState *env)
     long kernel_size;
 
     kernel_size = load_elf(tricoretb_binfo.kernel_filename, NULL,
-                           NULL, (uint64_t *)&entry, NULL,
+                           NULL, &entry, NULL,
                            NULL, 0,
                            EM_TRICORE, 1, 0);
     if (kernel_size <= 0) {
Signature · 1 line
2.5.5
#7 lore raw

PATCH 5/7 fw_cfg: remove useless casts

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

---
 hw/nvram/fw_cfg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/nvram/fw_cfg.c +2 -2
index 74a0079..9d84129 100644
@@ -180,7 +180,7 @@ static void fw_cfg_bootsplash(FWCfgState *s)
         temp = qemu_opt_get(opts, "splash-time");
         if (temp != NULL) {
             p = (char *)temp;
-            boot_splash_time = strtol(p, (char **)&p, 10);
+            boot_splash_time = strtol(p, &p, 10);
         }
     }
 
@@ -240,7 +240,7 @@ static void fw_cfg_reboot(FWCfgState *s)
         temp = qemu_opt_get(opts, "reboot-timeout");
         if (temp != NULL) {
             p = (char *)temp;
-            reboot_timeout = strtol(p, (char **)&p, 10);
+            reboot_timeout = strtol(p, &p, 10);
         }
     }
     /* validate the input */
Signature · 1 line
2.5.5
Reviewed-by on v1 5/7
#8 lore raw

Re: [Qemu-devel] [PATCH 5/7] fw_cfg: remove useless casts

Quoted 24 lines · Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

---
 hw/nvram/fw_cfg.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/nvram/fw_cfg.c +1 -1
index 74a0079..9d84129 100644
@@ -180,7 +180,7 @@ static void fw_cfg_bootsplash(FWCfgState *s)
         temp = qemu_opt_get(opts, "splash-time");
         if (temp != NULL) {
             p = (char *)temp;
-            boot_splash_time = strtol(p, (char **)&p, 10);
+            boot_splash_time = strtol(p, &p, 10);
         }
     }
 

hw/nvram/fw_cfg.c static void fw_cfg_reboot(FWCfgState *s) · line 246

Quoted 8 lines
hw/nvram/fw_cfg.c +1 -1
@@ -240,7 +240,7 @@ static void fw_cfg_reboot(FWCfgState *s)
         temp = qemu_opt_get(opts, "reboot-timeout");
         if (temp != NULL) {
             p = (char *)temp;
-            reboot_timeout = strtol(p, (char **)&p, 10);
+            reboot_timeout = strtol(p, &p, 10);
         }
     }
     /* validate the input */
#9 lore raw

PATCH 6/7 crypto: remove useless casts

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

CC Daniel P. Berrange <[email protected]>
---
 crypto/cipher-builtin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
crypto/cipher-builtin.c +2 -2
index 88963f6..d791c80 100644
@@ -132,7 +132,7 @@ static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_encrypt((AES_KEY *)&aesctx->enc,
+    qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc,
                                    src, dst, length);
 }
 
@@ -144,7 +144,7 @@ static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_decrypt((AES_KEY *)&aesctx->dec,
+    qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec,
                                    src, dst, length);
 }
 
Signature · 1 line
2.5.5
Reviewed-by on v1 6/7
#10 lore raw

Re: [Qemu-devel] [PATCH 6/7] crypto: remove useless casts

Quoted 23 lines · Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

CC Daniel P. Berrange <[email protected]>
---
 crypto/cipher-builtin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
crypto/cipher-builtin.c +1 -1
index 88963f6..d791c80 100644
@@ -132,7 +132,7 @@ static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_encrypt((AES_KEY *)&aesctx->enc,
+    qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc,
                                    src, dst, length);
 }
 

crypto/cipher-builtin.c static void qcrypto_cipher_aes_xts_decrypt(const void *ctx, · line 149

Quoted 7 lines
crypto/cipher-builtin.c +1 -1
@@ -144,7 +144,7 @@ static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_decrypt((AES_KEY *)&aesctx->dec,
+    qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec,
                                    src, dst, length);
 }

I'm fine with this going in via qemu-trivial.

Regards,
Daniel

Signature · 4 lines
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|
#11 lore raw

Re: [Qemu-devel] [Qemu-trivial] [PATCH 6/7] crypto: remove useless casts

In reply to Laurent Vivier

15.06.2016 19:14, Laurent Vivier пишет:

Quoted 23 lines

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

CC Daniel P. Berrange <[email protected]>
---
 crypto/cipher-builtin.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
crypto/cipher-builtin.c +1 -1
index 88963f6..d791c80 100644
@@ -132,7 +132,7 @@ static void qcrypto_cipher_aes_xts_encrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_encrypt((AES_KEY *)&aesctx->enc,
+    qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc,
                                    src, dst, length);
 }
 

crypto/cipher-builtin.c static void qcrypto_cipher_aes_xts_decrypt(const void *ctx, · line 149

Quoted 7 lines
crypto/cipher-builtin.c +1 -1
@@ -144,7 +144,7 @@ static void qcrypto_cipher_aes_xts_decrypt(const void *ctx,
 {
     const QCryptoCipherBuiltinAESContext *aesctx = ctx;
 
-    qcrypto_cipher_aes_ecb_decrypt((AES_KEY *)&aesctx->dec,
+    qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec,
                                    src, dst, length);
 }

This patch causes the following build failure:

CC crypto/block.o
In file included from /build/qemu/git/crypto/cipher.c:156:0:crypto/cipher-builtin.c: In function ‘qcrypto_cipher_aes_xts_encrypt’:
crypto/cipher-builtin.c:135:36: error: passing argument 1 of ‘qcrypto_cipher_aes_ecb_encrypt’ discards ‘const’ qualifier from pointer target type [-Werror]

     qcrypto_cipher_aes_ecb_encrypt(&aesctx->enc,
                                    ^
crypto/cipher-builtin.c:76:13: note: expected ‘struct AES_KEY *’ but argument is of type ‘const struct AES_KEY *’
 static void qcrypto_cipher_aes_ecb_encrypt(AES_KEY *key,
             ^
crypto/cipher-builtin.c: In function ‘qcrypto_cipher_aes_xts_decrypt’:
crypto/cipher-builtin.c:147:36: error: passing argument 1 of ‘qcrypto_cipher_aes_ecb_decrypt’ discards ‘const’ qualifier from pointer target type [-Werror]
     qcrypto_cipher_aes_ecb_decrypt(&aesctx->dec,
                                    ^
crypto/cipher-builtin.c:102:13: note: expected ‘struct AES_KEY *’ but argument is of type ‘const struct AES_KEY *’
 static void qcrypto_cipher_aes_ecb_decrypt(AES_KEY *key,
             ^

cc1: all warnings being treated as errors

It's interesting that the qemu functions accepts const argument, while apparently this is wrong, as qcrypto functions modifies their args. Looks like a bug either in qemu or qcrypto... :)

I'll reverted this change for now.

Thanks,

/mjt

#12 lore raw

PATCH 7/7 sheepdog: remove useless casts

In reply to Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

---
 block/sheepdog.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
block/sheepdog.c +2 -2
index 23fbace..95e6a11 100644
@@ -1049,7 +1049,7 @@ static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
     const char *host_spec, *vdi_spec;
     int nr_sep, ret;
 
-    strstart(filename, "sheepdog:", (const char **)&filename);
+    strstart(filename, "sheepdog:", &filename);
     p = q = g_strdup(filename);
 
     /* count the number of separators */
@@ -2652,7 +2652,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
     req.opcode = SD_OP_READ_VDIS;
     req.data_length = max;
 
-    ret = do_req(fd, s->aio_context, (SheepdogReq *)&req,
+    ret = do_req(fd, s->aio_context, &req,
                  vdi_inuse, &wlen, &rlen);
 
     closesocket(fd);
Signature · 1 line
2.5.5
Reviewed-by on v1 7/7
#13 lore raw

Re: [Qemu-devel] [PATCH 7/7] sheepdog: remove useless casts

At Wed, 15 Jun 2016 18:14:37 +0200,

Quoted 10 lines · Laurent Vivier

This patch is the result of coccinelle script
scripts/coccinelle/typecast.cocci

---
 block/sheepdog.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Thanks,
Hitoshi

Quoted 27 lines
block/sheepdog.c +2 -2
index 23fbace..95e6a11 100644
@@ -1049,7 +1049,7 @@ static int parse_vdiname(BDRVSheepdogState *s, const char *filename,
     const char *host_spec, *vdi_spec;
     int nr_sep, ret;
 
-    strstart(filename, "sheepdog:", (const char **)&filename);
+    strstart(filename, "sheepdog:", &filename);
     p = q = g_strdup(filename);
 
     /* count the number of separators */
@@ -2652,7 +2652,7 @@ static int sd_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
     req.opcode = SD_OP_READ_VDIS;
     req.data_length = max;
 
-    ret = do_req(fd, s->aio_context, (SheepdogReq *)&req,
+    ret = do_req(fd, s->aio_context, &req,
                  vdi_inuse, &wlen, &rlen);
 
     closesocket(fd);
Signature · 1 line
2.5.5
#14 lore raw
In reply to Laurent Vivier

Ping?

Laurent

Quoted 33 lines · Laurent Vivier

This series is the result of the following coccinelle script:

    @@
    type T;
    T v;
    @@
    -       (T *)&v
    +       &v

This script removes the cast of a variable when the variable is already of the casting type.

The script is added in scripts/coccinelle by the first patch.

Laurent Vivier (7):
coccinelle: add a script to remove useless casts linux-user,s390x: remove useless cast
s390x: remove useless cast
tricore: remove useless cast
fw_cfg: remove useless casts
crypto: remove useless casts
sheepdog: remove useless casts

 block/sheepdog.c                  | 4 ++--
 crypto/cipher-builtin.c           | 4 ++--
 hw/nvram/fw_cfg.c                 | 4 ++--
 hw/s390x/sclp.c                   | 2 +-
 hw/tricore/tricore_testboard.c    | 2 +-
 linux-user/signal.c               | 2 +-
 scripts/coccinelle/typecast.cocci | 7 +++++++
 7 files changed, 16 insertions(+), 9 deletions(-)
 create mode 100644 scripts/coccinelle/typecast.cocci
#15 lore raw
In reply to Laurent Vivier
Quoted 23 lines · Laurent Vivier

This series is the result of the following coccinelle script:

    @@
    type T;
    T v;
    @@
    -       (T *)&v
    +       &v

This script removes the cast of a variable when the variable is already of the casting type.

The script is added in scripts/coccinelle by the first patch.

Laurent Vivier (7):
coccinelle: add a script to remove useless casts linux-user,s390x: remove useless cast
s390x: remove useless cast
tricore: remove useless cast
fw_cfg: remove useless casts
crypto: remove useless casts
sheepdog: remove useless casts

Quoted 9 lines
 block/sheepdog.c                  | 4 ++--
 crypto/cipher-builtin.c           | 4 ++--
 hw/nvram/fw_cfg.c                 | 4 ++--
 hw/s390x/sclp.c                   | 2 +-
 hw/tricore/tricore_testboard.c    | 2 +-
 linux-user/signal.c               | 2 +-
 scripts/coccinelle/typecast.cocci | 7 +++++++
 7 files changed, 16 insertions(+), 9 deletions(-)
 create mode 100644 scripts/coccinelle/typecast.cocci

Applied all 7 to -trivial, thank you!

/mjt