Skip to contentSkip to conversation

vl.c: move "if (fd < 0)" into "if (fd <= STDERR_FILENO)"

PATCH In review Chen Gang 2 participants 3 messages Dec 28, 2013 – Dec 29, 2013

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

PATCH 1/1 vl.c: move "if (fd < 0)" into "if (fd <= STDERR_FILENO)"

For valid 'fd' (in most cases), it is enough to only check whether it is larger than STDERR_FILENO, so recommend to move "if (fd < 0)" into failure processing block.

---
 vl.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
vl.c +2 -7
index 2160933..fbea535 100644
@@ -1064,15 +1064,10 @@ static int parse_add_fd(QemuOpts *opts, void *opaque)
     fdset_id = qemu_opt_get_number(opts, "set", -1);
     fd_opaque = qemu_opt_get(opts, "opaque");
 
-    if (fd < 0) {
-        qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd option is required and must be non-negative");
-        return -1;
-    }
-
     if (fd <= STDERR_FILENO) {
         qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd cannot be a standard I/O stream");
+                      fd < 0 ? "fd option is required and must be non-negative"
+                             : "fd cannot be a standard I/O stream");
         return -1;
     }
 
Signature · 1 line
1.7.11.7
#1 lore raw
Quoted 11 lines · Chen Gang

For valid 'fd' (in most cases), it is enough to only check whether it is larger than STDERR_FILENO, so recommend to move "if (fd < 0)" into failure processing block.

vl.c +0 -3
@@ -1064,15 +1064,10 @@ static int parse_add_fd(QemuOpts *opts, void *opaque)
     fdset_id = qemu_opt_get_number(opts, "set", -1);
     fd_opaque = qemu_opt_get(opts, "opaque");
 
-    if (fd < 0) {
-        qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd option is required and must be non-negative");

vl.c static int parse_add_fd(QemuOpts *opts, void *opaque) · line 1072

Quoted 10 lines
vl.c +2 -4
-        return -1;
-    }
-
     if (fd <= STDERR_FILENO) {
         qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd cannot be a standard I/O stream");
+                      fd < 0 ? "fd option is required and must be non-negative"
+                             : "fd cannot be a standard I/O stream");
         return -1;
     }

This patch doesn't change the behaviour, but I think it makes the code less clear to read (because we've folded two different error cases into one and then split them out again with a ternary operator on the string). This isn't performance critical code (it's run only a few times and only at startup), so I think we should favour clarity and ease-of-reading, and I think the existing code is better here.

thanks
-- PMM

author
#2 lore raw

Firstly, thank you very much for your reply, this is my first patch for qemu. Next year (2014), as a volunteer, I will try to make a patch for qemu in each month. :-)

Quoted 19 lines · Peter Maydell
Quoted 18 lines · Chen Gang

For valid 'fd' (in most cases), it is enough to only check whether it is larger than STDERR_FILENO, so recommend to move "if (fd < 0)" into failure processing block.

vl.c +1 -7
@@ -1064,15 +1064,10 @@ static int parse_add_fd(QemuOpts *opts, void *opaque)
     fdset_id = qemu_opt_get_number(opts, "set", -1);
     fd_opaque = qemu_opt_get(opts, "opaque");
 
-    if (fd < 0) {
-        qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd option is required and must be non-negative");
-        return -1;
-    }
-
     if (fd <= STDERR_FILENO) {
         qerror_report(ERROR_CLASS_GENERIC_ERROR,
-                      "fd cannot be a standard I/O stream");
+                      fd < 0 ? "fd option is required and must be non-negative"
Quoted 8 lines

vl.c static int parse_add_fd(QemuOpts *opts, void *opaque) · line 1072

Quoted 3 lines
vl.c +1 -0
+                             : "fd cannot be a standard I/O stream");
         return -1;
     }

This patch doesn't change the behaviour, but I think it makes the code less clear to read (because we've folded two different error cases into one and then split them out again with a ternary operator on the string).

When we check the variable (e.g. 'fd') whether valid or not, we often try to use one code block ("if () {...}") or a function (when it is complex) to perform it.

For readers, firstly, they mainly focus on the variable (e.g. 'fd') whether valid or not, they don't mainly focus on the variable failure details.

Quoted 5 lines

This isn't performance critical code (it's run only a few times and only at startup), so I think we should favour clarity and ease-of-reading, and I think the existing code is better here.

Yeah.

Thanks.

Signature · 3 lines
Chen Gang

Open, share and attitude like air, water and life which God blessed