diff mbox

Re: [PATCH 1/5] Suppress some gcc warnings with -Wtype-limits

Message ID AANLkTi=_DZi1XoUBEUiiybMgaHMmi8q4gdBtnE0++L2C@mail.gmail.com
State New
Headers show

Commit Message

Blue Swirl Sept. 5, 2010, 9:44 a.m. UTC
On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>> >> In the unsigned number space, the checks can be merged into one,
>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>> >> could have:
>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>> >>
>> >> This would also implement the check that the writer of this code was
>> >> trying to make.
>> >> The important thing to note is however is that the check as it is now
>> >> is not correct.
>> >
>> > I agree. But it seems to indicate a bigger problem.
>> >
>> > If we are trying to pass in a negative value, which is not one
>> > of enum values, using BlkDebugEvent as type is just confusing,
>> > we should just pass int instead.
>>
>> AFAICT it's only possible to use the values listed in event_names in
>> blkdebug.c, other values are rejected. So the check should actually be
>> an assert() or it could even be removed.
>
> Sounds good.
>
>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>> >> >> check? Then if the value changes, the need to add the comparison back
>> >> >> will be obvious.
>> >> >
>> >> > This would work but it's weird.  The thing is it's currently a correct
>> >> > code and the check may be useless but it's the optimiser's task to
>> >> > remove it, not ours.  The compiler is not able to tell whether the
>> >> > check makes sense or nott, because the compiler only has access to
>> >> > preprocessed code.  So why should you let the compiler have anything
>> >> > to say on it.
>> >>
>> >> Good point. I'll try to invent something better.
>> >
>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>
>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>
>> I think the assertion is still the best way, it ensures that something
>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>> adding a new define could still forget to adjust the check anyway.
>
> We could replace it with a macro
> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
> but all this does look artificial. And of course using type casts
> is always scary ...
>
> Would it help to have some inline functions that do the range checking correctly?
> We have a couple of range helpers in pci.h, these could be moved out
> to range.h and we could add some more. As there act on u64 this will get
> the type limits mostly automatically right.

That seems to be the best solution, I get no warnings with this:

 static int omap_validate_imif_addr(struct omap_mpu_state_s *s,

I'll add range.h and respin the patches.

Comments

Michael S. Tsirkin Sept. 5, 2010, 10:35 a.m. UTC | #1
On Sun, Sep 05, 2010 at 09:44:01AM +0000, Blue Swirl wrote:
> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
> > On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
> >> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
> >> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
> >> >> In the unsigned number space, the checks can be merged into one,
> >> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
> >> >> could have:
> >> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
> >> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
> >> >>
> >> >> This would also implement the check that the writer of this code was
> >> >> trying to make.
> >> >> The important thing to note is however is that the check as it is now
> >> >> is not correct.
> >> >
> >> > I agree. But it seems to indicate a bigger problem.
> >> >
> >> > If we are trying to pass in a negative value, which is not one
> >> > of enum values, using BlkDebugEvent as type is just confusing,
> >> > we should just pass int instead.
> >>
> >> AFAICT it's only possible to use the values listed in event_names in
> >> blkdebug.c, other values are rejected. So the check should actually be
> >> an assert() or it could even be removed.
> >
> > Sounds good.
> >
> >> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
> >> >> >> check? Then if the value changes, the need to add the comparison back
> >> >> >> will be obvious.
> >> >> >
> >> >> > This would work but it's weird.  The thing is it's currently a correct
> >> >> > code and the check may be useless but it's the optimiser's task to
> >> >> > remove it, not ours.  The compiler is not able to tell whether the
> >> >> > check makes sense or nott, because the compiler only has access to
> >> >> > preprocessed code.  So why should you let the compiler have anything
> >> >> > to say on it.
> >> >>
> >> >> Good point. I'll try to invent something better.
> >> >
> >> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
> >>
> >> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
> >>
> >> I think the assertion is still the best way, it ensures that something
> >> will happen if OMAP_EMIFS_BASE changes. We could for example remove
> >> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
> >> adding a new define could still forget to adjust the check anyway.
> >
> > We could replace it with a macro
> > #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
> > but all this does look artificial. And of course using type casts
> > is always scary ...
> >
> > Would it help to have some inline functions that do the range checking correctly?
> > We have a couple of range helpers in pci.h, these could be moved out
> > to range.h and we could add some more. As there act on u64 this will get
> > the type limits mostly automatically right.
> 
> That seems to be the best solution, I get no warnings with this:
> 
> diff --git a/hw/omap1.c b/hw/omap1.c
> index b00f870..8bf88e7 100644
> --- a/hw/omap1.c
> +++ b/hw/omap1.c
> @@ -3672,14 +3672,25 @@ static int omap_validate_emiff_addr(struct
> omap_mpu_state_s *s,
>      return addr >= OMAP_EMIFF_BASE && addr < OMAP_EMIFF_BASE + s->sdram_size;
>  }
> 
> +/* Get last byte of a range from offset + length.
> + * Undefined for ranges that wrap around 0. */
> +static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
> +{
> +    return offset + len - 1;
> +}
> +
> +/* Check whether a given range covers a given byte. */
> +static inline int range_covers_byte(uint64_t offset, uint64_t len,
> +                                    uint64_t byte)
> +{
> +    return offset <= byte && byte <= range_get_last(offset, len);
> +}
> +
>  static int omap_validate_emifs_addr(struct omap_mpu_state_s *s,
>                  target_phys_addr_t addr)
>  {
> -    /* If OMAP_EMIFS_BASE ever becomes nonzero, adjust the check below
> -       to also include the lower bound check like
> -       addr >= OMAP_EMIFS_BASE && addr < OMAP_EMIFF_BASE */
> -    assert(OMAP_EMIFS_BASE == 0);
> -    return addr < OMAP_EMIFF_BASE;
> +    return range_covers_byte(OMAP_EMIFS_BASE,
> +                             OMAP_EMIFF_BASE - OMAP_EMIFS_BASE, addr);
>  }
> 
>  static int omap_validate_imif_addr(struct omap_mpu_state_s *s,
> 
> I'll add range.h and respin the patches.

BTW, maybe we want a variant of range_covers_byte that gets
first and after last byte values, but so far we could not
come up with good name for that function, and 1 after last
semantics might be confusing.

One small comment: these are currenly wrong if the range
wraps around to 0 - and there's a comment that says so there.
This was never a problem for pci, but it might be
if we are making them generic.
andrzej zaborowski Sept. 5, 2010, 3:26 p.m. UTC | #2
On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>> >> In the unsigned number space, the checks can be merged into one,
>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>> >> could have:
>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>> >>
>>> >> This would also implement the check that the writer of this code was
>>> >> trying to make.
>>> >> The important thing to note is however is that the check as it is now
>>> >> is not correct.

I agree, assuming that an enum can reach 0x80000000 different values,
perhaps the current code is not ideal.  Still I think calling it
"wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
calling patches that remove a warning a "fix", they are workarounds)

>>> >
>>> > I agree. But it seems to indicate a bigger problem.
>>> >
>>> > If we are trying to pass in a negative value, which is not one
>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>> > we should just pass int instead.
>>>
>>> AFAICT it's only possible to use the values listed in event_names in
>>> blkdebug.c, other values are rejected. So the check should actually be
>>> an assert() or it could even be removed.
>>
>> Sounds good.
>>
>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>> >> >> check? Then if the value changes, the need to add the comparison back
>>> >> >> will be obvious.
>>> >> >
>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>> >> > code and the check may be useless but it's the optimiser's task to
>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>> >> > check makes sense or nott, because the compiler only has access to
>>> >> > preprocessed code.  So why should you let the compiler have anything
>>> >> > to say on it.
>>> >>
>>> >> Good point. I'll try to invent something better.
>>> >
>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>
>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>
>>> I think the assertion is still the best way, it ensures that something
>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>> adding a new define could still forget to adjust the check anyway.
>>
>> We could replace it with a macro
>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>> but all this does look artificial. And of course using type casts
>> is always scary ...
>>
>> Would it help to have some inline functions that do the range checking correctly?
>> We have a couple of range helpers in pci.h, these could be moved out
>> to range.h and we could add some more. As there act on u64 this will get
>> the type limits mostly automatically right.
>
> That seems to be the best solution, I get no warnings with this:

While the resulting code is clean (just as the current code), I think
it really shows that this warning should not be enabled.  At this
point you find yourself working around your compiler and potentially
forcing other write some really strange code to work around the
problem caused by this.

There are many warnings that should not be enabled by default for this
reason (like the uninitialised variable warning) unless they are fixed
to be really intelligent (which is unlikely in this case).

Cheers
Blue Swirl Sept. 5, 2010, 4:15 p.m. UTC | #3
On Sun, Sep 5, 2010 at 3:26 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>>> >> In the unsigned number space, the checks can be merged into one,
>>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>>> >> could have:
>>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>> >>
>>>> >> This would also implement the check that the writer of this code was
>>>> >> trying to make.
>>>> >> The important thing to note is however is that the check as it is now
>>>> >> is not correct.
>
> I agree, assuming that an enum can reach 0x80000000 different values,
> perhaps the current code is not ideal.  Still I think calling it
> "wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
> calling patches that remove a warning a "fix", they are workarounds)

On what basis do you still claim that? I think I explained the problem
at detail. There is a bug. I have a fix for the bug. The fix is not a
workaround, except maybe for committee-induced stupidity which created
the enum signedness ambiguity in the first place.

>>>> > I agree. But it seems to indicate a bigger problem.
>>>> >
>>>> > If we are trying to pass in a negative value, which is not one
>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>> > we should just pass int instead.
>>>>
>>>> AFAICT it's only possible to use the values listed in event_names in
>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>> an assert() or it could even be removed.
>>>
>>> Sounds good.
>>>
>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>> >> >> will be obvious.
>>>> >> >
>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>> >> > check makes sense or nott, because the compiler only has access to
>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>> >> > to say on it.
>>>> >>
>>>> >> Good point. I'll try to invent something better.
>>>> >
>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>
>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>
>>>> I think the assertion is still the best way, it ensures that something
>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>> adding a new define could still forget to adjust the check anyway.
>>>
>>> We could replace it with a macro
>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>> but all this does look artificial. And of course using type casts
>>> is always scary ...
>>>
>>> Would it help to have some inline functions that do the range checking correctly?
>>> We have a couple of range helpers in pci.h, these could be moved out
>>> to range.h and we could add some more. As there act on u64 this will get
>>> the type limits mostly automatically right.
>>
>> That seems to be the best solution, I get no warnings with this:
>
> While the resulting code is clean (just as the current code), I think
> it really shows that this warning should not be enabled.  At this
> point you find yourself working around your compiler and potentially
> forcing other write some really strange code to work around the
> problem caused by this.

The warnings generated by -Wtype-limits are very useful, because with
it I have found several bugs in the code. Even the patches that are
not bugs fixes are cleanups, not 'some really strange code'. Please
take a look at the 15 piece patch set I sent last, the patches
identify the problems better than this one you are replying to. Which
ones do you still think are only workarounds? Please be more specific.

> There are many warnings that should not be enabled by default for this
> reason (like the uninitialised variable warning) unless they are fixed
> to be really intelligent (which is unlikely in this case).

Please review the latest set of patches and provide hard facts to
support your claims.
andrzej zaborowski Sept. 5, 2010, 5:02 p.m. UTC | #4
On 5 September 2010 18:15, Blue Swirl <blauwirbel@gmail.com> wrote:
> On Sun, Sep 5, 2010 at 3:26 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>> On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>>> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>>>> >> In the unsigned number space, the checks can be merged into one,
>>>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>>>> >> could have:
>>>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>> >>
>>>>> >> This would also implement the check that the writer of this code was
>>>>> >> trying to make.
>>>>> >> The important thing to note is however is that the check as it is now
>>>>> >> is not correct.
>>
>> I agree, assuming that an enum can reach 0x80000000 different values,
>> perhaps the current code is not ideal.  Still I think calling it
>> "wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
>> calling patches that remove a warning a "fix", they are workarounds)
>
> On what basis do you still claim that?

I wanted to ask the same question.  Without constants in the
definition, the values of an enum range from 0 to N-1.  You explained
that if the enum had INT_MAX different values, then the signedness of
the values would matter (but for it to be signed would require it to
have constants again, which is not the case for enumerations of types
of an event).  Can an enum even have INT_MAX values?  It for sure
can't have UINT_MAX values.  You failed to give an example value which
would make any difference in the result of the check.  Perhaps I'm
misunderstanding where you see the bug.

> I think I explained the problem
> at detail. There is a bug. I have a fix for the bug. The fix is not a
> workaround, except maybe for committee-induced stupidity which created
> the enum signedness ambiguity in the first place.
>
>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>> >
>>>>> > If we are trying to pass in a negative value, which is not one
>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>> > we should just pass int instead.
>>>>>
>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>> an assert() or it could even be removed.
>>>>
>>>> Sounds good.
>>>>
>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>> >> >> will be obvious.
>>>>> >> >
>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>> >> > to say on it.
>>>>> >>
>>>>> >> Good point. I'll try to invent something better.
>>>>> >
>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>
>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>
>>>>> I think the assertion is still the best way, it ensures that something
>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>> adding a new define could still forget to adjust the check anyway.
>>>>
>>>> We could replace it with a macro
>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>> but all this does look artificial. And of course using type casts
>>>> is always scary ...
>>>>
>>>> Would it help to have some inline functions that do the range checking correctly?
>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>> to range.h and we could add some more. As there act on u64 this will get
>>>> the type limits mostly automatically right.
>>>
>>> That seems to be the best solution, I get no warnings with this:
>>
>> While the resulting code is clean (just as the current code), I think
>> it really shows that this warning should not be enabled.  At this
>> point you find yourself working around your compiler and potentially
>> forcing other write some really strange code to work around the
>> problem caused by this.
>
> The warnings generated by -Wtype-limits are very useful, because with
> it I have found several bugs in the code.

Is that an argument for enabling a warning *by default*?  Looking at
any specific part of the code you'll find bugs. If you enable some
warning, it'll hint on a given subset of the places in the code, some
of which are bugs and some are false-positives.  Enable a different
warning and you get a different subset.  Grep for any given keyword or
constant and you get a different subset.

> Even the patches that are
> not bugs fixes are cleanups, not 'some really strange code'. Please
> take a look at the 15 piece patch set I sent last, the patches
> identify the problems better than this one you are replying to. Which
> ones do you still think are only workarounds? Please be more specific.

Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
with a different that achieves the exact same functionality for all
input values, what do they "fix"?   What is the scenario in which they
perform better.  Some of the new code has worse self-documenting
function after the change, some are actual clean-up.  The always-false
or always-true comparisons should be and are handled by the compiler
and it is a completely normal thing to write them.  Take for example
how KVM was compile-time disabled or enabled at one point, these were
all comparisons with a constant result.  I'm not sure if the warning
would have triggered because they were behind a static inline function
(do we want this to make a difference?  This is what forces the
'really strange code')

Cheers
andrzej zaborowski Sept. 5, 2010, 5:09 p.m. UTC | #5
On 5 September 2010 19:02, andrzej zaborowski <balrogg@gmail.com> wrote:
> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
> with a different that achieves the exact same functionality for all

Sorry, patch 11 is a fix (unrelated to the warning though).

Cheers
Blue Swirl Sept. 5, 2010, 7:16 p.m. UTC | #6
On Sun, Sep 5, 2010 at 5:02 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> On 5 September 2010 18:15, Blue Swirl <blauwirbel@gmail.com> wrote:
>> On Sun, Sep 5, 2010 at 3:26 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>>> On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>>>> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>>>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>>>>> >> In the unsigned number space, the checks can be merged into one,
>>>>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>>>>> >> could have:
>>>>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>> >>
>>>>>> >> This would also implement the check that the writer of this code was
>>>>>> >> trying to make.
>>>>>> >> The important thing to note is however is that the check as it is now
>>>>>> >> is not correct.
>>>
>>> I agree, assuming that an enum can reach 0x80000000 different values,
>>> perhaps the current code is not ideal.  Still I think calling it
>>> "wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
>>> calling patches that remove a warning a "fix", they are workarounds)
>>
>> On what basis do you still claim that?
>
> I wanted to ask the same question.  Without constants in the
> definition, the values of an enum range from 0 to N-1.  You explained
> that if the enum had INT_MAX different values, then the signedness of
> the values would matter

I never said anything about INT_MAX different values, you did.

> (but for it to be signed would require it to
> have constants again, which is not the case for enumerations of types
> of an event).  Can an enum even have INT_MAX values?  It for sure
> can't have UINT_MAX values.  You failed to give an example value which
> would make any difference in the result of the check.  Perhaps I'm
> misunderstanding where you see the bug.

Yes, please read the discussion again. Especially my message with the
example program.

>> I think I explained the problem
>> at detail. There is a bug. I have a fix for the bug. The fix is not a
>> workaround, except maybe for committee-induced stupidity which created
>> the enum signedness ambiguity in the first place.
>>
>>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>>> >
>>>>>> > If we are trying to pass in a negative value, which is not one
>>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>>> > we should just pass int instead.
>>>>>>
>>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>>> an assert() or it could even be removed.
>>>>>
>>>>> Sounds good.
>>>>>
>>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>>> >> >> will be obvious.
>>>>>> >> >
>>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>>> >> > to say on it.
>>>>>> >>
>>>>>> >> Good point. I'll try to invent something better.
>>>>>> >
>>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>>
>>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>>
>>>>>> I think the assertion is still the best way, it ensures that something
>>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>>> adding a new define could still forget to adjust the check anyway.
>>>>>
>>>>> We could replace it with a macro
>>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>>> but all this does look artificial. And of course using type casts
>>>>> is always scary ...
>>>>>
>>>>> Would it help to have some inline functions that do the range checking correctly?
>>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>>> to range.h and we could add some more. As there act on u64 this will get
>>>>> the type limits mostly automatically right.
>>>>
>>>> That seems to be the best solution, I get no warnings with this:
>>>
>>> While the resulting code is clean (just as the current code), I think
>>> it really shows that this warning should not be enabled.  At this
>>> point you find yourself working around your compiler and potentially
>>> forcing other write some really strange code to work around the
>>> problem caused by this.
>>
>> The warnings generated by -Wtype-limits are very useful, because with
>> it I have found several bugs in the code.
>
> Is that an argument for enabling a warning *by default*?  Looking at
> any specific part of the code you'll find bugs. If you enable some
> warning, it'll hint on a given subset of the places in the code, some
> of which are bugs and some are false-positives.  Enable a different
> warning and you get a different subset.  Grep for any given keyword or
> constant and you get a different subset.

Right, so when we enable *by default* the warning, buggy code (and
unfortunately the false positives, if any) will not be committed.

>> Even the patches that are
>> not bugs fixes are cleanups, not 'some really strange code'. Please
>> take a look at the 15 piece patch set I sent last, the patches
>> identify the problems better than this one you are replying to. Which
>> ones do you still think are only workarounds? Please be more specific.
>
> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
> with a different that achieves the exact same functionality for all
> input values, what do they "fix"?

5: refactoring, as noted in pci.h, the code does not belong there.
6: refactoring and cleanup using the range functions.
7: cleanup leftover code.
9: cleanup. We already had a hack in place because of mingw32
compiler, replace that with a cleaner approach.
11: bug fix.
14: cleanup. Hiding semicolons after comments is asking for trouble,
this is not obfuscated C contest. I already fixed a bug because of
that, remember?
15: cleanup, declarations belong to header files, not to .c files.

>  What is the scenario in which they
> perform better.

There is no change in performance.

> Some of the new code has worse self-documenting
> function after the change, some are actual clean-up.

Which ones? I really don't see what you are complaining about.

>  The always-false
> or always-true comparisons should be and are handled by the compiler
> and it is a completely normal thing to write them.  Take for example
> how KVM was compile-time disabled or enabled at one point, these were
> all comparisons with a constant result.  I'm not sure if the warning
> would have triggered because they were behind a static inline function
> (do we want this to make a difference?  This is what forces the
> 'really strange code')

Please identify clearly any 'really strange code' which the changes
introduce in your opinion. 14 actually removes some strange
obfuscations which can be demonstrated to have caused a bug.
andrzej zaborowski Sept. 5, 2010, 8:32 p.m. UTC | #7
On 5 September 2010 21:16, Blue Swirl <blauwirbel@gmail.com> wrote:
> On Sun, Sep 5, 2010 at 5:02 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>> On 5 September 2010 18:15, Blue Swirl <blauwirbel@gmail.com> wrote:
>>> On Sun, Sep 5, 2010 at 3:26 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>>>> On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>>>>> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>>>>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>>>>>> >> In the unsigned number space, the checks can be merged into one,
>>>>>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>>>>>> >> could have:
>>>>>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>>> >>
>>>>>>> >> This would also implement the check that the writer of this code was
>>>>>>> >> trying to make.
>>>>>>> >> The important thing to note is however is that the check as it is now
>>>>>>> >> is not correct.
>>>>
>>>> I agree, assuming that an enum can reach 0x80000000 different values,
>>>> perhaps the current code is not ideal.  Still I think calling it
>>>> "wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
>>>> calling patches that remove a warning a "fix", they are workarounds)
>>>
>>> On what basis do you still claim that?
>>
>> I wanted to ask the same question.  Without constants in the
>> definition, the values of an enum range from 0 to N-1.  You explained
>> that if the enum had INT_MAX different values, then the signedness of
>> the values would matter
>
> I never said anything about INT_MAX different values, you did.

You said a BLKDBG_EVENT_MAX >= 0x80000000 and that the enum has to be
signed, how will that happen?

>
>> (but for it to be signed would require it to
>> have constants again, which is not the case for enumerations of types
>> of an event).  Can an enum even have INT_MAX values?  It for sure
>> can't have UINT_MAX values.  You failed to give an example value which
>> would make any difference in the result of the check.  Perhaps I'm
>> misunderstanding where you see the bug.
>
> Yes, please read the discussion again. Especially my message with the
> example program.

I've re-read it and confirmed that you failed to show a scenario that
the check does not address.  I'm trying to understand why you call
this code buggy.

The line is if (event < 0 || event >= BLKDBG_EVENT_MAX) { and it
assures that an out-of-range value of "event" will not get used.

>
>>> I think I explained the problem
>>> at detail. There is a bug. I have a fix for the bug. The fix is not a
>>> workaround, except maybe for committee-induced stupidity which created
>>> the enum signedness ambiguity in the first place.
>>>
>>>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>>>> >
>>>>>>> > If we are trying to pass in a negative value, which is not one
>>>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>>>> > we should just pass int instead.
>>>>>>>
>>>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>>>> an assert() or it could even be removed.
>>>>>>
>>>>>> Sounds good.
>>>>>>
>>>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>>>> >> >> will be obvious.
>>>>>>> >> >
>>>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>>>> >> > to say on it.
>>>>>>> >>
>>>>>>> >> Good point. I'll try to invent something better.
>>>>>>> >
>>>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>>>
>>>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>>>
>>>>>>> I think the assertion is still the best way, it ensures that something
>>>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>>>> adding a new define could still forget to adjust the check anyway.
>>>>>>
>>>>>> We could replace it with a macro
>>>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>>>> but all this does look artificial. And of course using type casts
>>>>>> is always scary ...
>>>>>>
>>>>>> Would it help to have some inline functions that do the range checking correctly?
>>>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>>>> to range.h and we could add some more. As there act on u64 this will get
>>>>>> the type limits mostly automatically right.
>>>>>
>>>>> That seems to be the best solution, I get no warnings with this:
>>>>
>>>> While the resulting code is clean (just as the current code), I think
>>>> it really shows that this warning should not be enabled.  At this
>>>> point you find yourself working around your compiler and potentially
>>>> forcing other write some really strange code to work around the
>>>> problem caused by this.
>>>
>>> The warnings generated by -Wtype-limits are very useful, because with
>>> it I have found several bugs in the code.
>>
>> Is that an argument for enabling a warning *by default*?  Looking at
>> any specific part of the code you'll find bugs. If you enable some
>> warning, it'll hint on a given subset of the places in the code, some
>> of which are bugs and some are false-positives.  Enable a different
>> warning and you get a different subset.  Grep for any given keyword or
>> constant and you get a different subset.
>
> Right, so when we enable *by default* the warning, buggy code (and
> unfortunately the false positives, if any) will not be committed.

Ok, so "malloc causes memory leeks, let's forbid dynamic allocation", right?

>
>>> Even the patches that are
>>> not bugs fixes are cleanups, not 'some really strange code'. Please
>>> take a look at the 15 piece patch set I sent last, the patches
>>> identify the problems better than this one you are replying to. Which
>>> ones do you still think are only workarounds? Please be more specific.
>>
>> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
>> with a different that achieves the exact same functionality for all
>> input values, what do they "fix"?
>
> 5: refactoring, as noted in pci.h, the code does not belong there.
> 6: refactoring and cleanup using the range functions.
> 7: cleanup leftover code.
> 9: cleanup. We already had a hack in place because of mingw32
> compiler, replace that with a cleaner approach.
> 11: bug fix.
> 14: cleanup. Hiding semicolons after comments is asking for trouble,
> this is not obfuscated C contest.

If you're used to reading one code style, other styles look like IOCCC
to you, there's no hiding anything.

> 15: cleanup, declarations belong to header files, not to .c files.

So, skipping 11 (bugfix unrelated to the warnings), where are those
"fixes"?  What is the improvement in behaviour?

>
>>  The always-false
>> or always-true comparisons should be and are handled by the compiler
>> and it is a completely normal thing to write them.  Take for example
>> how KVM was compile-time disabled or enabled at one point, these were
>> all comparisons with a constant result.  I'm not sure if the warning
>> would have triggered because they were behind a static inline function
>> (do we want this to make a difference?  This is what forces the
>> 'really strange code')
>
> Please identify clearly any 'really strange code' which the changes
> introduce in your opinion. 14 actually removes some strange
> obfuscations which can be demonstrated to have caused a bug.

I didn't say you introduced any, I'm saying that you're trying to
force new code to be written with workarounds, such as forcing hiding
a constant value behind an inline function if it is to be used as a
condition.  Knowing C syntax and the qemu documentation will not be
enough, you'll have to fight the humours of the compiler.

Cheers
Blue Swirl Sept. 5, 2010, 9:44 p.m. UTC | #8
On Sun, Sep 5, 2010 at 8:32 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> On 5 September 2010 21:16, Blue Swirl <blauwirbel@gmail.com> wrote:
>> On Sun, Sep 5, 2010 at 5:02 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>>> On 5 September 2010 18:15, Blue Swirl <blauwirbel@gmail.com> wrote:
>>>> On Sun, Sep 5, 2010 at 3:26 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
>>>>> On 5 September 2010 11:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>>>>>> On Sun, Sep 5, 2010 at 9:26 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>>>> On Sun, Sep 05, 2010 at 09:06:10AM +0000, Blue Swirl wrote:
>>>>>>>> On Sun, Sep 5, 2010 at 7:54 AM, Michael S. Tsirkin <mst@redhat.com> wrote:
>>>>>>>> > On Sat, Sep 04, 2010 at 05:21:24PM +0000, Blue Swirl wrote:
>>>>>>>> >> In the unsigned number space, the checks can be merged into one,
>>>>>>>> >> assuming that BLKDBG_EVEN_MAX is less than INT_MAX. Alternatively we
>>>>>>>> >> could have:
>>>>>>>> >>  -    if (event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>>>> >>  +    if ((int)event < 0 || event >= BLKDBG_EVENT_MAX) {
>>>>>>>> >>
>>>>>>>> >> This would also implement the check that the writer of this code was
>>>>>>>> >> trying to make.
>>>>>>>> >> The important thing to note is however is that the check as it is now
>>>>>>>> >> is not correct.
>>>>>
>>>>> I agree, assuming that an enum can reach 0x80000000 different values,
>>>>> perhaps the current code is not ideal.  Still I think calling it
>>>>> "wrong" is wrong, and calling your patch a "fix" is wrong. (Same as
>>>>> calling patches that remove a warning a "fix", they are workarounds)
>>>>
>>>> On what basis do you still claim that?
>>>
>>> I wanted to ask the same question.  Without constants in the
>>> definition, the values of an enum range from 0 to N-1.  You explained
>>> that if the enum had INT_MAX different values, then the signedness of
>>> the values would matter
>>
>> I never said anything about INT_MAX different values, you did.
>
> You said a BLKDBG_EVENT_MAX >= 0x80000000 and that the enum has to be
> signed, how will that happen?

Please be more careful with your attributions. I did not say that either.

The problem case is when BLKDBG_EVENT_MAX > 0x80000000 and the type of
enum is unsigned. This can happen easily by

typedef enum {
    BLKDBG_EVENT_MAX = 0x80000001,
} BlkDebugEvent;

>>> (but for it to be signed would require it to
>>> have constants again, which is not the case for enumerations of types
>>> of an event).  Can an enum even have INT_MAX values?  It for sure
>>> can't have UINT_MAX values.  You failed to give an example value which
>>> would make any difference in the result of the check.  Perhaps I'm
>>> misunderstanding where you see the bug.
>>
>> Yes, please read the discussion again. Especially my message with the
>> example program.
>
> I've re-read it and confirmed that you failed to show a scenario that
> the check does not address.  I'm trying to understand why you call
> this code buggy.
>
> The line is if (event < 0 || event >= BLKDBG_EVENT_MAX) { and it
> assures that an out-of-range value of "event" will not get used.

The problem case is when BLKDBG_EVENT_MAX > 0x80000000 and the type of
enum is unsigned. Then the first check is ignored by the compiler and
the second does not catch values which are between 0x80000000 and
BLKDBG_EVENT_MAX. This may not be what was desired by the check,
though.

Those values will be caught with the int cast, or if the compiler
still happens to make the enum signed (for example, because
BLKDBG_EVENT_MAX was changed to a #define in order to support
compilers which don't allow too large enum values).

>>>> I think I explained the problem
>>>> at detail. There is a bug. I have a fix for the bug. The fix is not a
>>>> workaround, except maybe for committee-induced stupidity which created
>>>> the enum signedness ambiguity in the first place.
>>>>
>>>>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>>>>> >
>>>>>>>> > If we are trying to pass in a negative value, which is not one
>>>>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>>>>> > we should just pass int instead.
>>>>>>>>
>>>>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>>>>> an assert() or it could even be removed.
>>>>>>>
>>>>>>> Sounds good.
>>>>>>>
>>>>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>>>>> >> >> will be obvious.
>>>>>>>> >> >
>>>>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>>>>> >> > to say on it.
>>>>>>>> >>
>>>>>>>> >> Good point. I'll try to invent something better.
>>>>>>>> >
>>>>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>>>>
>>>>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>>>>
>>>>>>>> I think the assertion is still the best way, it ensures that something
>>>>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>>>>> adding a new define could still forget to adjust the check anyway.
>>>>>>>
>>>>>>> We could replace it with a macro
>>>>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>>>>> but all this does look artificial. And of course using type casts
>>>>>>> is always scary ...
>>>>>>>
>>>>>>> Would it help to have some inline functions that do the range checking correctly?
>>>>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>>>>> to range.h and we could add some more. As there act on u64 this will get
>>>>>>> the type limits mostly automatically right.
>>>>>>
>>>>>> That seems to be the best solution, I get no warnings with this:
>>>>>
>>>>> While the resulting code is clean (just as the current code), I think
>>>>> it really shows that this warning should not be enabled.  At this
>>>>> point you find yourself working around your compiler and potentially
>>>>> forcing other write some really strange code to work around the
>>>>> problem caused by this.
>>>>
>>>> The warnings generated by -Wtype-limits are very useful, because with
>>>> it I have found several bugs in the code.
>>>
>>> Is that an argument for enabling a warning *by default*?  Looking at
>>> any specific part of the code you'll find bugs. If you enable some
>>> warning, it'll hint on a given subset of the places in the code, some
>>> of which are bugs and some are false-positives.  Enable a different
>>> warning and you get a different subset.  Grep for any given keyword or
>>> constant and you get a different subset.
>>
>> Right, so when we enable *by default* the warning, buggy code (and
>> unfortunately the false positives, if any) will not be committed.
>
> Ok, so "malloc causes memory leeks, let's forbid dynamic allocation", right?

The questionable malloc policies of your employer have nothing to do
with this. If you don't agree with them, you can argue for a change in
the rules or seek employment in a company without those rules.

>>>> Even the patches that are
>>>> not bugs fixes are cleanups, not 'some really strange code'. Please
>>>> take a look at the 15 piece patch set I sent last, the patches
>>>> identify the problems better than this one you are replying to. Which
>>>> ones do you still think are only workarounds? Please be more specific.
>>>
>>> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
>>> with a different that achieves the exact same functionality for all
>>> input values, what do they "fix"?
>>
>> 5: refactoring, as noted in pci.h, the code does not belong there.
>> 6: refactoring and cleanup using the range functions.
>> 7: cleanup leftover code.
>> 9: cleanup. We already had a hack in place because of mingw32
>> compiler, replace that with a cleaner approach.
>> 11: bug fix.
>> 14: cleanup. Hiding semicolons after comments is asking for trouble,
>> this is not obfuscated C contest.
>
> If you're used to reading one code style, other styles look like IOCCC
> to you

Faulty generalization.

>, there's no hiding anything.

Even if you didn't intend to hide anything, putting the semicolons
immediately after comments certainly makes them less visible. I can't
see what could ever be the benefit of doing that, for example putting
the semicolon on a separate line would make much better sense to me.

>> 15: cleanup, declarations belong to header files, not to .c files.
>
> So, skipping 11 (bugfix unrelated to the warnings), where are those
> "fixes"?  What is the improvement in behaviour?

I did not claim all of the changes are bug fixes (again, please try be
more careful), some of them are cleanups. It's OK to also clean up
code, not only fix bugs.

>>>  The always-false
>>> or always-true comparisons should be and are handled by the compiler
>>> and it is a completely normal thing to write them.  Take for example
>>> how KVM was compile-time disabled or enabled at one point, these were
>>> all comparisons with a constant result.  I'm not sure if the warning
>>> would have triggered because they were behind a static inline function
>>> (do we want this to make a difference?  This is what forces the
>>> 'really strange code')
>>
>> Please identify clearly any 'really strange code' which the changes
>> introduce in your opinion. 14 actually removes some strange
>> obfuscations which can be demonstrated to have caused a bug.

I must admit that #6 was not written as clearly as it should have.
It's easy to fix though.

> I didn't say you introduced any, I'm saying that you're trying to
> force new code to be written with workarounds, such as forcing hiding
> a constant value behind an inline function if it is to be used as a
> condition.  Knowing C syntax and the qemu documentation will not be
> enough, you'll have to fight the humours of the compiler.

Considering patches #1 to #12 (-Wtype-limits), 7 of those (1 to 4, 8,
10 and 11) fix bugs. #5 is unrelated change and #12 only changes the
flags. Even if the remaining three were considered to exist only to
please the compiler (ignoring the cleanup value), I think the ratio of
those changes to bug fixes still justifies adding the flag to catch
future bugs.
andrzej zaborowski Sept. 5, 2010, 10:33 p.m. UTC | #9
On 5 September 2010 23:44, Blue Swirl <blauwirbel@gmail.com> wrote:
> The problem case is when BLKDBG_EVENT_MAX > 0x80000000 and the type of
> enum is unsigned. Then the first check is ignored by the compiler and
> the second does not catch values which are between 0x80000000 and
> BLKDBG_EVENT_MAX. This may not be what was desired by the check,
> though.
>
> Those values will be caught with the int cast, or if the compiler
> still happens to make the enum signed (for example, because
> BLKDBG_EVENT_MAX was changed to a #define in order to support
> compilers which don't allow too large enum values).

So you're actually talking about INT_MAX + 1, not 0x80000000, the
number depends on the abi.

Quite clearly BLKDBG_EVENT_MAX is the max value in the enum so that
the values can be used as indices of an array of a known size.  I
think it's safe to say it is < INT_MAX.

>
>>>>> I think I explained the problem
>>>>> at detail. There is a bug. I have a fix for the bug. The fix is not a
>>>>> workaround, except maybe for committee-induced stupidity which created
>>>>> the enum signedness ambiguity in the first place.
>>>>>
>>>>>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>>>>>> >
>>>>>>>>> > If we are trying to pass in a negative value, which is not one
>>>>>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>>>>>> > we should just pass int instead.
>>>>>>>>>
>>>>>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>>>>>> an assert() or it could even be removed.
>>>>>>>>
>>>>>>>> Sounds good.
>>>>>>>>
>>>>>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>>>>>> >> >> will be obvious.
>>>>>>>>> >> >
>>>>>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>>>>>> >> > to say on it.
>>>>>>>>> >>
>>>>>>>>> >> Good point. I'll try to invent something better.
>>>>>>>>> >
>>>>>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>>>>>
>>>>>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>>>>>
>>>>>>>>> I think the assertion is still the best way, it ensures that something
>>>>>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>>>>>> adding a new define could still forget to adjust the check anyway.
>>>>>>>>
>>>>>>>> We could replace it with a macro
>>>>>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>>>>>> but all this does look artificial. And of course using type casts
>>>>>>>> is always scary ...
>>>>>>>>
>>>>>>>> Would it help to have some inline functions that do the range checking correctly?
>>>>>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>>>>>> to range.h and we could add some more. As there act on u64 this will get
>>>>>>>> the type limits mostly automatically right.
>>>>>>>
>>>>>>> That seems to be the best solution, I get no warnings with this:
>>>>>>
>>>>>> While the resulting code is clean (just as the current code), I think
>>>>>> it really shows that this warning should not be enabled.  At this
>>>>>> point you find yourself working around your compiler and potentially
>>>>>> forcing other write some really strange code to work around the
>>>>>> problem caused by this.
>>>>>
>>>>> The warnings generated by -Wtype-limits are very useful, because with
>>>>> it I have found several bugs in the code.
>>>>
>>>> Is that an argument for enabling a warning *by default*?  Looking at
>>>> any specific part of the code you'll find bugs. If you enable some
>>>> warning, it'll hint on a given subset of the places in the code, some
>>>> of which are bugs and some are false-positives.  Enable a different
>>>> warning and you get a different subset.  Grep for any given keyword or
>>>> constant and you get a different subset.
>>>
>>> Right, so when we enable *by default* the warning, buggy code (and
>>> unfortunately the false positives, if any) will not be committed.
>>
>> Ok, so "malloc causes memory leeks, let's forbid dynamic allocation", right?
>
> The questionable malloc policies of your employer have nothing to do
> with this. If you don't agree with them, you can argue for a change in
> the rules or seek employment in a company without those rules.

First, the policy is almost identical to the policy you're introducing
so it has everything to do with this.  I'm pointing out what is an
actual faulty generalisation.  Avoiding malloc or avoiding strcat or
avoiding "if (0 <= 1)" is unlikely to reduce the number of bugs, quite
the opposite.  It's identical to arguing against file sharing on the
internet because illegal file sharing is possible, it's a faulty
generalisation. (Criminals use cars => cars are evil)  See your
statement above about buggy code not being committed.  (I've never
been employed by Nokia.  But I know people who wanted to submit
improvements to gpsd, which is the project that originally had this
policy.)

>
>>>>> Even the patches that are
>>>>> not bugs fixes are cleanups, not 'some really strange code'. Please
>>>>> take a look at the 15 piece patch set I sent last, the patches
>>>>> identify the problems better than this one you are replying to. Which
>>>>> ones do you still think are only workarounds? Please be more specific.
>>>>
>>>> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
>>>> with a different that achieves the exact same functionality for all
>>>> input values, what do they "fix"?
>>>
>>> 5: refactoring, as noted in pci.h, the code does not belong there.
>>> 6: refactoring and cleanup using the range functions.
>>> 7: cleanup leftover code.
>>> 9: cleanup. We already had a hack in place because of mingw32
>>> compiler, replace that with a cleaner approach.
>>> 11: bug fix.
>>> 14: cleanup. Hiding semicolons after comments is asking for trouble,
>>> this is not obfuscated C contest.
>>
>> If you're used to reading one code style, other styles look like IOCCC
>> to you
>
> Faulty generalization.
>
>>, there's no hiding anything.
>
> Even if you didn't intend to hide anything, putting the semicolons
> immediately after comments certainly makes them less visible. I can't
> see what could ever be the benefit of doing that, for example putting
> the semicolon on a separate line would make much better sense to me.

They're exactly equally good.

>
>>> 15: cleanup, declarations belong to header files, not to .c files.
>>
>> So, skipping 11 (bugfix unrelated to the warnings), where are those
>> "fixes"?  What is the improvement in behaviour?
>
> I did not claim all of the changes are bug fixes (again, please try be
> more careful), some of them are cleanups.

You asked which ones I thought were workarounds.  If you have to make
an effort to satisfy the compiler warnings code then you have to
re-think what the warnings are for.  It really seems you want them
enabled just because they exist.

Cheers
Blue Swirl Sept. 6, 2010, 7:08 p.m. UTC | #10
On Sun, Sep 5, 2010 at 10:33 PM, andrzej zaborowski <balrogg@gmail.com> wrote:
> On 5 September 2010 23:44, Blue Swirl <blauwirbel@gmail.com> wrote:
>> The problem case is when BLKDBG_EVENT_MAX > 0x80000000 and the type of
>> enum is unsigned. Then the first check is ignored by the compiler and
>> the second does not catch values which are between 0x80000000 and
>> BLKDBG_EVENT_MAX. This may not be what was desired by the check,
>> though.
>>
>> Those values will be caught with the int cast, or if the compiler
>> still happens to make the enum signed (for example, because
>> BLKDBG_EVENT_MAX was changed to a #define in order to support
>> compilers which don't allow too large enum values).
>
> So you're actually talking about INT_MAX + 1, not 0x80000000, the
> number depends on the abi.
>
> Quite clearly BLKDBG_EVENT_MAX is the max value in the enum so that
> the values can be used as indices of an array of a known size.  I
> think it's safe to say it is < INT_MAX.
>
>>
>>>>>> I think I explained the problem
>>>>>> at detail. There is a bug. I have a fix for the bug. The fix is not a
>>>>>> workaround, except maybe for committee-induced stupidity which created
>>>>>> the enum signedness ambiguity in the first place.
>>>>>>
>>>>>>>>>> > I agree. But it seems to indicate a bigger problem.
>>>>>>>>>> >
>>>>>>>>>> > If we are trying to pass in a negative value, which is not one
>>>>>>>>>> > of enum values, using BlkDebugEvent as type is just confusing,
>>>>>>>>>> > we should just pass int instead.
>>>>>>>>>>
>>>>>>>>>> AFAICT it's only possible to use the values listed in event_names in
>>>>>>>>>> blkdebug.c, other values are rejected. So the check should actually be
>>>>>>>>>> an assert() or it could even be removed.
>>>>>>>>>
>>>>>>>>> Sounds good.
>>>>>>>>>
>>>>>>>>>> >> >> How about adding assert(OMAP_EMIFS_BASE == 0) and commenting out the
>>>>>>>>>> >> >> check? Then if the value changes, the need to add the comparison back
>>>>>>>>>> >> >> will be obvious.
>>>>>>>>>> >> >
>>>>>>>>>> >> > This would work but it's weird.  The thing is it's currently a correct
>>>>>>>>>> >> > code and the check may be useless but it's the optimiser's task to
>>>>>>>>>> >> > remove it, not ours.  The compiler is not able to tell whether the
>>>>>>>>>> >> > check makes sense or nott, because the compiler only has access to
>>>>>>>>>> >> > preprocessed code.  So why should you let the compiler have anything
>>>>>>>>>> >> > to say on it.
>>>>>>>>>> >>
>>>>>>>>>> >> Good point. I'll try to invent something better.
>>>>>>>>>> >
>>>>>>>>>> > Use #pragma to supress the warning? Maybe we could wrap this in a macro ..
>>>>>>>>>>
>>>>>>>>>> Those lines may also desynch silently with changes to OMAP_EMIFS_BASE.
>>>>>>>>>>
>>>>>>>>>> I think the assertion is still the best way, it ensures that something
>>>>>>>>>> will happen if OMAP_EMIFS_BASE changes. We could for example remove
>>>>>>>>>> OMAP_EMIFS_BASE entirely (it's only used for the check), but someone
>>>>>>>>>> adding a new define could still forget to adjust the check anyway.
>>>>>>>>>
>>>>>>>>> We could replace it with a macro
>>>>>>>>> #define OMAP_EMIFS_VALID(addr) ((target_phys_addr_t)addr < OMAP_EMIFF_BASE)
>>>>>>>>> but all this does look artificial. And of course using type casts
>>>>>>>>> is always scary ...
>>>>>>>>>
>>>>>>>>> Would it help to have some inline functions that do the range checking correctly?
>>>>>>>>> We have a couple of range helpers in pci.h, these could be moved out
>>>>>>>>> to range.h and we could add some more. As there act on u64 this will get
>>>>>>>>> the type limits mostly automatically right.
>>>>>>>>
>>>>>>>> That seems to be the best solution, I get no warnings with this:
>>>>>>>
>>>>>>> While the resulting code is clean (just as the current code), I think
>>>>>>> it really shows that this warning should not be enabled.  At this
>>>>>>> point you find yourself working around your compiler and potentially
>>>>>>> forcing other write some really strange code to work around the
>>>>>>> problem caused by this.
>>>>>>
>>>>>> The warnings generated by -Wtype-limits are very useful, because with
>>>>>> it I have found several bugs in the code.
>>>>>
>>>>> Is that an argument for enabling a warning *by default*?  Looking at
>>>>> any specific part of the code you'll find bugs. If you enable some
>>>>> warning, it'll hint on a given subset of the places in the code, some
>>>>> of which are bugs and some are false-positives.  Enable a different
>>>>> warning and you get a different subset.  Grep for any given keyword or
>>>>> constant and you get a different subset.
>>>>
>>>> Right, so when we enable *by default* the warning, buggy code (and
>>>> unfortunately the false positives, if any) will not be committed.
>>>
>>> Ok, so "malloc causes memory leeks, let's forbid dynamic allocation", right?
>>
>> The questionable malloc policies of your employer have nothing to do
>> with this. If you don't agree with them, you can argue for a change in
>> the rules or seek employment in a company without those rules.
>
> First, the policy is almost identical to the policy you're introducing
> so it has everything to do with this.  I'm pointing out what is an
> actual faulty generalisation.  Avoiding malloc or avoiding strcat or
> avoiding "if (0 <= 1)" is unlikely to reduce the number of bugs, quite
> the opposite.  It's identical to arguing against file sharing on the
> internet because illegal file sharing is possible, it's a faulty
> generalisation. (Criminals use cars => cars are evil)  See your
> statement above about buggy code not being committed.  (I've never
> been employed by Nokia.  But I know people who wanted to submit
> improvements to gpsd, which is the project that originally had this
> policy.)

I agree that the policy of using static allocations instead of using
malloc() will very likely generate more bugs, because people are not
used to think that way (uncommon policy) and the gap between the two
methods is not trivial. The cost is therefore big and benefit low.

Using pstrcat instead of strcat is much less of a problem: it's not
uncommon to avoid functions which may cause buffer overflows,
especially when writing hardened software and the conversion is often
trivial. The cost is then low but the benefit big.

I can't imagine how the conversions from avoiding -Wtype-limits
warnings can cause bugs. I then assume the cost is low but in any case
the benefit can be demonstrated to be big, see the bug fixes.

>>>>>> Even the patches that are
>>>>>> not bugs fixes are cleanups, not 'some really strange code'. Please
>>>>>> take a look at the 15 piece patch set I sent last, the patches
>>>>>> identify the problems better than this one you are replying to. Which
>>>>>> ones do you still think are only workarounds? Please be more specific.
>>>>>
>>>>> Patches 05, 06, 07, 09, 11, 14, 15 all replace one version of the code
>>>>> with a different that achieves the exact same functionality for all
>>>>> input values, what do they "fix"?
>>>>
>>>> 5: refactoring, as noted in pci.h, the code does not belong there.
>>>> 6: refactoring and cleanup using the range functions.
>>>> 7: cleanup leftover code.
>>>> 9: cleanup. We already had a hack in place because of mingw32
>>>> compiler, replace that with a cleaner approach.
>>>> 11: bug fix.
>>>> 14: cleanup. Hiding semicolons after comments is asking for trouble,
>>>> this is not obfuscated C contest.
>>>
>>> If you're used to reading one code style, other styles look like IOCCC
>>> to you
>>
>> Faulty generalization.
>>
>>>, there's no hiding anything.
>>
>> Even if you didn't intend to hide anything, putting the semicolons
>> immediately after comments certainly makes them less visible. I can't
>> see what could ever be the benefit of doing that, for example putting
>> the semicolon on a separate line would make much better sense to me.
>
> They're exactly equally good.

They are syntactically the same, but a human looking at the code will
have more problems when the semicolon is after the comment block.

But thank you, this amounts to you acking the separation of comments
and semicolons since either version is exactly equally good to you.

>>>> 15: cleanup, declarations belong to header files, not to .c files.
>>>
>>> So, skipping 11 (bugfix unrelated to the warnings), where are those
>>> "fixes"?  What is the improvement in behaviour?
>>
>> I did not claim all of the changes are bug fixes (again, please try be
>> more careful), some of them are cleanups.
>
> You asked which ones I thought were workarounds.  If you have to make
> an effort to satisfy the compiler warnings code then you have to
> re-think what the warnings are for.

To find bugs? Remember the bug fix to non bug fix ratio, which you
conveniently deleted from the discussion?

>  It really seems you want them
> enabled just because they exist.

I agree that this applies somewhat to #13, since the flags didn't
catch any bugs now. In my wishful thinking they may catch some in the
future. If any of them causes problems, it can be deleted without much
regret because we haven't seen the benefit.

I want the other flags enabled because they contribute positively to
the code quality.

It seems to me that you don't want to improve the quality of code but
want to stay in your comfort zone regardless of any improvement.
diff mbox

Patch

diff --git a/hw/omap1.c b/hw/omap1.c
index b00f870..8bf88e7 100644
--- a/hw/omap1.c
+++ b/hw/omap1.c
@@ -3672,14 +3672,25 @@  static int omap_validate_emiff_addr(struct
omap_mpu_state_s *s,
     return addr >= OMAP_EMIFF_BASE && addr < OMAP_EMIFF_BASE + s->sdram_size;
 }

+/* Get last byte of a range from offset + length.
+ * Undefined for ranges that wrap around 0. */
+static inline uint64_t range_get_last(uint64_t offset, uint64_t len)
+{
+    return offset + len - 1;
+}
+
+/* Check whether a given range covers a given byte. */
+static inline int range_covers_byte(uint64_t offset, uint64_t len,
+                                    uint64_t byte)
+{
+    return offset <= byte && byte <= range_get_last(offset, len);
+}
+
 static int omap_validate_emifs_addr(struct omap_mpu_state_s *s,
                 target_phys_addr_t addr)
 {
-    /* If OMAP_EMIFS_BASE ever becomes nonzero, adjust the check below
-       to also include the lower bound check like
-       addr >= OMAP_EMIFS_BASE && addr < OMAP_EMIFF_BASE */
-    assert(OMAP_EMIFS_BASE == 0);
-    return addr < OMAP_EMIFF_BASE;
+    return range_covers_byte(OMAP_EMIFS_BASE,
+                             OMAP_EMIFF_BASE - OMAP_EMIFS_BASE, addr);
 }