Skip to contentSkip to conversation

scsi: Don't deference in_buf if NULL

PATCH In review Fam Zheng 3 participants 5 messages Jan 4, 2018 – Jan 5, 2018

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 scsi: Don't deference in_buf if NULL

scsi_disk_emulate_command passes in_buf=NULL and in_len=0 in the REQUEST_SENSE branch. Inline the fixed_in evaluation and put it after the in_len test.

---
 scsi/utils.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scsi/utils.c +1 -3
index ddae650a99..9a0a925ef9 100644
@@ -320,10 +320,8 @@ int scsi_convert_sense(uint8_t *in_buf, int in_len,
                        uint8_t *buf, int len, bool fixed)
 {
     SCSISense sense;
-    bool fixed_in;
 
-    fixed_in = (in_buf[0] & 2) == 0;
-    if (in_len && fixed == fixed_in) {
+    if (in_len && !!fixed == ((in_buf[0] & 2) == 0)) {
         memcpy(buf, in_buf, MIN(len, in_len));
         return MIN(len, in_len);
     }
Signature · 1 line
2.14.3
#1 lore raw
Quoted 14 lines · Fam Zheng

scsi_disk_emulate_command passes in_buf=NULL and in_len=0 in the REQUEST_SENSE branch. Inline the fixed_in evaluation and put it after the in_len test.

---
 scsi/utils.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
scsi/utils.c +0 -0
index ddae650a99..9a0a925ef9 100644

scsi/utils.c int scsi_convert_sense(uint8_t *in_buf, int in_len, · line 324

Quoted 8 lines
scsi/utils.c +1 -3
@@ -320,10 +320,8 @@ int scsi_convert_sense(uint8_t *in_buf, int in_len,
                        uint8_t *buf, int len, bool fixed)
 {
     SCSISense sense;
-    bool fixed_in;
 
-    fixed_in = (in_buf[0] & 2) == 0;
-    if (in_len && fixed == fixed_in) {
+    if (in_len && !!fixed == ((in_buf[0] & 2) == 0)) {

Huh? 'fixed' is already a bool, so '!!fixed' is just extra typing that makes things harder to read. Did you mean:

if (in_len && fixed == !(in_buf[0] & 2))

as something that is slightly more legible (the LHS is already bool, the RHS uses a single ! to convert a bitwise test into a bool with the correct sense)?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
Quoted 10 lines · Eric Blake
Quoted 8 lines · Fam Zheng

scsi_disk_emulate_command passes in_buf=NULL and in_len=0 in the REQUEST_SENSE branch. Inline the fixed_in evaluation and put it after the in_len test.

---
 scsi/utils.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
Quoted 8 lines

Huh? 'fixed' is already a bool, so '!!fixed' is just extra typing that makes things harder to read. Did you mean:

if (in_len && fixed == !(in_buf[0] & 2))

as something that is slightly more legible (the LHS is already bool, the RHS uses a single ! to convert a bitwise test into a bool with the correct sense)?

It seems correct and clearer.

With Eric's modification:

#3 lore raw
In reply to Fam Zheng
Quoted 8 lines · Fam Zheng

scsi_disk_emulate_command passes in_buf=NULL and in_len=0 in the REQUEST_SENSE branch. Inline the fixed_in evaluation and put it after the in_len test.

---
 scsi/utils.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Does this patch duplicate
https://lists.gnu.org/archive/html/qemu-devel/2017-12/msg04964.html ?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#4 lore raw
Quoted 5 lines · Eric Blake
Quoted 4 lines · Fam Zheng

scsi_disk_emulate_command passes in_buf=NULL and in_len=0 in the REQUEST_SENSE branch. Inline the fixed_in evaluation and put it after the in_len test.

Quoted 7 lines
Quoted 4 lines
---
 scsi/utils.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Does this patch duplicate
https://lists.gnu.org/archive/html/qemu-devel/2017-12/msg04964.html ?

Yes. Thanks for pointing out this.

Fam