Skip to contentSkip to conversation

KVM: selftests: Fix a condition in test_hv_cpuid()

PATCH Applied Dan Carpenter 4 participants 4 messages May 14, 2019 – May 20, 2019

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

PATCH 1/1 KVM: selftests: Fix a condition in test_hv_cpuid()

The code is trying to check that all the padding is zeroed out and it does this:

    entry->padding[0] = entry->padding[1] = entry->padding[2] = 0

Assume everything is zeroed correctly, then the first comparison is true, the next comparison is false and false is equal to zero so the overall condition is true. This bug doesn't affect run time very badly, but the code should instead just check that all three paddings are zero individually.

Also the error message was copy and pasted from an earlier error and it wasn't correct.

Fixes 7edcb7343327 ("KVM: selftests: Add hyperv_cpuid test")
---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +2 -3
index 9a21e912097c..63b9fc3fdfbe 100644
@@ -58,9 +58,8 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
 		TEST_ASSERT(entry->flags = 0,
 			    ".flags field should be zero");
 
-		TEST_ASSERT(entry->padding[0] = entry->padding[1]
-			    = entry->padding[2] = 0,
-			    ".index field should be zero");
+		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
+			    !entry->padding[2], "padding should be zero");
 
 		/*
 		 * If needed for debug:
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Dan Carpenter

The code is trying to check that all the padding is zeroed out and it does this:

    entry->padding[0] = entry->padding[1] = entry->padding[2] = 0

Assume everything is zeroed correctly, then the first comparison is true, the next comparison is false and false is equal to zero so the overall condition is true. This bug doesn't affect run time very badly, but the code should instead just check that all three paddings are zero individually.

Also the error message was copy and pasted from an earlier error and it wasn't correct.

Fixes 7edcb7343327 ("KVM: selftests: Add hyperv_cpuid test")
---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 9a21e912097c..63b9fc3fdfbe 100644

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c static void test_hv_cpuid(struct kvm_cpuid2 · line 58

Quoted 1 line
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
@@ -58,9 +58,8 @@ static void test_hv_cpuid(struct kvm_cpuid2
 *hv_cpuid_entries,

we also seem to check for 'entry->index = 0' twice here.

Quoted 11 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +2 -3
 		TEST_ASSERT(entry->flags = 0,
 			    ".flags field should be zero");
 
-		TEST_ASSERT(entry->padding[0] = entry->padding[1]
-			    = entry->padding[2] = 0,
-			    ".index field should be zero");
+		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
+			    !entry->padding[2], "padding should be zero");
 
 		/*

* If needed for debug:

Signature · 1 line
Vitaly
Reviewed-by on v1 1/1
#2 lore raw
In reply to Dan Carpenter
Quoted 27 lines · Dan Carpenter

The code is trying to check that all the padding is zeroed out and it does this:

    entry->padding[0] = entry->padding[1] = entry->padding[2] = 0

Assume everything is zeroed correctly, then the first comparison is true, the next comparison is false and false is equal to zero so the overall condition is true. This bug doesn't affect run time very badly, but the code should instead just check that all three paddings are zero individually.

Also the error message was copy and pasted from an earlier error and it wasn't correct.

Fixes 7edcb7343327 ("KVM: selftests: Add hyperv_cpuid test")
---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 9a21e912097c..63b9fc3fdfbe 100644
@@ -58,9 +58,8 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
 		TEST_ASSERT(entry->flags = 0,

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, · line 65

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +2 -3
 			    ".flags field should be zero");
 
-		TEST_ASSERT(entry->padding[0] = entry->padding[1]
-			    = entry->padding[2] = 0,
-			    ".index field should be zero");
+		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
+			    !entry->padding[2], "padding should be zero");
 
 		/*
 		 * If needed for debug:

That was yet another good example that compiling with "-Wall" is a good idea...

maintainer Applied
#3 lore raw
In reply to Dan Carpenter
Quoted 27 lines · Dan Carpenter

The code is trying to check that all the padding is zeroed out and it does this:

    entry->padding[0] = entry->padding[1] = entry->padding[2] = 0

Assume everything is zeroed correctly, then the first comparison is true, the next comparison is false and false is equal to zero so the overall condition is true. This bug doesn't affect run time very badly, but the code should instead just check that all three paddings are zero individually.

Also the error message was copy and pasted from an earlier error and it wasn't correct.

Fixes 7edcb7343327 ("KVM: selftests: Add hyperv_cpuid test")
---
 tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +0 -0
index 9a21e912097c..63b9fc3fdfbe 100644
@@ -58,9 +58,8 @@ static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries,
 		TEST_ASSERT(entry->flags = 0,

tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c static void test_hv_cpuid(struct kvm_cpuid2 *hv_cpuid_entries, · line 65

Quoted 10 lines
tools/testing/selftests/kvm/x86_64/hyperv_cpuid.c +2 -3
 			    ".flags field should be zero");
 
-		TEST_ASSERT(entry->padding[0] = entry->padding[1]
-			    = entry->padding[2] = 0,
-			    ".index field should be zero");
+		TEST_ASSERT(!entry->padding[0] && !entry->padding[1] &&
+			    !entry->padding[2], "padding should be zero");
 
 		/*
 		 * If needed for debug:

Queued, thanks.

Paolo