4 * redundant conditions

Bug #1464611 reported by dcb
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
QEMU
Fix Released
Undecided
Unassigned

Bug Description

1.

[qemu/hw/block/nvme.c:355]: (style) Redundant condition: sqid. 'A && (!A || B)' is equivalent to 'A || B'

  if (!sqid || (sqid && !nvme_check_sqid(n, sqid))) {

2.

[qemu/hw/block/nvme.c:429]: (style) Redundant condition: cqid. 'A && (!A || B)' is equivalent to 'A || B'

  if (!cqid || (cqid && !nvme_check_cqid(n, cqid))) {

3.

[qemu/hw/tpm/tpm_passthrough.c:157]: (style) Redundant condition: tpm_pt.tpm_op_canceled. 'A && (!A || B)' is equivalent to 'A || B'

     if (!tpm_pt->tpm_op_canceled ||
            (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {

4.

[qemu/target-arm/translate-a64.c:5729]: (style) Redundant condition: size<3. 'A && (!A || B)' is equivalent to 'A || B'

      if (size > 3
            || (size < 3 && is_q)
            || (size == 3 && !is_q)) {

Revision history for this message
Peter Maydell (pmaydell) wrote : Re: [Qemu-devel] [Bug 1464611] [NEW] 4 * redundant conditions

On 12 June 2015 at 11:38, dcb <email address hidden> wrote:
> Public bug reported:
>
>
> 1.
>
> [qemu/hw/block/nvme.c:355]: (style) Redundant condition: sqid. 'A && (!A
> || B)' is equivalent to 'A || B'
>
> if (!sqid || (sqid && !nvme_check_sqid(n, sqid))) {
>
> 2.
>
> [qemu/hw/block/nvme.c:429]: (style) Redundant condition: cqid. 'A && (!A
> || B)' is equivalent to 'A || B'
>
> if (!cqid || (cqid && !nvme_check_cqid(n, cqid))) {
>
> 3.
>
> [qemu/hw/tpm/tpm_passthrough.c:157]: (style) Redundant condition:
> tpm_pt.tpm_op_canceled. 'A && (!A || B)' is equivalent to 'A || B'
>
> if (!tpm_pt->tpm_op_canceled ||
> (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {

These three are all straightforward and would look simpler
in their simplified versions...

> 4.
>
> [qemu/target-arm/translate-a64.c:5729]: (style) Redundant condition:
> size<3. 'A && (!A || B)' is equivalent to 'A || B'
>
> if (size > 3
> || (size < 3 && is_q)
> || (size == 3 && !is_q)) {

...but I'm less sure about this one. I'm not even sure
what it's trying to suggest this should simplify to:
just dropping "size < 3" is obviously wrong, and the
condition format isn't "A && (!A || B)" either.

thanks
-- PMM

Revision history for this message
Eric Blake (eblake) wrote :

On 06/12/2015 05:01 AM, Peter Maydell wrote:

>> 4.
>>
>> [qemu/target-arm/translate-a64.c:5729]: (style) Redundant condition:
>> size<3. 'A && (!A || B)' is equivalent to 'A || B'
>>
>> if (size > 3
>> || (size < 3 && is_q)
>> || (size == 3 && !is_q)) {
>
> ...but I'm less sure about this one. I'm not even sure
> what it's trying to suggest this should simplify to:
> just dropping "size < 3" is obviously wrong, and the
> condition format isn't "A && (!A || B)" either.

Let's break it down into the 6 possibilities based on the binary *
ternary conditions being checked:

> 3, is_q => accept
> 3, !is_q => accept
== 3, is_q => reject
== 3, !is_q => accept
< 3, is_q => accept
< 3, !is_q => reject

Here's a shorter conditional with the same properties, but it's gross:

if (size > 3 || (is_q != (size == 3))) {

Too much mental thought to prove it accepts the same set of conditions.

--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Revision history for this message
Peter Maydell (pmaydell) wrote :

On 12 June 2015 at 14:03, Eric Blake <email address hidden> wrote:
> On 06/12/2015 05:01 AM, Peter Maydell wrote:
>
>>> 4.
>>>
>>> [qemu/target-arm/translate-a64.c:5729]: (style) Redundant condition:
>>> size<3. 'A && (!A || B)' is equivalent to 'A || B'
>>>
>>> if (size > 3
>>> || (size < 3 && is_q)
>>> || (size == 3 && !is_q)) {
>>
>> ...but I'm less sure about this one. I'm not even sure
>> what it's trying to suggest this should simplify to:
>> just dropping "size < 3" is obviously wrong, and the
>> condition format isn't "A && (!A || B)" either.
>
> Let's break it down into the 6 possibilities based on the binary *
> ternary conditions being checked:
>
>> 3, is_q => accept
>> 3, !is_q => accept
> == 3, is_q => reject
> == 3, !is_q => accept
> < 3, is_q => accept
> < 3, !is_q => reject
>
> Here's a shorter conditional with the same properties, but it's gross:
>
> if (size > 3 || (is_q != (size == 3))) {
>
> Too much mental thought to prove it accepts the same set of conditions.

Yeah, I think this is the kind of thing where I say "the compiler
should do this simplification if it cares enough" :-)

-- PMM

Revision history for this message
dcb (dcb314) wrote :

>These three are all straightforward and would look simpler
>in their simplified versions...

Agreed. The first 3 look valid candidates for simplification.

> 4.
>
> [qemu/target-arm/translate-a64.c:5729]: (style) Redundant condition:
> size<3. 'A && (!A || B)' is equivalent to 'A || B'
>
> if (size > 3
> || (size < 3 && is_q)
> || (size == 3 && !is_q)) {

>...but I'm less sure about this one.

Me too. Suggest regard as a false positive from the static analysis tool
and so leave the original code alone.

Revision history for this message
Thomas Huth (th-huth) wrote :
Changed in qemu:
status: New → Fix Committed
Revision history for this message
Thomas Huth (th-huth) wrote :

Released with version 2.8

Changed in qemu:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.