RE: [PATCH v2 02/10] netfilter: Avoid assigning 'const' pointer to non-const pointer

From: David Laight
Date: Mon Jan 27 2020 - 07:04:45 EST


From: Nick Desaulniers
> Sent: 24 January 2020 17:20
...
> > > Good thing it's the variable being modified was not declared const; I
> > > get spooked when I see -Wdiscarded-qualifiers because of Section
> > > 6.7.3.6 of the ISO C11 draft spec:
> > >
> > > ```
> > > If an attempt is made to modify an object defined with a const-qualified
> > > type through use of an lvalue with non-const-qualified type,
> > > the behavior is undefined.

Well some old systems had small integer constants at fixes addresses.
So 'const int one = 1;' would be a reference to the global constant.
An assignment like '*(int *)&one = 2;' would change the value of the
system-wide 'one' constant'.

Pretty much 'undefined'.

But no excuse for the compiler just discarding the code.

I suspect that the code to remove 'const' needs to 'launder' the value
through a suitable integer type.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)