Skip to contentSkip to conversation

New Defects reported by Coverity Scan for XenProject

DISCUSSION Jan Beulich 2 participants 5 messages Sep 8, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 12 lines · On 07.09.2025 16:37, [email protected] wrote:

** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()

_____________________________________________________________________________________________
*** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102

Quoted 9 lines
103     found_first:
>>>     CID 1665362:         Integer handling issues  (INTEGER_OVERFLOW)
>>>     Expression "0xffffffffffffffffUL << size", where "size" is known to be equal to 63, overflows the type of "0xffffffffffffffffUL << size", which is type "unsigned long".
104     	tmp |= ~0UL << size;
105     	if (tmp == ~0UL)	/* Are any bits zero? */
106     		return result + size;	/* Nope. */
107     found_middle:
108     	return result + ffz(tmp);
109     }

I cannot make sense of their claim. 0xffffffffffffffffUL << 63 is simply 0x8000000000000000UL, isn't it? Where's the overflow there? There also cannot be talk of a 32-bit build, or else ~0UL would have been transformed to 0xffffffffUL.

Jan

#2 lore raw
Quoted 23 lines · Jan Beulich
Quoted 12 lines · On 07.09.2025 16:37, [email protected] wrote:

** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()

_____________________________________________________________________________________________
*** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102

Quoted 9 lines
103     found_first:
>>>     CID 1665362:         Integer handling issues  (INTEGER_OVERFLOW)
>>>     Expression "0xffffffffffffffffUL << size", where "size" is known to be equal to 63, overflows the type of "0xffffffffffffffffUL << size", which is type "unsigned long".
104     	tmp |= ~0UL << size;
105     	if (tmp == ~0UL)	/* Are any bits zero? */
106     		return result + size;	/* Nope. */
107     found_middle:
108     	return result + ffz(tmp);
109     }
Quoted 6 lines

I cannot make sense of their claim. 0xffffffffffffffffUL << 63 is simply 0x8000000000000000UL, isn't it? Where's the overflow there? There also cannot be talk of a 32-bit build, or else ~0UL would have been transformed to 0xffffffffUL.

Jan

The offending line LGTM too.

The only credible explanation I can think of is Coverity flagging discarded 1s on left shifts as loss of precision.

If so, "~((1 << size) - 1)", or "(~0UL >> size) << size" should make it happy, but surely that error would flare up with all uses of GENMASK() too?

Cheers,
Alejandro

#3 lore raw
Quoted 29 lines · Alejandro Vallejo
Quoted 23 lines · Jan Beulich
Quoted 12 lines · On 07.09.2025 16:37, [email protected] wrote:

** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()

_____________________________________________________________________________________________
*** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102

Quoted 9 lines
103     found_first:
>>>     CID 1665362:         Integer handling issues  (INTEGER_OVERFLOW)
>>>     Expression "0xffffffffffffffffUL << size", where "size" is known to be equal to 63, overflows the type of "0xffffffffffffffffUL << size", which is type "unsigned long".
104     	tmp |= ~0UL << size;
105     	if (tmp == ~0UL)	/* Are any bits zero? */
106     		return result + size;	/* Nope. */
107     found_middle:
108     	return result + ffz(tmp);
109     }
Quoted 4 lines

I cannot make sense of their claim. 0xffffffffffffffffUL << 63 is simply 0x8000000000000000UL, isn't it? Where's the overflow there? There also cannot be talk of a 32-bit build, or else ~0UL would have been transformed to 0xffffffffUL.

Quoted 7 lines

The offending line LGTM too.

The only credible explanation I can think of is Coverity flagging discarded 1s on left shifts as loss of precision.

If so, "~((1 << size) - 1)", or "(~0UL >> size) << size" should make it happy, but surely that error would flare up with all uses of GENMASK() too?

And with any other non-zero values of "size" here.

Jan

#4 lore raw
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Alejandro Vallejo
Quoted 23 lines · Jan Beulich
Quoted 12 lines · On 07.09.2025 16:37, [email protected] wrote:

** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()

_____________________________________________________________________________________________
*** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102

Quoted 9 lines
103     found_first:
>>>     CID 1665362:         Integer handling issues  (INTEGER_OVERFLOW)
>>>     Expression "0xffffffffffffffffUL << size", where "size" is known to be equal to 63, overflows the type of "0xffffffffffffffffUL << size", which is type "unsigned long".
104     	tmp |= ~0UL << size;
105     	if (tmp == ~0UL)	/* Are any bits zero? */
106     		return result + size;	/* Nope. */
107     found_middle:
108     	return result + ffz(tmp);
109     }
Quoted 4 lines

I cannot make sense of their claim. 0xffffffffffffffffUL << 63 is simply 0x8000000000000000UL, isn't it? Where's the overflow there? There also cannot be talk of a 32-bit build, or else ~0UL would have been transformed to 0xffffffffUL.

The offending line LGTM too.

The only credible explanation I can think of is Coverity flagging discarded 1s on left shifts as loss of precision.

Quoted 6 lines
Quoted 2 lines

If so, "~((1 << size) - 1)", or "(~0UL >> size) << size" should make it happy, but surely that error would flare up with all uses of GENMASK() too?

And with any other non-zero values of "size" here.

Jan

Is this the only issue flagged? Or are there any others of the same kind? It might me easier to spot a pattern with a larger dataset.

Cheers,
Alejandro

#5 lore raw
Quoted 36 lines · Alejandro Vallejo
Quoted 35 lines · Jan Beulich
Quoted 34 lines · Alejandro Vallejo
Quoted 23 lines · Jan Beulich
Quoted 12 lines · On 07.09.2025 16:37, [email protected] wrote:

** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()

_____________________________________________________________________________________________
*** CID 1665362: Integer handling issues (INTEGER_OVERFLOW)
/xen/lib/find-next-bit.c: 104 in find_next_zero_bit()
98 }
99 if (!size)
100 return result;
101 tmp = *p;
102

Quoted 9 lines
103     found_first:
>>>     CID 1665362:         Integer handling issues  (INTEGER_OVERFLOW)
>>>     Expression "0xffffffffffffffffUL << size", where "size" is known to be equal to 63, overflows the type of "0xffffffffffffffffUL << size", which is type "unsigned long".
104     	tmp |= ~0UL << size;
105     	if (tmp == ~0UL)	/* Are any bits zero? */
106     		return result + size;	/* Nope. */
107     found_middle:
108     	return result + ffz(tmp);
109     }
Quoted 4 lines

I cannot make sense of their claim. 0xffffffffffffffffUL << 63 is simply 0x8000000000000000UL, isn't it? Where's the overflow there? There also cannot be talk of a 32-bit build, or else ~0UL would have been transformed to 0xffffffffUL.

The offending line LGTM too.

The only credible explanation I can think of is Coverity flagging discarded 1s on left shifts as loss of precision.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

If so, "~((1 << size) - 1)", or "(~0UL >> size) << size" should make it happy, but surely that error would flare up with all uses of GENMASK() too?

And with any other non-zero values of "size" here.

Is this the only issue flagged? Or are there any others of the same kind? It might me easier to spot a pattern with a larger dataset.

I've seen only this one report.

Jan