[cfe-dev] [RFC][clang-tidy] New check for widening pointer cast

Daniel Marjamäki via cfe-dev cfe-dev at lists.llvm.org
Tue Mar 15 05:45:02 PDT 2016


Hello!

I am working on a new check for clang-tidy.

It will warn about dangerous casts when there is a pointer cast and the result type is larger than the source data.

A typical case where I think a warning is OK:

    struct A {
        int A;
    };

    struct AB {
        int A;
        int B;
    };

    void warn1() {
        struct A A1;
        struct AB *AB1 = (struct AB *)&A1;  // <- WARN
    }

There is no UB in that specific code as far as I know. But imho it's dangerous.

My work-in-progress code can be seen in the attached patch.

Initially I only warn about record types but maybe simple types (short,int,..) can be checked also.

Best regards,
Daniel Marjamäki

..................................................................................................................
Daniel Marjamäki Senior Engineer
Evidente ES East AB  Warfvinges väg 34  SE-112 51 Stockholm  Sweden

Mobile:                 +46 (0)709 12 42 62
E-mail:                 Daniel.Marjamaki at evidente.se

www.evidente.se
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 160315-widening-pointer-cast.diff
Type: text/x-patch
Size: 6757 bytes
Desc: 160315-widening-pointer-cast.diff
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160315/c8f8c9a2/attachment.bin>


More information about the cfe-dev mailing list