Page MenuHomeFreeBSD

linuxkpi: Add parentheses to pacify -Wparentheses warnings from GCC.
ClosedPublic

Authored by jhb on Feb 2 2022, 8:20 PM.
Tags
None
Referenced Files
F102655780: D34145.diff
Fri, Nov 15, 10:26 AM
Unknown Object (File)
Tue, Oct 22, 11:12 PM
Unknown Object (File)
Oct 14 2024, 9:41 PM
Unknown Object (File)
Sep 20 2024, 9:06 PM
Unknown Object (File)
Sep 17 2024, 11:51 AM
Unknown Object (File)
Sep 17 2024, 12:07 AM
Unknown Object (File)
Sep 5 2024, 8:58 AM
Unknown Object (File)
Aug 31 2024, 4:02 PM

Diff Detail

Repository
rS FreeBSD src repository - subversion
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 44252
Build 41140: arc lint + arc unit

Event Timeline

jhb requested review of this revision.Feb 2 2022, 8:20 PM

Accept for the mac80211 change; the xarray also seems ok.

This revision is now accepted and ready to land.Feb 2 2022, 9:42 PM

mac80211 LGTM
I'd rather __xa_alloc_cyclic be deobfuscated

hselasky added a subscriber: hselasky.
hselasky added inline comments.
sys/compat/linuxkpi/common/src/linux_xarray.c
106

It should be:
MPASS(mask > ((xa->flags & XA_FLAGS_ALLOC1) ? 1 : 0))

Ditto for next one.

sys/compat/linuxkpi/common/src/linux_xarray.c
106

Eh, why the ternary at all? Just

MPASS(mask > (xa->flags & XA_FLAGS_ALLOC1));

should be sufficient in that case?

sys/compat/linuxkpi/common/src/linux_xarray.c
106
#define	XA_FLAGS_ALLOC1 (1U << 2)

@jhb: This is not the same, when you look at the definition.

sys/compat/linuxkpi/common/src/linux_xarray.c
106

MPASS(mask > ((xa->flags & XA_FLAGS_ALLOC1) ? 1 : 0))

This version looks good

sys/compat/linuxkpi/common/src/linux_xarray.c
106

It doesn't matter. KASSERT uses if(), so the expanded expression is:

if (!(mask > (xa->flags & XA_FLAGS_ALLOC1)))
   panic(...)

It doesn't care about exact values of 0 or 1, just 0 and non-zero. More typical FreeBSD style would probably be to use '!= 0' to generate the boolean expression than a ternary.

sys/compat/linuxkpi/common/src/linux_xarray.c
106

Remember there is a ">" greater operator there, that compare two values. Of course which values being compared matters!

"mask" is the one value, and the other one is "((xa->flags & XA_FLAGS_ALLOC1) ? 1 : 0)" 1 or 0. After your change the assert compares 0 or 4 with mask!

Is anything unclear?

sys/compat/linuxkpi/common/src/linux_xarray.c
106

Oh, so it's not pacifying the warning, it's that the code is actually broken as-is? ?: has lower priority than >, so today the > is already evaluated first. And in fact, the > is even evaluated before the !=, so today what you have is:

((mask > (xa->flags & XA_FLAGS_ALLOC1)) != 0) ? 1 : 0
sys/compat/linuxkpi/common/src/linux_xarray.c
106

Maybe, I'm not sure.

via godbolt.org,

((mask > (xa->flags & XA_FLAGS_ALLOC1)) != 0) ? 1 : 0

mov     rax, QWORD PTR [rbp-16]
mov     eax, DWORD PTR [rax]
shr     eax, 2
and     eax, 1
cmp     DWORD PTR [rbp-4], eax
setg    al
movzx   eax, al

mask > ((xa->flags & XA_FLAGS_ALLOC1) ? 1 : 0))

mov     rax, QWORD PTR [rbp-16]
mov     eax, DWORD PTR [rax]
shr     eax, 2
and     eax, 1
cmp     DWORD PTR [rbp-4], eax
setg    al
movzx   eax, al

mask > ((xa->flags & XA_FLAGS_ALLOC1) ? 1 : 0))

        mov     rax, QWORD PTR [rbp-16]
        mov     eax, DWORD PTR [rax]
        and     eax, 4
        mov     edx, eax
        mov     eax, DWORD PTR [rbp-4]
        cmp     edx, eax
        setb    al
        movzx   eax, al
        test    eax, eax
        je      .L2
        mov     eax, 1
        jmp     .L4
.L2:
        mov     eax, 0
.L4:

return ((mask > (xa->flags & XA_FLAGS_ALLOC1)) != 0) ? 1 : 0

        mov     rax, QWORD PTR [rbp-16]
        mov     eax, DWORD PTR [rax]
        and     eax, 4
        mov     edx, eax
        mov     eax, DWORD PTR [rbp-4]
        cmp     edx, eax
        setb    al
        movzx   eax, al
        test    eax, eax
        je      .L2
        mov     eax, 1
        jmp     .L4
.L2:
        mov     eax, 0
.L4:
sys/compat/linuxkpi/common/src/linux_xarray.c
106

Yes, this is fixing a bug not pacifying a warning. @jhb's example above compiles to the same as what is currently in the tree.

sys/compat/linuxkpi/common/src/linux_xarray.c
106

Oh, so it's not pacifying the warning, it's that the code is actually broken as-is? ?: has lower priority than >, so today the > is already evaluated first. And in fact, the > is even evaluated before the !=, so today what you have is:

((mask > (xa->flags & XA_FLAGS_ALLOC1)) != 0) ? 1 : 0

Yes, the code is actually broken. With all parentheses set in their places It should look like

MPASS(mask > (((xa->flags & XA_FLAGS_ALLOC1) != 0) ? 1 : 0));

Accidentally current broken code does almost the same check the proper code does.

Move xarray changes to a new change.

This revision now requires review to proceed.Feb 7 2022, 9:34 PM
This revision is now accepted and ready to land.Feb 7 2022, 9:34 PM

Moved the linux_xarray.c changes to D34197