[PATCH] avoid Possible null pointer dereference in linguistic/source/spelldsp.hxx

Riccardo Magliocchetti riccardo.magliocchetti at gmail.com
Sat Feb 11 07:55:29 PST 2012


Hello,

Il 11/02/2012 16:21, julien2412 ha scritto:
> Hello,
>
> Cppcheck reports this :
> [spelldsp.hxx:138]: (error) Possible null pointer dereference: pCache -
> otherwise it is redundant to check if pCache is null at line 136
>
> Here are the lines :
>      134 inline linguistic::SpellCache&  SpellCheckerDispatcher::GetCache()
> const
>      135 {
>      136     if (!pCache)
>      137         ((SpellCheckerDispatcher *) this)->pCache = new
> linguistic::SpellCache();
>      138     return *pCache;
>      139 }
>
> Here is a simple (naive ?) patch
>
> diff --git a/linguistic/source/spelldsp.hxx b/linguistic/source/spelldsp.hxx
> index 9ae9cd4..34ac28f 100644
> --- a/linguistic/source/spelldsp.hxx
> +++ b/linguistic/source/spelldsp.hxx
> @@ -134,8 +134,11 @@ public:
>   inline linguistic::SpellCache&  SpellCheckerDispatcher::GetCache() const
>   {
>       if (!pCache)
> +    {
>           ((SpellCheckerDispatcher *) this)->pCache = new
> linguistic::SpellCache();
> -    return *pCache;
> +        return *pCache;
> +    }
> +    return NULL;
>
> Is it ok ?

It looks wrong to me, i'm really not into C++ but it seems 
((SpellCheckerDispatcher *) this)->pCache should be the same pCache you 
are dereferencing later. In any case you should not return NULL if 
pCache is something else, no?

What about this?

if (pCache)
    return *pCache;

return ((SpellCheckerDispatcher *) this)->pCache = new 
linguistic::SpellCache();

cheers,

-- 
Riccardo Magliocchetti


More information about the LibreOffice mailing list