Bug Summary

File:home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx
Warning:line 454, column 92
Use of memory after it is freed

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name dbsubcomponentcontroller.cxx -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D BOOST_ERROR_CODE_HEADER_ONLY -D BOOST_SYSTEM_NO_DEPRECATED -D CPPU_ENV=gcc3 -D LINUX -D OSL_DEBUG_LEVEL=1 -D SAL_LOG_INFO -D SAL_LOG_WARN -D UNIX -D UNX -D X86_64 -D _PTHREADS -D _REENTRANT -D DBACCESS_DLLIMPLEMENTATION -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/external/unixODBC/inc -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include -I /usr/lib/jvm/java-11-openjdk-11.0.9.10-0.0.ea.fc33.x86_64/include/linux -I /home/maarten/src/libreoffice/core/config_host -I /home/maarten/src/libreoffice/core/dbaccess/inc -I /home/maarten/src/libreoffice/core/dbaccess/source/inc -I /home/maarten/src/libreoffice/core/dbaccess/source/ui/inc -I /home/maarten/src/libreoffice/core/workdir/YaccTarget/connectivity/source/parse -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O0 -Wno-missing-braces -std=c++17 -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core -ferror-limit 19 -fvisibility hidden -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -debug-info-kind=constructor -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ /home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx

/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <browserids.hxx>
21#include <commontypes.hxx>
22#include <core_resource.hxx>
23#include <dbaccess/dataview.hxx>
24#include <strings.hrc>
25#include <strings.hxx>
26#include <dbaccess/dbsubcomponentcontroller.hxx>
27
28#include <com/sun/star/frame/XUntitledNumbers.hpp>
29#include <com/sun/star/container/XChild.hpp>
30#include <com/sun/star/sdb/XDocumentDataSource.hpp>
31#include <com/sun/star/sdb/XOfficeDatabaseDocument.hpp>
32#include <com/sun/star/sdbc/XDataSource.hpp>
33#include <com/sun/star/util/NumberFormatter.hpp>
34#include <com/sun/star/lang/IllegalArgumentException.hpp>
35
36#include <comphelper/types.hxx>
37#include <connectivity/dbexception.hxx>
38#include <connectivity/dbmetadata.hxx>
39#include <connectivity/dbtools.hxx>
40#include <comphelper/interfacecontainer2.hxx>
41#include <rtl/ustrbuf.hxx>
42#include <sal/log.hxx>
43#include <toolkit/helper/vclunohelper.hxx>
44#include <tools/debug.hxx>
45#include <tools/diagnose_ex.h>
46#include <vcl/svapp.hxx>
47#include <vcl/weld.hxx>
48
49namespace dbaui
50{
51
52 using ::com::sun::star::uno::Any;
53 using ::com::sun::star::uno::Reference;
54 using ::com::sun::star::beans::XPropertySet;
55 using ::com::sun::star::uno::Sequence;
56 using ::com::sun::star::uno::Type;
57 using ::com::sun::star::uno::XComponentContext;
58 using ::com::sun::star::sdbc::XConnection;
59 using ::com::sun::star::uno::UNO_QUERY;
60 using ::com::sun::star::container::XChild;
61 using ::com::sun::star::sdbc::XDataSource;
62 using ::com::sun::star::util::NumberFormatter;
63 using ::com::sun::star::util::XNumberFormatter;
64 using ::com::sun::star::util::XNumberFormatsSupplier;
65 using ::com::sun::star::frame::XFrame;
66 using ::com::sun::star::uno::Exception;
67 using ::com::sun::star::lang::EventObject;
68 using ::com::sun::star::beans::PropertyValue;
69 using ::com::sun::star::frame::XModel;
70 using ::com::sun::star::sdb::XOfficeDatabaseDocument;
71 using ::com::sun::star::awt::XWindow;
72 using ::com::sun::star::sdbc::XDatabaseMetaData;
73 using ::com::sun::star::sdb::XDocumentDataSource;
74 using ::com::sun::star::document::XEmbeddedScripts;
75 using ::com::sun::star::lang::IllegalArgumentException;
76 using ::com::sun::star::uno::UNO_SET_THROW;
77 using ::com::sun::star::uno::UNO_QUERY_THROW;
78 using ::com::sun::star::frame::XUntitledNumbers;
79
80 namespace {
81
82 class DataSourceHolder
83 {
84 public:
85 DataSourceHolder()
86 {
87 }
88
89 explicit DataSourceHolder(const Reference< XDataSource >& _rxDataSource)
90 : m_xDataSource(_rxDataSource)
91 {
92 Reference< XDocumentDataSource > xDocDS( m_xDataSource, UNO_QUERY );
93 if ( xDocDS.is() )
94 m_xDocument = xDocDS->getDatabaseDocument();
95
96 m_xDataSourceProps.set( m_xDataSource, UNO_QUERY );
97 }
98
99 const Reference< XDataSource >& getDataSource() const { return m_xDataSource; }
100 const Reference< XPropertySet >& getDataSourceProps() const { return m_xDataSourceProps; }
101 const Reference< XOfficeDatabaseDocument >& getDatabaseDocument() const { return m_xDocument; }
102
103 bool is() const { return m_xDataSource.is(); }
104
105 void clear()
106 {
107 m_xDataSource.clear();
108 m_xDocument.clear();
109 }
110
111 private:
112 Reference< XDataSource > m_xDataSource;
113 Reference< XPropertySet > m_xDataSourceProps;
114 Reference< XOfficeDatabaseDocument > m_xDocument;
115 };
116
117 }
118
119 struct DBSubComponentController_Impl
120 {
121 private:
122 ::std::optional< bool > m_aDocScriptSupport;
123
124 public:
125 ::dbtools::SQLExceptionInfo m_aCurrentError;
126
127 ::comphelper::OInterfaceContainerHelper2
128 m_aModifyListeners;
129
130 // <properties>
131 SharedConnection m_xConnection;
132 ::dbtools::DatabaseMetaData m_aSdbMetaData;
133 // </properties>
134 OUString m_sDataSourceName; // the data source we're working for
135 DataSourceHolder m_aDataSource;
136 Reference< XNumberFormatter > m_xFormatter; // a number formatter working with the connection's NumberFormatsSupplier
137 sal_Int32 m_nDocStartNumber;
138 bool m_bSuspended; // is true when the controller was already suspended
139 bool m_bEditable; // is the control readonly or not
140 bool m_bModified; // is the data modified
141 bool m_bNotAttached;
142
143 explicit DBSubComponentController_Impl(osl::Mutex& i_rMutex)
144 :m_aDocScriptSupport()
145 ,m_aModifyListeners( i_rMutex )
146 ,m_nDocStartNumber(0)
147 ,m_bSuspended( false )
148 ,m_bEditable(true)
149 ,m_bModified(false)
150 ,m_bNotAttached(true)
151 {
152 }
153
154 bool documentHasScriptSupport() const
155 {
156 OSL_PRECOND( !!m_aDocScriptSupport,do { if (true && (!(!!m_aDocScriptSupport))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "157" ": "), "%s", "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?"
); } } while (false)
157 "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?" )do { if (true && (!(!!m_aDocScriptSupport))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "157" ": "), "%s", "DBSubComponentController_Impl::documentHasScriptSupport: not completely initialized, yet - don't know!?"
); } } while (false)
;
158 return !!m_aDocScriptSupport && *m_aDocScriptSupport;
159 }
160
161 void setDocumentScriptSupport( const bool _bSupport )
162 {
163 OSL_PRECOND( !m_aDocScriptSupport,do { if (true && (!(!m_aDocScriptSupport))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "164" ": "), "%s", "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!"
); } } while (false)
164 "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!" )do { if (true && (!(!m_aDocScriptSupport))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "164" ": "), "%s", "DBSubComponentController_Impl::setDocumentScriptSupport: already initialized!"
); } } while (false)
;
165 m_aDocScriptSupport = ::std::optional< bool >( _bSupport );
166 }
167 };
168
169 // DBSubComponentController
170 DBSubComponentController::DBSubComponentController(const Reference< XComponentContext >& _rxORB)
171 :DBSubComponentController_Base( _rxORB )
172 ,m_pImpl( new DBSubComponentController_Impl( getMutex() ) )
173 {
174 }
175
176 DBSubComponentController::~DBSubComponentController()
177 {
178 }
179
180 void DBSubComponentController::impl_initialize()
181 {
182 OGenericUnoController::impl_initialize();
183
184 const ::comphelper::NamedValueCollection& rArguments( getInitParams() );
185
186 Reference< XConnection > xConnection;
187 xConnection = rArguments.getOrDefault( PROPERTY_ACTIVE_CONNECTION"ActiveConnection", xConnection );
188
189 if ( !xConnection.is() )
190 ::dbtools::isEmbeddedInDatabase( getModel(), xConnection );
191
192 if ( xConnection.is() )
193 initializeConnection( xConnection );
194
195 bool bShowError = true;
196 if ( !isConnected() )
197 {
198 reconnect( false );
199 bShowError = false;
200 }
201 if ( !isConnected() )
202 {
203 if ( bShowError )
204 connectionLostMessage();
205 throw IllegalArgumentException();
206 }
207 }
208
209 Any SAL_CALL DBSubComponentController::queryInterface(const Type& _rType)
210 {
211 if ( _rType.equals( cppu::UnoType<XScriptInvocationContext>::get() ) )
212 {
213 if ( m_pImpl->documentHasScriptSupport() )
214 return makeAny( Reference< XScriptInvocationContext >( this ) );
215 return Any();
216 }
217
218 return DBSubComponentController_Base::queryInterface( _rType );
219 }
220
221 Sequence< Type > SAL_CALL DBSubComponentController::getTypes( )
222 {
223 Sequence< Type > aTypes( DBSubComponentController_Base::getTypes() );
224 if ( !m_pImpl->documentHasScriptSupport() )
225 {
226 auto newEnd = std::remove_if( aTypes.begin(), aTypes.end(),
227 [](const Type& type)
228 { return type == cppu::UnoType<XScriptInvocationContext>::get(); } );
229 aTypes.realloc( std::distance(aTypes.begin(), newEnd) );
230 }
231 return aTypes;
232 }
233
234 void DBSubComponentController::initializeConnection( const Reference< XConnection >& _rxForeignConn )
235 {
236 DBG_ASSERT( !isConnected(), "DBSubComponentController::initializeConnection: not to be called when already connected!" )do { if (true && (!(!isConnected()))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "236" ": "), "%s", "DBSubComponentController::initializeConnection: not to be called when already connected!"
); } } while (false)
;
237 // usually this gets called from within initialize of derived classes ...
238 if ( isConnected() )
239 disconnect();
240
241 m_pImpl->m_xConnection.reset( _rxForeignConn, SharedConnection::NoTakeOwnership );
242 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
243 startConnectionListening( m_pImpl->m_xConnection );
244
245 // get the data source the connection belongs to
246 try
247 {
248 // determine our data source
249 OSL_PRECOND( !m_pImpl->m_aDataSource.is(), "DBSubComponentController::initializeConnection: already a data source in this phase?" )do { if (true && (!(!m_pImpl->m_aDataSource.is()))
) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "249" ": "), "%s", "DBSubComponentController::initializeConnection: already a data source in this phase?"
); } } while (false)
;
250 {
251 Reference< XChild > xConnAsChild( m_pImpl->m_xConnection, UNO_QUERY );
252 Reference< XDataSource > xDS;
253 if ( xConnAsChild.is() )
254 xDS.set( xConnAsChild->getParent(), UNO_QUERY );
255
256 // (take the indirection through XDataSource to ensure we have a correct object...)
257 m_pImpl->m_aDataSource = DataSourceHolder(xDS);
258 }
259 SAL_WARN_IF( !m_pImpl->m_aDataSource.is(), "dbaccess.ui", "DBSubComponentController::initializeConnection: unable to obtain the data source object!" )do { if (true && (!m_pImpl->m_aDataSource.is())) {
switch (sal_detail_log_report(::SAL_DETAIL_LOG_LEVEL_WARN, "dbaccess.ui"
)) { case SAL_DETAIL_LOG_ACTION_IGNORE: break; case SAL_DETAIL_LOG_ACTION_LOG
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("dbaccess.ui"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "259" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("dbaccess.ui"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "259" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("dbaccess.ui"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "259" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "DBSubComponentController::initializeConnection: unable to obtain the data source object!"
; ::sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("dbaccess.ui"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "259" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
260
261 if ( m_pImpl->m_bNotAttached )
262 {
263 Reference< XUntitledNumbers > xUntitledProvider( getDatabaseDocument(), UNO_QUERY );
264 m_pImpl->m_nDocStartNumber = 1;
265 if ( xUntitledProvider.is() )
266 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
267 }
268
269 // determine the availability of script support in our document. Our own XScriptInvocationContext
270 // interface depends on this
271 m_pImpl->setDocumentScriptSupport( Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY ).is() );
272
273 // get a number formatter
274 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps(), UNO_SET_THROW );
275 xDataSourceProps->getPropertyValue( PROPERTY_NAME"Name" ) >>= m_pImpl->m_sDataSourceName;
276 DBG_ASSERT( !m_pImpl->m_sDataSourceName.isEmpty(), "DBSubComponentController::initializeConnection: invalid data source name!" )do { if (true && (!(!m_pImpl->m_sDataSourceName.isEmpty
()))) { sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.tools"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "276" ": "), "%s", "DBSubComponentController::initializeConnection: invalid data source name!"
); } } while (false)
;
277 Reference< XNumberFormatsSupplier> xSupplier = ::dbtools::getNumberFormats(m_pImpl->m_xConnection);
278 if(xSupplier.is())
279 {
280 m_pImpl->m_xFormatter.set(NumberFormatter::create(getORB()), UNO_QUERY_THROW);
281 m_pImpl->m_xFormatter->attachNumberFormatsSupplier(xSupplier);
282 }
283 OSL_ENSURE(m_pImpl->m_xFormatter.is(),"No NumberFormatter!")do { if (true && (!(m_pImpl->m_xFormatter.is()))) {
sal_detail_logFormat((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"
), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "283" ": "), "%s", "No NumberFormatter!"); } } while (false
)
;
284 }
285 catch( const Exception& )
286 {
287 DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "287" ": ", "dbaccess" );
;
288 }
289 }
290
291 void DBSubComponentController::reconnect( bool _bUI )
292 {
293 OSL_ENSURE(!m_pImpl->m_bSuspended, "Cannot reconnect while suspended!")do { if (true && (!(!m_pImpl->m_bSuspended))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.osl"), ("/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "293" ": "), "%s", "Cannot reconnect while suspended!");
} } while (false)
;
294
295 stopConnectionListening( m_pImpl->m_xConnection );
296 m_pImpl->m_aSdbMetaData.reset( nullptr );
297 m_pImpl->m_xConnection.clear();
298
299 // reconnect
300 bool bReConnect = true;
301 if ( _bUI )
302 {
303 std::unique_ptr<weld::MessageDialog> xQuery(Application::CreateMessageDialog(getFrameWeld(),
304 VclMessageType::Question, VclButtonsType::YesNo,
305 DBA_RES(STR_QUERY_CONNECTION_LOST)::dbaccess::ResourceManager::loadString( reinterpret_cast<
char const *>("STR_QUERY_CONNECTION_LOST" "\004" u8"The connection to the database has been lost. Do you want to reconnect?"
) )
));
306 bReConnect = (RET_YES == xQuery->run());
307 }
308
309 // now really reconnect ...
310 if ( bReConnect )
311 {
312 m_pImpl->m_xConnection.reset( connect( m_pImpl->m_aDataSource.getDataSource() ), SharedConnection::TakeOwnership );
313 m_pImpl->m_aSdbMetaData.reset( m_pImpl->m_xConnection );
314 }
315
316 // invalidate all slots
317 InvalidateAll();
318 }
319
320 void DBSubComponentController::disconnect()
321 {
322 stopConnectionListening(m_pImpl->m_xConnection);
323 m_pImpl->m_aSdbMetaData.reset( nullptr );
324 m_pImpl->m_xConnection.clear();
325
326 InvalidateAll();
327 }
328
329 void DBSubComponentController::losingConnection()
330 {
331 // our connection was disposed so we need a new one
332 reconnect( true );
333 InvalidateAll();
334 }
335
336 void SAL_CALL DBSubComponentController::disposing()
337 {
338 DBSubComponentController_Base::disposing();
339
340 disconnect();
341
342 attachFrame( Reference < XFrame >() );
343
344 m_pImpl->m_aDataSource.clear();
345 }
346
347 void SAL_CALL DBSubComponentController::disposing(const EventObject& _rSource)
348 {
349 if ( _rSource.Source == getConnection() )
350 {
351 if ( !m_pImpl->m_bSuspended // when already suspended then we don't have to reconnect
352 && !getBroadcastHelper().bInDispose
353 && !getBroadcastHelper().bDisposed
354 && isConnected()
355 )
356 {
357 losingConnection();
358 }
359 else
360 {
361 m_pImpl->m_xConnection.reset( m_pImpl->m_xConnection, SharedConnection::NoTakeOwnership );
362 // this prevents the "disposeComponent" call in disconnect
363 disconnect();
364 }
365 }
366 else
367 DBSubComponentController_Base::disposing( _rSource );
368 }
369
370 void DBSubComponentController::appendError( const OUString& _rErrorMessage )
371 {
372 m_pImpl->m_aCurrentError.append( ::dbtools::SQLExceptionInfo::TYPE::SQLException, _rErrorMessage,
373 getStandardSQLState( ::dbtools::StandardSQLState::GENERAL_ERROR ),
374 1000 );
375 }
376 void DBSubComponentController::clearError()
377 {
378 m_pImpl->m_aCurrentError = ::dbtools::SQLExceptionInfo();
379 }
380
381 bool DBSubComponentController::hasError() const
382 {
383 return m_pImpl->m_aCurrentError.isValid();
384 }
385
386 const ::dbtools::SQLExceptionInfo& DBSubComponentController::getError() const
387 {
388 return m_pImpl->m_aCurrentError;
389 }
390
391 void DBSubComponentController::displayError()
392 {
393 showError( m_pImpl->m_aCurrentError );
394 }
395
396 sal_Bool SAL_CALL DBSubComponentController::suspend(sal_Bool bSuspend)
397 {
398 m_pImpl->m_bSuspended = bSuspend;
399 if ( !bSuspend && !isConnected() )
400 reconnect(true);
401
402 return true;
403 }
404
405 sal_Bool SAL_CALL DBSubComponentController::attachModel( const Reference< XModel > & _rxModel)
406 {
407 if ( !_rxModel.is() )
408 return false;
409 if ( !DBSubComponentController_Base::attachModel( _rxModel ) )
410 return false;
411
412 m_pImpl->m_bNotAttached = false;
413 if ( m_pImpl->m_nDocStartNumber == 1 )
414 releaseNumberForComponent();
415
416 Reference< XUntitledNumbers > xUntitledProvider( _rxModel, UNO_QUERY );
417 m_pImpl->m_nDocStartNumber = 1;
418 if ( xUntitledProvider.is() )
419 m_pImpl->m_nDocStartNumber = xUntitledProvider->leaseNumber( static_cast< XWeak* >( this ) );
420
421 return true;
422 }
423
424 void DBSubComponentController::Execute(sal_uInt16 _nId, const Sequence< PropertyValue >& _rArgs)
425 {
426 if ( _nId == ID_BROWSER_CLOSE(5000 + 621) )
427 {
428 closeTask();
429 return;
430 }
431
432 DBSubComponentController_Base::Execute( _nId, _rArgs );
433 InvalidateFeature( _nId );
434 }
435
436 OUString DBSubComponentController::getDataSourceName() const
437 {
438 OUString sName;
439 Reference< XPropertySet > xDataSourceProps( m_pImpl->m_aDataSource.getDataSourceProps() );
440 if ( xDataSourceProps.is() )
441 xDataSourceProps->getPropertyValue(PROPERTY_NAME"Name") >>= sName;
442 return sName;
443 }
444 void DBSubComponentController::connectionLostMessage() const
445 {
446 OUString aMessage(DBA_RES(RID_STR_CONNECTION_LOST)::dbaccess::ResourceManager::loadString( reinterpret_cast<
char const *>("RID_STR_CONNECTION_LOST" "\004" u8"Connection lost"
) )
);
447 Reference< XWindow > xWindow = getTopMostContainerWindow();
448 vcl::Window* pWin = nullptr;
449 if ( xWindow.is() )
1
Taking true branch
450 pWin = VCLUnoHelper::GetWindow(xWindow).get();
2
Calling implicit destructor for 'VclPtr<vcl::Window>'
3
Calling '~Reference'
11
Returning from '~Reference'
12
Returning from destructor for 'VclPtr<vcl::Window>'
451 if ( !pWin
12.1
'pWin' is non-null
12.1
'pWin' is non-null
12.1
'pWin' is non-null
)
13
Taking false branch
452 pWin = getView()->Window::GetParent();
453
454 std::unique_ptr<weld::MessageDialog> xInfo(Application::CreateMessageDialog(pWin
13.1
'pWin' is non-null
13.1
'pWin' is non-null
13.1
'pWin' is non-null
? pWin->GetFrameWeld() : nullptr,
14
'?' condition is true
15
Use of memory after it is freed
455 VclMessageType::Info, VclButtonsType::Ok, aMessage));
456 xInfo->run();
457 }
458 const Reference< XConnection >& DBSubComponentController::getConnection() const
459 {
460 return m_pImpl->m_xConnection;
461 }
462
463 bool DBSubComponentController::isReadOnly() const
464 {
465 return !m_pImpl->m_bEditable;
466 }
467
468 bool DBSubComponentController::isEditable() const
469 {
470 return m_pImpl->m_bEditable;
471 }
472
473 void DBSubComponentController::setEditable(bool _bEditable)
474 {
475 m_pImpl->m_bEditable = _bEditable;
476 }
477
478 const ::dbtools::DatabaseMetaData& DBSubComponentController::getSdbMetaData() const
479 {
480 return m_pImpl->m_aSdbMetaData;
481 }
482
483 bool DBSubComponentController::isConnected() const
484 {
485 return m_pImpl->m_xConnection.is();
486 }
487
488 Reference< XDatabaseMetaData > DBSubComponentController::getMetaData( ) const
489 {
490 Reference< XDatabaseMetaData > xMeta;
491 try
492 {
493 if ( isConnected() )
494 xMeta.set( m_pImpl->m_xConnection->getMetaData(), UNO_SET_THROW );
495 }
496 catch( const Exception& )
497 {
498 DBG_UNHANDLED_EXCEPTION("dbaccess")DbgUnhandledException( DbgGetCaughtException(), __func__, "/home/maarten/src/libreoffice/core/dbaccess/source/ui/misc/dbsubcomponentcontroller.cxx"
":" "498" ": ", "dbaccess" );
;
499 }
500 return xMeta;
501 }
502
503 const Reference< XPropertySet >& DBSubComponentController::getDataSource() const
504 {
505 return m_pImpl->m_aDataSource.getDataSourceProps();
506 }
507
508 bool DBSubComponentController::haveDataSource() const
509 {
510 return m_pImpl->m_aDataSource.is();
511 }
512
513 Reference< XModel > DBSubComponentController::getDatabaseDocument() const
514 {
515 return Reference< XModel >( m_pImpl->m_aDataSource.getDatabaseDocument(), UNO_QUERY );
516 }
517
518 Reference< XNumberFormatter > const & DBSubComponentController::getNumberFormatter() const
519 {
520 return m_pImpl->m_xFormatter;
521 }
522
523 Reference< XModel > DBSubComponentController::getPrivateModel() const
524 {
525 return getDatabaseDocument();
526 }
527 // XTitle
528 OUString SAL_CALL DBSubComponentController::getTitle()
529 {
530 ::osl::MutexGuard aGuard( getMutex() );
531 if ( m_bExternalTitle )
532 return impl_getTitleHelper_throw()->getTitle ();
533
534 OUStringBuffer sTitle;
535 Reference< XTitle > xTitle(getPrivateModel(),UNO_QUERY);
536 if ( xTitle.is() )
537 {
538 sTitle.append( xTitle->getTitle() );
539 sTitle.append(" : ");
540 }
541 sTitle.append( getPrivateTitle() );
542 return sTitle.makeStringAndClear();
543 }
544
545 sal_Int32 DBSubComponentController::getCurrentStartNumber() const
546 {
547 return m_pImpl->m_nDocStartNumber;
548 }
549
550 Reference< XEmbeddedScripts > SAL_CALL DBSubComponentController::getScriptContainer()
551 {
552 ::osl::MutexGuard aGuard( getMutex() );
553 if ( !m_pImpl->documentHasScriptSupport() )
554 return nullptr;
555
556 return Reference< XEmbeddedScripts >( getDatabaseDocument(), UNO_QUERY_THROW );
557 }
558
559 void SAL_CALL DBSubComponentController::addModifyListener( const Reference< XModifyListener >& i_Listener )
560 {
561 ::osl::MutexGuard aGuard( getMutex() );
562 m_pImpl->m_aModifyListeners.addInterface( i_Listener );
563 }
564
565 void SAL_CALL DBSubComponentController::removeModifyListener( const Reference< XModifyListener >& i_Listener )
566 {
567 ::osl::MutexGuard aGuard( getMutex() );
568 m_pImpl->m_aModifyListeners.removeInterface( i_Listener );
569 }
570
571 sal_Bool SAL_CALL DBSubComponentController::isModified( )
572 {
573 ::osl::MutexGuard aGuard( getMutex() );
574 return impl_isModified();
575 }
576
577 void SAL_CALL DBSubComponentController::setModified( sal_Bool i_bModified )
578 {
579 ::osl::ClearableMutexGuard aGuard( getMutex() );
580
581 if ( m_pImpl->m_bModified == bool(i_bModified) )
582 return;
583
584 m_pImpl->m_bModified = i_bModified;
585 impl_onModifyChanged();
586
587 EventObject aEvent( *this );
588 aGuard.clear();
589 m_pImpl->m_aModifyListeners.notifyEach( &XModifyListener::modified, aEvent );
590 }
591
592 bool DBSubComponentController::impl_isModified() const
593 {
594 return m_pImpl->m_bModified;
595 }
596
597 void DBSubComponentController::impl_onModifyChanged()
598 {
599 InvalidateFeature( ID_BROWSER_SAVEDOC(5000 + 505) );
600 if ( isFeatureSupported( ID_BROWSER_SAVEASDOC(5000 + 502) ) )
601 InvalidateFeature( ID_BROWSER_SAVEASDOC(5000 + 502) );
602 }
603
604} // namespace dbaui
605
606/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/rtl/ref.hxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#ifndef INCLUDED_RTL_REF_HXX
21#define INCLUDED_RTL_REF_HXX
22
23#include "sal/config.h"
24
25#include <cassert>
26#include <cstddef>
27#include <functional>
28#ifdef LIBO_INTERNAL_ONLY1
29#include <type_traits>
30#endif
31
32#include "sal/types.h"
33
34namespace rtl
35{
36
37/** Template reference class for reference type.
38*/
39template <class reference_type>
40class Reference
41{
42 /** The <b>reference_type</b> body pointer.
43 */
44 reference_type * m_pBody;
45
46
47public:
48 /** Constructor...
49 */
50 Reference()
51 : m_pBody (NULL__null)
52 {}
53
54
55 /** Constructor...
56 */
57 Reference (reference_type * pBody, __sal_NoAcquire)
58 : m_pBody (pBody)
59 {
60 }
61
62 /** Constructor...
63 */
64 Reference (reference_type * pBody)
65 : m_pBody (pBody)
66 {
67 if (m_pBody)
68 m_pBody->acquire();
69 }
70
71 /** Copy constructor...
72 */
73 Reference (const Reference<reference_type> & handle)
74 : m_pBody (handle.m_pBody)
75 {
76 if (m_pBody)
77 m_pBody->acquire();
78 }
79
80#ifdef LIBO_INTERNAL_ONLY1
81 /** Move constructor...
82 */
83 Reference (Reference<reference_type> && handle) noexcept
84 : m_pBody (handle.m_pBody)
85 {
86 handle.m_pBody = nullptr;
87 }
88#endif
89
90#if defined LIBO_INTERNAL_ONLY1
91 /** Up-casting conversion constructor: Copies interface reference.
92
93 Does not work for up-casts to ambiguous bases.
94
95 @param rRef another reference
96 */
97 template< class derived_type >
98 inline Reference(
99 const Reference< derived_type > & rRef,
100 std::enable_if_t<std::is_base_of_v<reference_type, derived_type>, int> = 0 )
101 : m_pBody (rRef.get())
102 {
103 if (m_pBody)
104 m_pBody->acquire();
105 }
106#endif
107
108 /** Destructor...
109 */
110 ~Reference() COVERITY_NOEXCEPT_FALSE
111 {
112 if (m_pBody)
4
Assuming field 'm_pBody' is non-null
5
Taking true branch
113 m_pBody->release();
6
Calling 'VclReferenceBase::release'
10
Returning; memory was released
114 }
115
116 /** Set...
117 Similar to assignment.
118 */
119 Reference<reference_type> &
120 SAL_CALL set (reference_type * pBody)
121 {
122 if (pBody)
123 pBody->acquire();
124 reference_type * const pOld = m_pBody;
125 m_pBody = pBody;
126 if (pOld)
127 pOld->release();
128 return *this;
129 }
130
131 /** Assignment.
132 Unbinds this instance from its body (if bound) and
133 bind it to the body represented by the handle.
134 */
135 Reference<reference_type> &
136 SAL_CALL operator= (const Reference<reference_type> & handle)
137 {
138 return set( handle.m_pBody );
139 }
140
141#ifdef LIBO_INTERNAL_ONLY1
142 /** Assignment.
143 * Unbinds this instance from its body (if bound),
144 * bind it to the body represented by the handle, and
145 * set the body represented by the handle to nullptr.
146 */
147 Reference<reference_type> &
148 operator= (Reference<reference_type> && handle)
149 {
150 // self-movement guts ourself
151 if (m_pBody)
152 m_pBody->release();
153 m_pBody = handle.m_pBody;
154 handle.m_pBody = nullptr;
155 return *this;
156 }
157#endif
158
159 /** Assignment...
160 */
161 Reference<reference_type> &
162 SAL_CALL operator= (reference_type * pBody)
163 {
164 return set( pBody );
165 }
166
167 /** Unbind the body from this handle.
168 Note that for a handle representing a large body,
169 "handle.clear().set(new body());" _might_
170 perform a little bit better than "handle.set(new body());",
171 since in the second case two large objects exist in memory
172 (the old body and the new body).
173 */
174 Reference<reference_type> & SAL_CALL clear()
175 {
176 if (m_pBody)
177 {
178 reference_type * const pOld = m_pBody;
179 m_pBody = NULL__null;
180 pOld->release();
181 }
182 return *this;
183 }
184
185
186 /** Get the body. Can be used instead of operator->().
187 I.e. handle->someBodyOp() and handle.get()->someBodyOp()
188 are the same.
189 */
190 reference_type * SAL_CALL get() const
191 {
192 return m_pBody;
193 }
194
195
196 /** Probably most common used: handle->someBodyOp().
197 */
198 reference_type * SAL_CALL operator->() const
199 {
200 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 200, __extension__ __PRETTY_FUNCTION__))
;
201 return m_pBody;
202 }
203
204
205 /** Allows (*handle).someBodyOp().
206 */
207 reference_type & SAL_CALL operator*() const
208 {
209 assert(m_pBody != NULL)(static_cast <bool> (m_pBody != __null) ? void (0) : __assert_fail
("m_pBody != NULL", "/home/maarten/src/libreoffice/core/include/rtl/ref.hxx"
, 209, __extension__ __PRETTY_FUNCTION__))
;
210 return *m_pBody;
211 }
212
213
214 /** Returns True if the handle does point to a valid body.
215 */
216 bool SAL_CALL is() const
217 {
218 return (m_pBody != NULL__null);
219 }
220
221#if defined LIBO_INTERNAL_ONLY1
222 /** Returns True if the handle does point to a valid body.
223 */
224 explicit operator bool() const
225 {
226 return is();
227 }
228#endif
229
230 /** Returns True if this points to pBody.
231 */
232 bool SAL_CALL operator== (const reference_type * pBody) const
233 {
234 return (m_pBody == pBody);
235 }
236
237
238 /** Returns True if handle points to the same body.
239 */
240 bool
241 SAL_CALL operator== (const Reference<reference_type> & handle) const
242 {
243 return (m_pBody == handle.m_pBody);
244 }
245
246
247 /** Needed to place References into STL collection.
248 */
249 bool
250 SAL_CALL operator!= (const Reference<reference_type> & handle) const
251 {
252 return (m_pBody != handle.m_pBody);
253 }
254
255
256 /** Needed to place References into STL collection.
257 */
258 bool
259 SAL_CALL operator< (const Reference<reference_type> & handle) const
260 {
261 return (m_pBody < handle.m_pBody);
262 }
263
264
265 /** Needed to place References into STL collection.
266 */
267 bool
268 SAL_CALL operator> (const Reference<reference_type> & handle) const
269 {
270 return (m_pBody > handle.m_pBody);
271 }
272};
273
274} // namespace rtl
275
276#if defined LIBO_INTERNAL_ONLY1
277namespace std
278{
279
280/// @cond INTERNAL
281/**
282 Make rtl::Reference hashable by default for use in STL containers.
283
284 @since LibreOffice 6.3
285*/
286template<typename T>
287struct hash<::rtl::Reference<T>>
288{
289 std::size_t operator()(::rtl::Reference<T> const & s) const
290 { return std::size_t(s.get()); }
291};
292/// @endcond
293
294}
295
296#endif
297
298#endif /* ! INCLUDED_RTL_REF_HXX */
299
300/* vim:set shiftwidth=4 softtabstop=4 expandtab: */

/home/maarten/src/libreoffice/core/include/vcl/vclreferencebase.hxx

1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19#ifndef INCLUDED_VCL_Reference_HXX
20#define INCLUDED_VCL_Reference_HXX
21
22#include <vcl/dllapi.h>
23#include <osl/interlck.h>
24
25class VCL_DLLPUBLIC__attribute__ ((visibility("default"))) VclReferenceBase
26{
27 mutable oslInterlockedCount mnRefCnt;
28
29 template<typename T> friend class VclPtr;
30
31public:
32 void acquire() const
33 {
34 osl_atomic_increment(&mnRefCnt)__sync_add_and_fetch((&mnRefCnt), 1);
35 }
36
37 void release() const
38 {
39 if (osl_atomic_decrement(&mnRefCnt)__sync_sub_and_fetch((&mnRefCnt), 1) == 0)
7
Assuming the condition is true
8
Taking true branch
40 delete this;
9
Memory is released
41 }
42#ifdef DBG_UTIL
43#ifndef _WIN32
44 sal_Int32 getRefCount() const { return mnRefCnt; }
45#endif
46#endif
47
48
49private:
50 VclReferenceBase(const VclReferenceBase&) = delete;
51 VclReferenceBase& operator=(const VclReferenceBase&) = delete;
52
53 bool mbDisposed : 1;
54
55protected:
56 VclReferenceBase();
57protected:
58 virtual ~VclReferenceBase();
59
60protected:
61 virtual void dispose();
62
63public:
64 void disposeOnce();
65 bool isDisposed() const { return mbDisposed; }
66
67};
68#endif