diff mbox series

[1/5] target/loongarch/cpu: Fix cpu_class_by_name function

Message ID 20220713095036.705102-2-yangxiaojuan@loongson.cn (mailing list archive)
State New, archived
Headers show
Series Fix LoongArch coverity error and cpu name bug | expand

Commit Message

Xiaojuan Yang July 13, 2022, 9:50 a.m. UTC
In loongarch_cpu_class_by_name(char *cpu_model) function,
the argument cpu_model already has the suffix '-loongarch-cpu',
so we should remove the LOONGARCH_CPU_TYPE_NAME(cpu_model) macro.
And add the assertion that 'cpu_model' resolves to a class of the
appropriate type.

Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn>
---
 target/loongarch/cpu.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

Comments

Richard Henderson July 13, 2022, 4:02 p.m. UTC | #1
On 7/13/22 15:20, Xiaojuan Yang wrote:
> In loongarch_cpu_class_by_name(char *cpu_model) function,
> the argument cpu_model already has the suffix '-loongarch-cpu',
> so we should remove the LOONGARCH_CPU_TYPE_NAME(cpu_model) macro.
> And add the assertion that 'cpu_model' resolves to a class of the
> appropriate type.
> 
> Signed-off-by: Xiaojuan Yang <yangxiaojuan@loongson.cn>
> ---
>   target/loongarch/cpu.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~
> 
> diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
> index e21715592a..ed26f9beed 100644
> --- a/target/loongarch/cpu.c
> +++ b/target/loongarch/cpu.c
> @@ -571,11 +571,12 @@ static void loongarch_cpu_init(Object *obj)
>   static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
>   {
>       ObjectClass *oc;
> -    char *typename;
>   
> -    typename = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
> -    oc = object_class_by_name(typename);
> -    g_free(typename);
> +    oc = object_class_by_name(cpu_model);
> +    if (!oc || !object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU) ||
> +        object_class_is_abstract(oc)) {
> +        return NULL;
> +    }
>       return oc;
>   }
>
diff mbox series

Patch

diff --git a/target/loongarch/cpu.c b/target/loongarch/cpu.c
index e21715592a..ed26f9beed 100644
--- a/target/loongarch/cpu.c
+++ b/target/loongarch/cpu.c
@@ -571,11 +571,12 @@  static void loongarch_cpu_init(Object *obj)
 static ObjectClass *loongarch_cpu_class_by_name(const char *cpu_model)
 {
     ObjectClass *oc;
-    char *typename;
 
-    typename = g_strdup_printf(LOONGARCH_CPU_TYPE_NAME("%s"), cpu_model);
-    oc = object_class_by_name(typename);
-    g_free(typename);
+    oc = object_class_by_name(cpu_model);
+    if (!oc || !object_class_dynamic_cast(oc, TYPE_LOONGARCH_CPU) ||
+        object_class_is_abstract(oc)) {
+        return NULL;
+    }
     return oc;
 }