arm: virt: Fix the segmentation fault when specifying an unsupported CPU

Message ID 1484477462-14088-1-git-send-email-zhaoshenglong@huawei.com
State New
Headers show

Commit Message

Shannon Zhao Jan. 15, 2017, 10:51 a.m.
From: Shannon Zhao <shannon.zhao@linaro.org>


For example, using -cpu generic will cause qemu segmentation fault.

Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>

---
 hw/arm/virt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

-- 
2.0.4

Comments

Peter Maydell Jan. 16, 2017, 5:27 p.m. | #1
On 15 January 2017 at 10:51, Shannon Zhao <zhaoshenglong@huawei.com> wrote:
> From: Shannon Zhao <shannon.zhao@linaro.org>

>

> For example, using -cpu generic will cause qemu segmentation fault.

>

> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>

> ---

>  hw/arm/virt.c | 2 +-

>  1 file changed, 1 insertion(+), 1 deletion(-)

>

> diff --git a/hw/arm/virt.c b/hw/arm/virt.c

> index 7a03f84..4b301c2 100644

> --- a/hw/arm/virt.c

> +++ b/hw/arm/virt.c

> @@ -175,7 +175,7 @@ static bool cpuname_valid(const char *cpu)

>      int i;

>

>      for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {

> -        if (strcmp(cpu, valid_cpus[i]) == 0) {

> +        if (valid_cpus[i] != NULL && strcmp(cpu, valid_cpus[i]) == 0) {

>              return true;

>          }

>      }


A better fix is to just remove the NULL entry from the
valid_cpus[] array. We already have one "stop when we
run out of entries" condition (the ARRAY_SIZE check),
we don't need two...

thanks
-- PMM
Shannon Zhao Jan. 17, 2017, 1:21 a.m. | #2
On 2017/1/17 1:27, Peter Maydell wrote:
> On 15 January 2017 at 10:51, Shannon Zhao <zhaoshenglong@huawei.com> wrote:

>> From: Shannon Zhao <shannon.zhao@linaro.org>

>>

>> For example, using -cpu generic will cause qemu segmentation fault.

>>

>> Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org>

>> ---

>>  hw/arm/virt.c | 2 +-

>>  1 file changed, 1 insertion(+), 1 deletion(-)

>>

>> diff --git a/hw/arm/virt.c b/hw/arm/virt.c

>> index 7a03f84..4b301c2 100644

>> --- a/hw/arm/virt.c

>> +++ b/hw/arm/virt.c

>> @@ -175,7 +175,7 @@ static bool cpuname_valid(const char *cpu)

>>      int i;

>>

>>      for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {

>> -        if (strcmp(cpu, valid_cpus[i]) == 0) {

>> +        if (valid_cpus[i] != NULL && strcmp(cpu, valid_cpus[i]) == 0) {

>>              return true;

>>          }

>>      }

> 

> A better fix is to just remove the NULL entry from the

> valid_cpus[] array. We already have one "stop when we

> run out of entries" condition (the ARRAY_SIZE check),

> we don't need two...

> 

Right. Will send the update one.

Thanks,
-- 
Shannon

Patch

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index 7a03f84..4b301c2 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -175,7 +175,7 @@  static bool cpuname_valid(const char *cpu)
     int i;
 
     for (i = 0; i < ARRAY_SIZE(valid_cpus); i++) {
-        if (strcmp(cpu, valid_cpus[i]) == 0) {
+        if (valid_cpus[i] != NULL && strcmp(cpu, valid_cpus[i]) == 0) {
             return true;
         }
     }