[Development] Compiler warnings

Kurt Pattyn pattyn.kurt at gmail.com
Fri Oct 17 20:14:51 CEST 2014





> On 17 Oct 2014, at 19:26, Mathias Hasselmann <mathias at taschenorakel.de> wrote:
> 
> 
> 
>> Am 17.10.2014 um 18:31 schrieb Thiago Macieira:
>>> On Friday 17 October 2014 13:06:39 Milian Wolff wrote:
>>> enum Foo {
>>> Bar = 1, Baz = 2
>>> };
>>> 
>>> Foo foo = static_cast<Foo>(3);
>>> 
>>> Now what do you do without a default clause?
>> 
>> Shoot the developer who abused the API.
>> 
>> If the function accepts enum values 1 and 2 and you pass a 3, you deserve the
>> undefined behaviour.
>> 
>> That is the same as passing a bool that doesn't contain exactly values 0 or 1
>> or passing an uninitialised pointer.
>> 
>> We have to handle all regular conditions. We don't have to guard against
>> stupidity.
>> 
> 
> I don't think this example has to do with stupidity. To me this just 
> seems like a minimal example of typical integration problems that happen 
> in real world. Far fetching I'd talk about distributed services, reading 
> files written by a different program version, and such.
Indeed this scenario immediately came to my mind also. And this doesn't need to be an intentional 'rape' of the API. A simple bug could lead to this situation.
Foo foo = static_cast<Foo>(someValueComingFromAJsonObject);

We encountered this particular problem when implementing some X-Ray driving logic. The networked 3rd party client was in a particular bad state (was work in progress) which obliged us to take extreme defensive measures (which is normal for that kind of application).
However, it is not up to Qt to be paranoid. Considering the above example: we would never ever pass that cast integer to Qt (after all Qt is marked as SOUP).

> Also Qt already 
> deals with such issues where it is obvious:
> 
>     const char * QVariant::typeToName(int typeId) [static]
>     Converts the int representation of the storage type, typeId, to its
>     string representation.
> 
>     Returns a null pointer if the type is QVariant::Invalid(obsolete)
>     or doesn't exist.
> _______________________________________________
> Development mailing list
> Development at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/development



More information about the Development mailing list