1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/
#ifndef DESTR_HXX
#define DESTR_HXX

#include "prim.hxx"


namespace cppu
{

//##################################################################################################
//#### destruction #################################################################################
//##################################################################################################

//--------------------------------------------------------------------------------------------------
inline void _destructUnion(
    void * pValue,
    typelib_TypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
    SAL_THROW(())
{
    typelib_TypeDescriptionReference * pType = _unionGetSetType( pValue, pTypeDescr );
    ::uno_type_destructData(
        (char *)pValue + ((typelib_UnionTypeDescription *)pTypeDescr)->nValueOffset,
        pType, release );
    ::typelib_typedescriptionreference_release( pType );
}
//==================================================================================================
void destructStruct(
    void * pValue,
    typelib_CompoundTypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
    SAL_THROW(());
//--------------------------------------------------------------------------------------------------
inline void _destructStruct(
    void * pValue,
    typelib_CompoundTypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
    SAL_THROW(())
{
    if (pTypeDescr->pBaseTypeDescription)
    {
        destructStruct( pValue, pTypeDescr->pBaseTypeDescription, release );
    }

    typelib_TypeDescriptionReference ** ppTypeRefs = pTypeDescr->ppTypeRefs;
    sal_Int32 * pMemberOffsets = pTypeDescr->pMemberOffsets;
    sal_Int32 nDescr = pTypeDescr->nMembers;
    while (nDescr--)
    {
        ::uno_type_destructData(
            (char *)pValue + pMemberOffsets[nDescr],
            ppTypeRefs[nDescr], release );
    }
}

//--------------------------------------------------------------------------------------------------
inline void _destructArray(
    void * pValue,
    typelib_ArrayTypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
    throw ()
{
    typelib_TypeDescription * pElementType = NULL;
    TYPELIB_DANGER_GET( &pElementType, ((typelib_IndirectTypeDescription *)pTypeDescr)->pType );
    sal_Int32 nElementSize = pElementType->nSize;<--- Possible null pointer dereference: pElementType
    TYPELIB_DANGER_RELEASE( pElementType );

    sal_Int32 nTotalElements = pTypeDescr->nTotalElements;
    for(sal_Int32 i=0; i < nTotalElements; i++)
    {
        ::uno_type_destructData(
            (sal_Char *)pValue + i * nElementSize,
            ((typelib_IndirectTypeDescription *)pTypeDescr)->pType, release );
    }

    typelib_typedescriptionreference_release(((typelib_IndirectTypeDescription *)pTypeDescr)->pType);
}

//==============================================================================
void destructSequence(
    uno_Sequence * pSequence,
    typelib_TypeDescriptionReference * pType,
    typelib_TypeDescription * pTypeDescr,
    uno_ReleaseFunc release );

//--------------------------------------------------------------------------------------------------
inline void _destructAny(
    uno_Any * pAny,
    uno_ReleaseFunc release )
    SAL_THROW(())
{
    typelib_TypeDescriptionReference * pType = pAny->pType;

    switch (pType->eTypeClass)
    {
    case typelib_TypeClass_HYPER:
    case typelib_TypeClass_UNSIGNED_HYPER:
        if (sizeof(void *) < sizeof(sal_Int64))
        {
            ::rtl_freeMemory( pAny->pData );
        }
        break;
    case typelib_TypeClass_FLOAT:
        if (sizeof(void *) < sizeof(float))
        {
            ::rtl_freeMemory( pAny->pData );
        }
        break;
    case typelib_TypeClass_DOUBLE:
        if (sizeof(void *) < sizeof(double))
        {
            ::rtl_freeMemory( pAny->pData );
        }
        break;
    case typelib_TypeClass_STRING:
        ::rtl_uString_release( (rtl_uString *)pAny->pReserved );
        break;
    case typelib_TypeClass_TYPE:
        ::typelib_typedescriptionreference_release(
            (typelib_TypeDescriptionReference *)pAny->pReserved );
        break;
    case typelib_TypeClass_ANY:
        OSL_FAIL( "### unexpected nested any!" );
        ::uno_any_destruct( (uno_Any *)pAny->pData, release );
        ::rtl_freeMemory( pAny->pData );
        break;
    case typelib_TypeClass_TYPEDEF:
        OSL_FAIL( "### unexpected typedef!" );
        break;
    case typelib_TypeClass_STRUCT:
    case typelib_TypeClass_EXCEPTION:
    {
        typelib_TypeDescription * pTypeDescr = 0;
        TYPELIB_DANGER_GET( &pTypeDescr, pType );
        _destructStruct( pAny->pData, (typelib_CompoundTypeDescription *)pTypeDescr, release );
        TYPELIB_DANGER_RELEASE( pTypeDescr );
        ::rtl_freeMemory( pAny->pData );
        break;
    }
    case typelib_TypeClass_UNION:
    {
        typelib_TypeDescription * pTypeDescr = 0;
        TYPELIB_DANGER_GET( &pTypeDescr, pType );
        _destructUnion( pAny->pData, pTypeDescr, release );
        TYPELIB_DANGER_RELEASE( pTypeDescr );
        ::rtl_freeMemory( pAny->pData );
        break;
    }
    case typelib_TypeClass_SEQUENCE:
    {
        destructSequence(
            *(uno_Sequence **) &pAny->pReserved, pType, 0, release );
        break;
    }
    case typelib_TypeClass_INTERFACE:
        _release( pAny->pReserved, release );
        break;
    default:
        break;
    }
#if OSL_DEBUG_LEVEL > 0
    pAny->pData = (void *)0xdeadbeef;
#endif

    ::typelib_typedescriptionreference_release( pType );
}
//--------------------------------------------------------------------------------------------------
inline sal_Int32 idestructElements(
    void * pElements, typelib_TypeDescriptionReference * pElementType,
    sal_Int32 nStartIndex, sal_Int32 nStopIndex,
    uno_ReleaseFunc release )
    SAL_THROW(())
{
    switch (pElementType->eTypeClass)
    {
    case typelib_TypeClass_CHAR:
        return (sal_Int32)(sizeof(sal_Unicode));
    case typelib_TypeClass_BOOLEAN:
        return (sal_Int32)(sizeof(sal_Bool));
    case typelib_TypeClass_BYTE:
        return (sal_Int32)(sizeof(sal_Int8));
    case typelib_TypeClass_SHORT:
    case typelib_TypeClass_UNSIGNED_SHORT:
        return (sal_Int32)(sizeof(sal_Int16));
    case typelib_TypeClass_LONG:
    case typelib_TypeClass_UNSIGNED_LONG:
        return (sal_Int32)(sizeof(sal_Int32));
    case typelib_TypeClass_HYPER:
    case typelib_TypeClass_UNSIGNED_HYPER:
        return (sal_Int32)(sizeof(sal_Int64));
    case typelib_TypeClass_FLOAT:
        return (sal_Int32)(sizeof(float));
    case typelib_TypeClass_DOUBLE:
        return (sal_Int32)(sizeof(double));

    case typelib_TypeClass_STRING:
    {
        rtl_uString ** pDest = (rtl_uString **)pElements;
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            ::rtl_uString_release( pDest[nPos] );
        }
        return (sal_Int32)(sizeof(rtl_uString *));
    }
    case typelib_TypeClass_TYPE:
    {
        typelib_TypeDescriptionReference ** pDest = (typelib_TypeDescriptionReference **)pElements;
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            ::typelib_typedescriptionreference_release( pDest[nPos] );
        }
        return (sal_Int32)(sizeof(typelib_TypeDescriptionReference *));
    }
    case typelib_TypeClass_ANY:
    {
        uno_Any * pDest = (uno_Any *)pElements;
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            _destructAny( &pDest[nPos], release );
        }
        return (sal_Int32)(sizeof(uno_Any));
    }
    case typelib_TypeClass_ENUM:
        return (sal_Int32)(sizeof(sal_Int32));
    case typelib_TypeClass_STRUCT:
    case typelib_TypeClass_EXCEPTION:
    {
        typelib_TypeDescription * pElementTypeDescr = 0;
        TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
        sal_Int32 nElementSize = pElementTypeDescr->nSize;<--- Possible null pointer dereference: pElementTypeDescr
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            _destructStruct(
                (char *)pElements + (nElementSize * nPos),
                (typelib_CompoundTypeDescription *)pElementTypeDescr,
                release );
        }
        sal_Int32 nSize = pElementTypeDescr->nSize;
        TYPELIB_DANGER_RELEASE( pElementTypeDescr );
        return nSize;
    }
    case typelib_TypeClass_UNION:
    {
        typelib_TypeDescription * pElementTypeDescr = 0;
        TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
        sal_Int32 nElementSize = pElementTypeDescr->nSize;<--- Possible null pointer dereference: pElementTypeDescr
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            _destructUnion(
                (char *)pElements + (nElementSize * nPos),
                pElementTypeDescr,
                release );
        }
        sal_Int32 nSize = pElementTypeDescr->nSize;
        TYPELIB_DANGER_RELEASE( pElementTypeDescr );
        return nSize;
    }
    case typelib_TypeClass_SEQUENCE:
    {
        typelib_TypeDescription * pElementTypeDescr = 0;
        TYPELIB_DANGER_GET( &pElementTypeDescr, pElementType );
        uno_Sequence ** pDest = (uno_Sequence **)pElements;
        for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
        {
            destructSequence(
                pDest[nPos],
                pElementTypeDescr->pWeakRef, pElementTypeDescr,<--- Possible null pointer dereference: pElementTypeDescr
                release );
        }
        TYPELIB_DANGER_RELEASE( pElementTypeDescr );
        return (sal_Int32)(sizeof(uno_Sequence *));
    }
    case typelib_TypeClass_INTERFACE:
    {
        if (release)
        {
            for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
            {
                void * p = ((void **)pElements)[nPos];
                if (p)
                {
                    (*release)( p );
                }
            }
        }
        else
        {
            for ( sal_Int32 nPos = nStartIndex; nPos < nStopIndex; ++nPos )
            {
                uno_Interface * p = ((uno_Interface **)pElements)[nPos];
                if (p)
                {
                    (*p->release)( p );
                }
            }
        }
        return (sal_Int32)(sizeof(void *));
    }
    default:
        OSL_ASSERT(false);
        return 0;
    }
}

//------------------------------------------------------------------------------
inline void idestructSequence(
    uno_Sequence * pSeq,
    typelib_TypeDescriptionReference * pType,
    typelib_TypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
{
    if (::osl_decrementInterlockedCount( &pSeq->nRefCount ) == 0)
    {
        if (pSeq->nElements > 0)
        {
            if (pTypeDescr)
            {
                idestructElements(
                    pSeq->elements,
                    ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
                    pSeq->nElements, release );
            }
            else
            {
                TYPELIB_DANGER_GET( &pTypeDescr, pType );
                idestructElements(
                    pSeq->elements,
                    ((typelib_IndirectTypeDescription *) pTypeDescr)->pType, 0,
                    pSeq->nElements, release );
                TYPELIB_DANGER_RELEASE( pTypeDescr );
            }
        }
        ::rtl_freeMemory( pSeq );
    }
}

//--------------------------------------------------------------------------------------------------
inline void _destructData(
    void * pValue,
    typelib_TypeDescriptionReference * pType,
    typelib_TypeDescription * pTypeDescr,
    uno_ReleaseFunc release )
    SAL_THROW(())
{
    switch (pType->eTypeClass)
    {
    case typelib_TypeClass_STRING:
        ::rtl_uString_release( *(rtl_uString **)pValue );
        break;
    case typelib_TypeClass_TYPE:
        ::typelib_typedescriptionreference_release( *(typelib_TypeDescriptionReference **)pValue );
        break;
    case typelib_TypeClass_ANY:
        _destructAny( (uno_Any *)pValue, release );
        break;
    case typelib_TypeClass_TYPEDEF:
        OSL_FAIL( "### unexpected typedef!" );
        break;
    case typelib_TypeClass_STRUCT:
    case typelib_TypeClass_EXCEPTION:
        if (pTypeDescr)
        {
            _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
        }
        else
        {
            TYPELIB_DANGER_GET( &pTypeDescr, pType );
            _destructStruct( pValue, (typelib_CompoundTypeDescription *)pTypeDescr, release );
            TYPELIB_DANGER_RELEASE( pTypeDescr );
        }
        break;
    case typelib_TypeClass_ARRAY:
        if (pTypeDescr)
        {
            _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
        }
        else
        {
            TYPELIB_DANGER_GET( &pTypeDescr, pType );
            _destructArray( pValue, (typelib_ArrayTypeDescription *)pTypeDescr, release );
            TYPELIB_DANGER_RELEASE( pTypeDescr );
        }
        break;
    case typelib_TypeClass_UNION:
        if (pTypeDescr)
        {
            _destructUnion( pValue, pTypeDescr, release );
        }
        else
        {
            TYPELIB_DANGER_GET( &pTypeDescr, pType );
            _destructUnion( pValue, pTypeDescr, release );
            TYPELIB_DANGER_RELEASE( pTypeDescr );
        }
        break;
    case typelib_TypeClass_SEQUENCE:
    {
        idestructSequence(
            *(uno_Sequence **)pValue, pType, pTypeDescr, release );
        break;
    }
    case typelib_TypeClass_INTERFACE:
        _release( *(void **)pValue, release );
        break;
    default:
        break;
    }
}

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */