[cfe-dev] Static analyzer: add smart pointer checker

David Blaikie dblaikie at gmail.com
Fri Jan 6 13:06:06 PST 2012


The direct initialization case should be easily caught, and that should
be the majority case. But more interesting cases where the pointer in
question comes from another API or any other complicated logic might
need analysis to see whether the pointer ultimately originates from a
new[] call.

(honestly I'd love to see a make_unique added to the standard (for
convenience and safety (see any article entitled 'always name your
smart pointers' for examples)) and then consider flagging any use of
direct new (in a style checker, not in the compiler) and/or potentially
warning about any use of the raw pointer shared/unique ptr constructors)
From: Ted Kremenek
Sent: 1/6/2012 11:00 AM
To: Dmitri Gribenko
Cc: cfe-dev at cs.uiuc.edu Developers
Subject: Re: [cfe-dev] Static analyzer: add smart pointer checker
I should add that this seems useful and would have a low (possibly 0%)
false positive rate.  If it is always correct, it seems really worth
doing as a compiler warning (with a FixIt) if that isn't unappealing
to people.

On Jan 6, 2012, at 12:58 PM, Ted Kremenek wrote:

> Seems fairly straightforward and wouldn't require much analysis.  If this is specific to 'std', we could consider making this a compiler warning.  I'm fine with trying this out as a static analyzer check first.
>
> On Jan 6, 2012, at 7:40 AM, Dmitri Gribenko wrote:
>
>> Hello,
>>
>> I want to implement a smart pointer checker (std::shared_ptr,
>> std::unique) that catches specifying invalid type:
>>
>> struct A
>> {
>> A() { cout << "A()" << endl; }
>> ~A() { cout << "~A()" << endl; }
>> };
>>
>> void test()
>> {
>> std::unique_ptr<A[]> p(new A[3]); // good, delete[] called
>> std::unique_ptr<A> p(new A[3]); // bad, delete instead of delete[] called
>> }
>>
>> Any comments?
>>
>> Dmitri Gribenko
>>
>> --
>> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
>> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>

_______________________________________________
cfe-dev mailing list
cfe-dev at cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev



More information about the cfe-dev mailing list