Avoid potential negative array index access to cached text.
[LibreOffice.git] / cppuhelper / source / exc_thrower.cxx
blob57e2ad1d0235e14adfc1548813d0f942bb417a6e
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 <rtl/instance.hxx>
21 #include <osl/diagnose.h>
22 #include <sal/log.hxx>
23 #include <uno/dispatcher.hxx>
24 #include <uno/lbnames.h>
25 #include <uno/mapping.hxx>
26 #include <cppuhelper/detail/XExceptionThrower.hpp>
27 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
28 #include <com/sun/star/ucb/NameClashException.hpp>
29 #include <com/sun/star/uno/RuntimeException.hpp>
31 #include <cppuhelper/exc_hlp.hxx>
33 using namespace ::osl;
34 using namespace ::cppu;
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::uno;
38 namespace
41 using cppuhelper::detail::XExceptionThrower;
44 struct ExceptionThrower : public uno_Interface, XExceptionThrower
46 ExceptionThrower();
48 virtual ~ExceptionThrower() {}
50 static Type const & getCppuType()
52 return cppu::UnoType<XExceptionThrower>::get();
55 // XInterface
56 virtual Any SAL_CALL queryInterface( Type const & type ) override;
57 virtual void SAL_CALL acquire() noexcept override;
58 virtual void SAL_CALL release() noexcept override;
60 // XExceptionThrower
61 virtual void SAL_CALL throwException( Any const & exc ) override;
62 virtual void SAL_CALL rethrowException() override;
65 extern "C"
69 void ExceptionThrower_acquire_release_nop(
70 SAL_UNUSED_PARAMETER uno_Interface * )
74 void ExceptionThrower_dispatch(
75 uno_Interface * pUnoI, typelib_TypeDescription const * pMemberType,
76 void * pReturn, void * pArgs [], uno_Any ** ppException )
78 OSL_ASSERT( pMemberType->eTypeClass == typelib_TypeClass_INTERFACE_METHOD );
80 switch (reinterpret_cast< typelib_InterfaceMemberTypeDescription * >(
81 const_cast< typelib_TypeDescription * >( pMemberType ) )->
82 nPosition)
84 case 0: // queryInterface()
86 Type const & rType_demanded =
87 *static_cast< Type const * >( pArgs[ 0 ] );
88 if (rType_demanded.equals( cppu::UnoType<XInterface>::get() ) ||
89 rType_demanded.equals( ExceptionThrower::getCppuType() ))
91 typelib_TypeDescription * pTD = nullptr;
92 TYPELIB_DANGER_GET( &pTD, rType_demanded.getTypeLibType() );
93 uno_any_construct(
94 static_cast< uno_Any * >( pReturn ), &pUnoI, pTD, nullptr );
95 TYPELIB_DANGER_RELEASE( pTD );
97 else
99 uno_any_construct(
100 static_cast< uno_Any * >( pReturn ), nullptr, nullptr, nullptr );
102 *ppException = nullptr;
103 break;
105 case 1: // acquire()
106 case 2: // release()
107 *ppException = nullptr;
108 break;
109 case 3: // throwException()
111 uno_Any * pAny = static_cast< uno_Any * >( pArgs[ 0 ] );
112 OSL_ASSERT( pAny->pType->eTypeClass == typelib_TypeClass_EXCEPTION );
113 uno_type_any_construct( *ppException, pAny->pData, pAny->pType, nullptr );
114 break;
116 default:
118 OSL_ASSERT( false );
119 RuntimeException exc( "not implemented!" );
120 uno_type_any_construct(
121 *ppException, &exc, cppu::UnoType<decltype(exc)>::get().getTypeLibType(), nullptr );
122 break;
127 } // extern "C"
130 Any ExceptionThrower::queryInterface( Type const & type )
132 if (type.equals( cppu::UnoType<XInterface>::get() ) ||
133 type.equals( ExceptionThrower::getCppuType() ))
135 XExceptionThrower * that = this;
136 return Any( &that, type );
138 return Any();
142 void ExceptionThrower::acquire() noexcept
146 void ExceptionThrower::release() noexcept
151 void ExceptionThrower::throwException( Any const & exc )
153 OSL_FAIL( "unexpected!" );
154 cppu::throwException( exc );
158 void ExceptionThrower::rethrowException()
160 throw;
164 ExceptionThrower::ExceptionThrower()
166 uno_Interface::acquire = ExceptionThrower_acquire_release_nop;
167 uno_Interface::release = ExceptionThrower_acquire_release_nop;
168 uno_Interface::pDispatcher = ExceptionThrower_dispatch;
171 #if defined(IOS) || defined(ANDROID) || defined(EMSCRIPTEN)
172 #define RETHROW_FAKE_EXCEPTIONS 1
173 #else
174 #define RETHROW_FAKE_EXCEPTIONS 0
175 #endif
177 class theExceptionThrower : public rtl::Static<ExceptionThrower, theExceptionThrower> {};
179 #if RETHROW_FAKE_EXCEPTIONS
180 // In the native iOS / Android app, where we don't have any Java, Python,
181 // BASIC, or other scripting, the only thing that would use the C++/UNO bridge
182 // functionality that invokes codeSnippet() was cppu::throwException().
184 // codeSnippet() is part of what corresponds to the code that uses
185 // run-time-generated machine code on other platforms. We can't generate code
186 // at run-time on iOS, that has been known forever.
188 // Instead of digging in and trying to understand what is wrong, another
189 // solution was chosen. It turns out that the number of types of exception
190 // objects thrown by cppu::throwException() is fairly small. During startup of
191 // the LibreOffice code, and loading of an .odt document, only one kind of
192 // exception is thrown this way... (The lovely
193 // css::ucb:InteractiveAugmentedIOException.)
195 // So we can simply have code that checks what the type of object being thrown
196 // is, and explicitly throws such an object then with a normal C++ throw
197 // statement. Seems to work.
198 template <class E> void tryThrow(css::uno::Any const& aException)
200 E aSpecificException;
201 if (aException >>= aSpecificException)
202 throw aSpecificException;
205 void lo_mobile_throwException(css::uno::Any const& aException)
207 assert(aException.getValueTypeClass() == css::uno::TypeClass_EXCEPTION);
209 tryThrow<css::ucb::InteractiveAugmentedIOException>(aException);
210 tryThrow<css::ucb::NameClashException>(aException);
211 tryThrow<css::uno::RuntimeException>(aException);
213 SAL_WARN("cppuhelper", "lo_mobile_throwException: Unhandled exception type: " << aException.getValueTypeName());
215 assert(false);
217 #endif // RETHROW_FAKE_EXCEPTIONS
219 } // anonymous namespace
222 namespace cppu
226 void SAL_CALL throwException( Any const & exc )
228 if (exc.getValueTypeClass() != TypeClass_EXCEPTION)
230 throw RuntimeException(
231 "no UNO exception given "
232 "(must be derived from com::sun::star::uno::Exception)!" );
235 #if RETHROW_FAKE_EXCEPTIONS
236 lo_mobile_throwException(exc);
237 #else
238 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
239 if (! uno2cpp.is())
241 throw RuntimeException(
242 "cannot get binary UNO to C++ mapping!" );
245 Reference< XExceptionThrower > xThrower;
246 uno2cpp.mapInterface(
247 reinterpret_cast< void ** >( &xThrower ),
248 static_cast< uno_Interface * >( &theExceptionThrower::get() ),
249 ExceptionThrower::getCppuType() );
250 OSL_ASSERT( xThrower.is() );
251 xThrower->throwException( exc );
252 #endif // !RETHROW_FAKE_EXCEPTIONS
256 Any SAL_CALL getCaughtException()
258 // why does this differ from RETHROW_FAKE_EXCEPTIONS?
259 #if defined(ANDROID) || defined(EMSCRIPTEN)
260 return Any();
261 #else
262 Mapping cpp2uno(Environment::getCurrent(), Environment(UNO_LB_UNO));
263 if (! cpp2uno.is())
265 throw RuntimeException(
266 "cannot get C++ to binary UNO mapping!" );
268 Mapping uno2cpp(Environment(UNO_LB_UNO), Environment::getCurrent());
269 if (! uno2cpp.is())
271 throw RuntimeException(
272 "cannot get binary UNO to C++ mapping!" );
275 typelib_TypeDescription * pTD = nullptr;
276 TYPELIB_DANGER_GET(
277 &pTD, ExceptionThrower::getCppuType().getTypeLibType() );
279 UnoInterfaceReference unoI;
280 cpp2uno.mapInterface(
281 reinterpret_cast< void ** >( &unoI.m_pUnoI ),
282 static_cast< XExceptionThrower * >( &theExceptionThrower::get() ), pTD );
283 OSL_ASSERT( unoI.is() );
285 typelib_TypeDescription * pMemberTD = nullptr;
286 TYPELIB_DANGER_GET(
287 &pMemberTD,
288 reinterpret_cast< typelib_InterfaceTypeDescription * >( pTD )->
289 ppMembers[ 1 ] /* rethrowException() */ );
291 uno_Any exc_mem;
292 uno_Any * exc = &exc_mem;
293 unoI.dispatch( pMemberTD, nullptr, nullptr, &exc );
295 TYPELIB_DANGER_RELEASE( pMemberTD );
296 TYPELIB_DANGER_RELEASE( pTD );
298 if (exc == nullptr)
300 throw RuntimeException( "rethrowing C++ exception failed!" );
303 Any ret;
304 uno_any_destruct( &ret, reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
305 uno_type_any_constructAndConvert(
306 &ret, exc->pData, exc->pType, uno2cpp.get() );
307 uno_any_destruct( exc, nullptr );
308 return ret;
309 #endif
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */