cid#1640468 Dereference after null check
[LibreOffice.git] / stoc / source / corereflection / crarray.cxx
blob1eecd9c99646ca4b0ac14587348f712f41a9fb61
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <typelib/typedescription.h>
21 #include <uno/data.h>
22 #include <cppuhelper/queryinterface.hxx>
23 #include <cppuhelper/typeprovider.hxx>
25 #include "base.hxx"
27 using namespace css::lang;
28 using namespace css::reflection;
29 using namespace css::uno;
31 namespace stoc_corefl
34 // XIdlArray
36 void ArrayIdlClassImpl::realloc( Any & rArray, sal_Int32 nLen )
38 TypeClass eTC = rArray.getValueTypeClass();
39 if (eTC != TypeClass_SEQUENCE)
41 throw IllegalArgumentException(
42 "expected sequence, but found " + rArray.getValueTypeName(),
43 getXWeak(), 0 );
45 if (nLen < 0)
47 throw IllegalArgumentException(
48 u"negative length given!"_ustr,
49 getXWeak(), 1 );
52 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
53 uno_sequence_realloc( ppSeq, &getTypeDescr()->aBase,
54 nLen,
55 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
56 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
57 rArray.pData = ppSeq;
60 sal_Int32 ArrayIdlClassImpl::getLen( const Any & rArray )
62 TypeClass eTC = rArray.getValueTypeClass();
63 if (eTC != TypeClass_SEQUENCE)
65 throw IllegalArgumentException(
66 "expected sequence, but found " + rArray.getValueTypeName(),
67 getXWeak(), 0 );
70 return (*static_cast<uno_Sequence * const *>(rArray.getValue()))->nElements;
73 Any ArrayIdlClassImpl::get( const Any & rArray, sal_Int32 nIndex )
75 TypeClass eTC = rArray.getValueTypeClass();
76 if (eTC != TypeClass_SEQUENCE)
78 throw IllegalArgumentException(
79 "expected sequence, but found " + rArray.getValueTypeName(),
80 getXWeak(), 0 );
83 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
84 if (pSeq->nElements <= nIndex)
86 throw ArrayIndexOutOfBoundsException(
87 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
88 getXWeak() );
91 Any aRet;
92 typelib_TypeDescription * pElemTypeDescr = nullptr;
93 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
94 uno_any_destruct( &aRet, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
95 uno_any_construct( &aRet, &pSeq->elements[nIndex * pElemTypeDescr->nSize],
96 pElemTypeDescr,
97 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
98 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
99 return aRet;
103 void ArrayIdlClassImpl::set( Any & rArray, sal_Int32 nIndex, const Any & rNewValue )
105 TypeClass eTC = rArray.getValueTypeClass();
106 if (eTC != TypeClass_SEQUENCE)
108 throw IllegalArgumentException(
109 "expected sequence, but found " + rArray.getValueTypeName(),
110 getXWeak(), 0 );
113 uno_Sequence * pSeq = *static_cast<uno_Sequence * const *>(rArray.getValue());
114 if (pSeq->nElements <= nIndex)
116 throw ArrayIndexOutOfBoundsException(
117 "illegal index given, index " + OUString::number(nIndex) + " is < " + OUString::number(pSeq->nElements),
118 getXWeak() );
121 uno_Sequence ** ppSeq = const_cast<uno_Sequence **>(static_cast<uno_Sequence * const *>(rArray.getValue()));
122 uno_sequence_reference2One(
123 ppSeq, &getTypeDescr()->aBase,
124 reinterpret_cast< uno_AcquireFunc >(cpp_acquire),
125 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
126 rArray.pData = ppSeq;
127 pSeq = *ppSeq;
129 typelib_TypeDescription * pElemTypeDescr = nullptr;
130 TYPELIB_DANGER_GET( &pElemTypeDescr, getTypeDescr()->pType );
132 if (! coerce_assign( &pSeq->elements[nIndex * pElemTypeDescr->nSize],
133 pElemTypeDescr, rNewValue, getReflection() ))
135 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
136 throw IllegalArgumentException(
137 u"sequence element is not assignable by given value!"_ustr,
138 getXWeak(), 2 );
140 TYPELIB_DANGER_RELEASE( pElemTypeDescr );
143 // ArrayIdlClassImpl
145 sal_Bool ArrayIdlClassImpl::isAssignableFrom( const Reference< XIdlClass > & xType )
147 return (xType.is() &&
148 (equals( xType ) ||
149 (xType->getTypeClass() == getTypeClass() && // must be sequence|array
150 getComponentType()->isAssignableFrom( xType->getComponentType() ))));
153 Reference< XIdlClass > ArrayIdlClassImpl::getComponentType()
155 return getReflection()->forType( getTypeDescr()->pType );
158 Reference< XIdlArray > ArrayIdlClassImpl::getArray()
160 return this;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */