Skip to contentSkip to conversation

tools/libxl: Correctly check if libxl_get_scheduler has failed

PATCH In review Julien Grall 2 participants 3 messages Mar 19, 2014 – Mar 20, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/libxl: Correctly check if libxl_get_scheduler has failed

libxl_get_scheduler will return an enum, therefore checking if the value is negative is wrong.

Both GCC and clang will never go to the error case.

Spotted by clang:
xl_cmdimpl.c:6709:48: error: comparison of unsigned enum expression < 0 is always false [-Werror,-Wtautological-compare]

        if ((sched = libxl_get_scheduler(ctx)) < 0) {
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~

---
I'm not sure this is the right way to test if libxl_get_scheduler has failed.

This small program should print ERROR, but on both clang and gcc it will print OK.
#include <stdio.h>

typedef enum libxl_error

{
    ERROR_FAIL = -3,
} libxl_error;

typedef enum libxl_sched
{
SCHED_SCHED,
} libxl_sched;

libxl_sched f(void)

{
    return ERROR_FAIL;
}

int main(void)

{
    printf("f() = %d\n", f());
    if ( f() < 0 )
        printf("ERROR\n");
    else
        printf("OK\n");

    return 0;
}
---
 tools/libxl/xl_cmdimpl.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/libxl/xl_cmdimpl.c +2 -2
index 8990020..8f6c411 100644
@@ -4826,7 +4826,7 @@ static void output_xeninfo(void)
         return;
     }
 
-    if ((sched = libxl_get_scheduler(ctx)) < 0) {
+    if ((int)(sched = libxl_get_scheduler(ctx)) < 0) {
         fprintf(stderr, "get_scheduler sysctl failed.\n");
         return;
     }
@@ -6706,7 +6706,7 @@ int main_cpupoolcreate(int argc, char **argv)
             goto out_cfg;
         }
     } else {
-        if ((sched = libxl_get_scheduler(ctx)) < 0) {
+        if ((int)(sched = libxl_get_scheduler(ctx)) < 0) {
             fprintf(stderr, "get_scheduler sysctl failed.\n");
             goto out_cfg;
         }
Signature · 1 line
1.7.10.4
#1 lore raw

Julien Grall writes ("[PATCH] tools/libxl: Correctly check if libxl_get_scheduler has failed"):

Quoted 2 lines

libxl_get_scheduler will return an enum, therefore checking if the value is negative is wrong.

Gah. enums in C are so hateful.

Review

Quoted 2 lines
(quoted diff) +1 -1
-    if ((sched = libxl_get_scheduler(ctx)) < 0) {
+    if ((int)(sched = libxl_get_scheduler(ctx)) < 0) {

I'm afraid this isn't the right fix. The right fix is not to use the enum type at all. libxl_get_scheduler, and the "sched" local, should be ints.

Ian.

#2 lore raw

Hi Ian,

Quoted 4 lines · Ian Jackson

Julien Grall writes ("[PATCH] tools/libxl: Correctly check if libxl_get_scheduler has failed"):

Quoted 2 lines

libxl_get_scheduler will return an enum, therefore checking if the value is negative is wrong.

Quoted 8 lines

Gah. enums in C are so hateful.

Review

Quoted 2 lines
(quoted diff) +1 -1
-    if ((sched = libxl_get_scheduler(ctx)) < 0) {
+    if ((int)(sched = libxl_get_scheduler(ctx)) < 0) {

I'm afraid this isn't the right fix. The right fix is not to use the enum type at all. libxl_get_scheduler, and the "sched" local, should be ints.

Thanks. I will rework the patch.

Regards,

Signature · 1 line
Julien Grall