qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function
@ 2023-10-12  8:57 Sergey Mironov
  2023-10-12 15:16 ` Alex Bennée
  2023-10-16 16:23 ` Peter Maydell
  0 siblings, 2 replies; 4+ messages in thread
From: Sergey Mironov @ 2023-10-12  8:57 UTC (permalink / raw)
  To: qemu-devel, peter.maydell, qemu-arm; +Cc: Sergey Mironov

6 out of 7 calls to get_arm_cp_reginfo() are checked

Signed-off-by: Sergey Mironov <mironov@fintech.ru>
---
 target/arm/helper.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/target/arm/helper.c b/target/arm/helper.c
index 74fbb6e1d7..cffbbaf571 100644
--- a/target/arm/helper.c
+++ b/target/arm/helper.c
@@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
     uint32_t regidx = (uintptr_t)key;
     const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
 
+    assert(ri != NULL);
     if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
         cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
         /* The value array need not be initialized at this point */
-- 
2.31.1



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function
  2023-10-12  8:57 [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function Sergey Mironov
@ 2023-10-12 15:16 ` Alex Bennée
  2023-10-16 16:23 ` Peter Maydell
  1 sibling, 0 replies; 4+ messages in thread
From: Alex Bennée @ 2023-10-12 15:16 UTC (permalink / raw)
  To: Sergey Mironov; +Cc: peter.maydell, qemu-arm, qemu-devel


Sergey Mironov <mironov@fintech.ru> writes:

> 6 out of 7 calls to get_arm_cp_reginfo() are checked

Yes but we should be careful with asserts (vs if (ri) legs) because I
don't think get_arm_cp_reginfo() guarantees it will always be
successful.

>
> Signed-off-by: Sergey Mironov <mironov@fintech.ru>
> ---
>  target/arm/helper.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/target/arm/helper.c b/target/arm/helper.c
> index 74fbb6e1d7..cffbbaf571 100644
> --- a/target/arm/helper.c
> +++ b/target/arm/helper.c
> @@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
>      uint32_t regidx = (uintptr_t)key;
>      const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
>  
> +    assert(ri != NULL);

  /* must always succeed as we are iterating the keys of cp_regs */
  assert(ri);

is enough for a !NULL check.

>      if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
>          cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
>          /* The value array need not be initialized at this point */

That said we already have an assert that would fire in
init_cpregs_list():

  assert(cpu->cpreg_array_len == arraylen);

so I'm not sure what this is adding to ensuring the contract is kept.

-- 
Alex Bennée
Virtualisation Tech Lead @ Linaro


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function
  2023-10-12  8:57 [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function Sergey Mironov
  2023-10-12 15:16 ` Alex Bennée
@ 2023-10-16 16:23 ` Peter Maydell
  2023-10-17  7:48   ` Миронов Сергей Владимирович
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Maydell @ 2023-10-16 16:23 UTC (permalink / raw)
  To: Sergey Mironov; +Cc: qemu-devel, qemu-arm

On Thu, 12 Oct 2023 at 09:57, Sergey Mironov <mironov@fintech.ru> wrote:
>
> 6 out of 7 calls to get_arm_cp_reginfo() are checked

This sounds like it's talking about a Coverity warning, though
it doesn't say so. Is that the motivation here ? If so,
it would be good to say so in the commit message. If not,
the commit message should explain why we're making the change.

That particular Coverity warning is quite prone to false
positives, since it's only a heuristic. Sometimes it's
useful to add an assert(), if it helps both Coverity and
human readers, but not always.

assert()s are also most useful if there's a comment that explains
why we can assume the thing they're assuming, as Alex suggests.

> Signed-off-by: Sergey Mironov <mironov@fintech.ru>
> ---
>  target/arm/helper.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/target/arm/helper.c b/target/arm/helper.c
> index 74fbb6e1d7..cffbbaf571 100644
> --- a/target/arm/helper.c
> +++ b/target/arm/helper.c
> @@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
>      uint32_t regidx = (uintptr_t)key;
>      const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
>
> +    assert(ri != NULL);

>      if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
>          cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
>          /* The value array need not be initialized at this point */
> --
> 2.31.1

thanks
-- PMM


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function
  2023-10-16 16:23 ` Peter Maydell
@ 2023-10-17  7:48   ` Миронов Сергей Владимирович
  0 siblings, 0 replies; 4+ messages in thread
From: Миронов Сергей Владимирович @ 2023-10-17  7:48 UTC (permalink / raw)
  To: Peter Maydell; +Cc: qemu-devel@nongnu.org, qemu-arm@nongnu.org

[-- Attachment #1: Type: text/plain, Size: 2074 bytes --]

Yes, the warning was initially received in the static analyzer SVACE,

the same type as Coverity.

In this case, return value of a function 'get_arm_cp_reginfo' is referenced
at helper.c without checking for ALL, but it is usually checked for this function (8/9).

________________________________
От: Peter Maydell <peter.maydell@linaro.org>
Отправлено: 16 октября 2023 г. 19:23
Кому: Миронов Сергей Владимирович
Копия: qemu-devel@nongnu.org; qemu-arm@nongnu.org
Тема: Re: [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function

On Thu, 12 Oct 2023 at 09:57, Sergey Mironov <mironov@fintech.ru> wrote:
>
> 6 out of 7 calls to get_arm_cp_reginfo() are checked

This sounds like it's talking about a Coverity warning, though
it doesn't say so. Is that the motivation here ? If so,
it would be good to say so in the commit message. If not,
the commit message should explain why we're making the change.

That particular Coverity warning is quite prone to false
positives, since it's only a heuristic. Sometimes it's
useful to add an assert(), if it helps both Coverity and
human readers, but not always.

assert()s are also most useful if there's a comment that explains
why we can assume the thing they're assuming, as Alex suggests.

> Signed-off-by: Sergey Mironov <mironov@fintech.ru>
> ---
>  target/arm/helper.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/target/arm/helper.c b/target/arm/helper.c
> index 74fbb6e1d7..cffbbaf571 100644
> --- a/target/arm/helper.c
> +++ b/target/arm/helper.c
> @@ -198,6 +198,7 @@ static void add_cpreg_to_list(gpointer key, gpointer opaque)
>      uint32_t regidx = (uintptr_t)key;
>      const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
>
> +    assert(ri != NULL);

>      if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) {
>          cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
>          /* The value array need not be initialized at this point */
> --
> 2.31.1

thanks
-- PMM

[-- Attachment #2: Type: text/html, Size: 4261 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-10-17  7:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-12  8:57 [PATCH 1/1] target/arm: Adding a check for the result of calling the CPU information check function Sergey Mironov
2023-10-12 15:16 ` Alex Bennée
2023-10-16 16:23 ` Peter Maydell
2023-10-17  7:48   ` Миронов Сергей Владимирович

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).