Skip to contentSkip to conversation

arm: Initialize target in kvm_cpu__arch_init

PATCH In review Mostafa Saleh 2 participants 3 messages Sep 27, 2023 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm: Initialize target in kvm_cpu__arch_init

After updating my toolchain to:
aarch64-linux-gnu-gcc (Debian 13.2.0-2) 13.2.0

I hit compilation error:
arm/kvm-cpu.c: In function ‘kvm_cpu__arch_init’:
arm/kvm-cpu.c:119:41: error: ‘target’ may be used uninitialized [-Werror=maybe-uninitialized]

  119 |         vcpu->cpu_compatible    = target->compatible;
      |                                   ~~~~~~^~~~~~~~~~~~

arm/kvm-cpu.c:40:32: note: ‘target’ was declared here
40 | struct kvm_arm_target *target;
| ^~~~~~

target is guaranteed to be initialized, as targets would be registered from other compilation units (arm/aarch(32|64)/arm-cpu.c).

Initializing the variable to NULL is sufficient to silence the compiler.

---
 arm/kvm-cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arm/kvm-cpu.c +1 -1
index 98bc5fd..57f92ee 100644
@@ -37,7 +37,7 @@ int kvm_cpu__register_kvm_arm_target(struct kvm_arm_target *target)
 
 struct kvm_cpu *kvm_cpu__arch_init(struct kvm *kvm, unsigned long cpu_id)
 {
-	struct kvm_arm_target *target;
+	struct kvm_arm_target *target = NULL;
 	struct kvm_cpu *vcpu;
 	int coalesced_offset, mmap_size, err = -1;
 	unsigned int i;
Signature · 1 line
2.42.0.582.g8ccd20d70d-goog
#1 lore raw
Quoted 7 lines · Mostafa Saleh

arm/kvm-cpu.c: In function ‘kvm_cpu__arch_init’:
arm/kvm-cpu.c:119:41: error: ‘target’ may be used uninitialized [-Werror=maybe-uninitialized]

  119 |         vcpu->cpu_compatible    = target->compatible;
      |                                   ~~~~~~^~~~~~~~~~~~

arm/kvm-cpu.c:40:32: note: ‘target’ was declared here
40 | struct kvm_arm_target *target;
| ^~~~~~

Already addressed by 426e875213d3 ("arm/kvm-cpu: Fix new build warning").

Zenghui

#2 lore raw

Hi Zenghui,

Quoted 11 lines · Zenghui Yu
Quoted 7 lines · Mostafa Saleh

arm/kvm-cpu.c: In function ‘kvm_cpu__arch_init’:
arm/kvm-cpu.c:119:41: error: ‘target’ may be used uninitialized [-Werror=maybe-uninitialized]

  119 |         vcpu->cpu_compatible    = target->compatible;
      |                                   ~~~~~~^~~~~~~~~~~~

arm/kvm-cpu.c:40:32: note: ‘target’ was declared here
40 | struct kvm_arm_target *target;
| ^~~~~~

Already addressed by 426e875213d3 ("arm/kvm-cpu: Fix new build warning").

Oh, I see that now, I was on the wrong branch, thanks for pointing this!

Quoted 1 line

Zenghui

Thanks,
Mostafa