Bug Summary

File:home/maarten/src/libreoffice/core/include/rtl/ref.hxx
Warning:line 77, column 13
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 uiobject.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 -isystem /usr/include/libxml2 -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 SW_DLLIMPLEMENTATION -D SWUI_DLL_NAME="libswuilo.so" -D SYSTEM_LIBXML -D EXCEPTIONS_ON -D LIBO_INTERNAL_ONLY -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/i18n -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/icu/source/common -I /home/maarten/src/libreoffice/core/external/boost/include -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/boost -I /home/maarten/src/libreoffice/core/sw/source/core/inc -I /home/maarten/src/libreoffice/core/sw/source/filter/inc -I /home/maarten/src/libreoffice/core/sw/source/uibase/inc -I /home/maarten/src/libreoffice/core/sw/inc -I /home/maarten/src/libreoffice/core/workdir/SdiTarget/sw/sdi -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/workdir/CustomTarget/officecfg/registry -I /home/maarten/src/libreoffice/core/workdir/CustomTarget/sw/generated -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/udkapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/offapi/normal -I /home/maarten/src/libreoffice/core/workdir/UnoApiHeadersTarget/oovbaapi/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/sw/source/uibase/uitest/uiobject.cxx

/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.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
10#include <memory>
11#include <uiobject.hxx>
12#include <edtwin.hxx>
13#include <view.hxx>
14#include <wrtsh.hxx>
15#include <navipi.hxx>
16#include <ndtxt.hxx>
17#include <sfx2/sidebar/Sidebar.hxx>
18#include <sfx2/viewfrm.hxx>
19
20#include <AnnotationWin.hxx>
21#include <comphelper/string.hxx>
22#include <editeng/editeng.hxx>
23#include <editeng/editview.hxx>
24
25SwEditWinUIObject::SwEditWinUIObject(const VclPtr<SwEditWin>& xEditWin):
26 WindowUIObject(xEditWin),
27 mxEditWin(xEditWin)
28{
29}
30
31namespace {
32
33SwWrtShell& getWrtShell(VclPtr<SwEditWin> const & xEditWin)
34{
35 return xEditWin->GetView().GetWrtShell();
36}
37
38}
39
40StringMap SwEditWinUIObject::get_state()
41{
42 StringMap aMap = WindowUIObject::get_state();
43
44 aMap["SelectedText"] = mxEditWin->GetView().GetSelectionText();
45
46 sal_uInt16 nPageNum = 0;
47 sal_uInt16 nVirtPageNum = 0;
48 SwWrtShell& rWrtShell = getWrtShell(mxEditWin);
49 rWrtShell.GetPageNum(nPageNum, nVirtPageNum);
50 aMap["CurrentPage"] = OUString::number(nPageNum);
51 rWrtShell.GetPageNum(nPageNum, nVirtPageNum, false);
52 aMap["TopVisiblePage"] = OUString::number(nPageNum);
53
54 sal_uInt16 nPages = rWrtShell.GetPageCnt();
55 aMap["Pages"] = OUString::number(nPages);
56
57 aMap["StartWord"] = OUString::boolean(rWrtShell.IsStartWord());
58 aMap["EndWord"] = OUString::boolean(rWrtShell.IsEndWord());
59 aMap["StartSentence"] = OUString::boolean(rWrtShell.IsStartSentence());
60 aMap["EndSentence"] = OUString::boolean(rWrtShell.IsEndSentence());
61 aMap["StartPara"] = OUString::boolean(rWrtShell.IsSttPara());
62 aMap["EndPara"] = OUString::boolean(rWrtShell.IsEndPara());
63 aMap["StartDoc"] = OUString::boolean(rWrtShell.IsStartOfDoc());
64 aMap["EndDoc"] = OUString::boolean(rWrtShell.IsEndOfDoc());
65
66 return aMap;
67}
68
69void SwEditWinUIObject::execute(const OUString& rAction,
70 const StringMap& rParameters)
71{
72 if (rAction == "SET")
73 {
74 if (rParameters.find("ZOOM") != rParameters.end())
75 {
76 auto itr = rParameters.find("ZOOM");
77 OUString aVal = itr->second;
78 sal_Int32 nVal = aVal.toInt32();
79 mxEditWin->GetView().SetZoom(SvxZoomType::PERCENT, nVal);
80 }
81 }
82 else if (rAction == "GOTO")
83 {
84 if (rParameters.find("PAGE") != rParameters.end())
85 {
86 auto itr = rParameters.find("PAGE");
87 OUString aVal = itr->second;
88 sal_Int32 nVal = aVal.toInt32();
89 getWrtShell(mxEditWin).GotoPage(nVal, false);
90 }
91 }
92 else if (rAction == "SELECT")
93 {
94 if (rParameters.find("START_POS") != rParameters.end())
95 {
96 auto itr = rParameters.find("START_POS");
97 OUString aStartPos = itr->second;
98 sal_Int32 nStartPos = aStartPos.toInt32();
99
100 itr = rParameters.find("END_POS");
101 assert(itr != rParameters.end())(static_cast <bool> (itr != rParameters.end()) ? void (
0) : __assert_fail ("itr != rParameters.end()", "/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
, 101, __extension__ __PRETTY_FUNCTION__))
;
102 OUString aEndPos = itr->second;
103 sal_Int32 nEndPos = aEndPos.toInt32();
104
105 auto & shell = getWrtShell(mxEditWin);
106 sal_Int32 len;
107 if (auto const text = shell.GetCursor_()->GetPoint()->nNode.GetNode().GetTextNode()) {
108 len = text->GetText().getLength();
109 } else {
110 len = 0;
111 }
112 SAL_WARN_IF(do { if (true && (nStartPos < 0 || nStartPos > len
|| nEndPos < 0 || nEndPos > len)) { switch (sal_detail_log_report
(::SAL_DETAIL_LOG_LEVEL_WARN, "sw.ui")) { case SAL_DETAIL_LOG_ACTION_IGNORE
: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail
::getResult( ::sal::detail::StreamStart() << "SELECT START/END_POS "
<< nStartPos << ".." << nEndPos << " outside 0.."
<< len) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.ui"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len) ==
1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
113 nStartPos < 0 || nStartPos > len || nEndPos < 0 || nEndPos > len, "sw.ui",do { if (true && (nStartPos < 0 || nStartPos > len
|| nEndPos < 0 || nEndPos > len)) { switch (sal_detail_log_report
(::SAL_DETAIL_LOG_LEVEL_WARN, "sw.ui")) { case SAL_DETAIL_LOG_ACTION_IGNORE
: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail
::getResult( ::sal::detail::StreamStart() << "SELECT START/END_POS "
<< nStartPos << ".." << nEndPos << " outside 0.."
<< len) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.ui"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len) ==
1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
114 "SELECT START/END_POS " << nStartPos << ".." << nEndPos << " outside 0.." << len)do { if (true && (nStartPos < 0 || nStartPos > len
|| nEndPos < 0 || nEndPos > len)) { switch (sal_detail_log_report
(::SAL_DETAIL_LOG_LEVEL_WARN, "sw.ui")) { case SAL_DETAIL_LOG_ACTION_IGNORE
: break; case SAL_DETAIL_LOG_ACTION_LOG: if (sizeof ::sal::detail
::getResult( ::sal::detail::StreamStart() << "SELECT START/END_POS "
<< nStartPos << ".." << nEndPos << " outside 0.."
<< len) == 1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN
), ("sw.ui"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; break; case SAL_DETAIL_LOG_ACTION_FATAL
: if (sizeof ::sal::detail::getResult( ::sal::detail::StreamStart
() << "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len) ==
1) { ::sal_detail_log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"
), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), ::sal::detail::unwrapStream( ::sal::detail::
StreamStart() << "SELECT START/END_POS " << nStartPos
<< ".." << nEndPos << " outside 0.." <<
len), 0); } else { ::std::ostringstream sal_detail_stream; sal_detail_stream
<< "SELECT START/END_POS " << nStartPos <<
".." << nEndPos << " outside 0.." << len; ::
sal::detail::log( (::SAL_DETAIL_LOG_LEVEL_WARN), ("sw.ui"), (
"/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "114" ": "), sal_detail_stream, 0); }; std::abort(); break
; } } } while (false)
;
115 shell.SelectText(
116 std::clamp(nStartPos, sal_Int32(0), len), std::clamp(nEndPos, sal_Int32(0), len));
117 }
118 }
119 else if (rAction == "SIDEBAR")
120 {
121 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
122 DBG_ASSERT(pViewFrm, "SwEditWinUIObject::execute: no viewframe")do { if (true && (!(pViewFrm))) { sal_detail_logFormat
((SAL_DETAIL_LOG_LEVEL_WARN), ("legacy.tools"), ("/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
":" "122" ": "), "%s", "SwEditWinUIObject::execute: no viewframe"
); } } while (false)
;
123 pViewFrm->ShowChildWindow(SID_SIDEBAR(10000 + 336));
124
125 if (rParameters.find("PANEL") != rParameters.end())
126 {
127 auto itr = rParameters.find("PANEL");
128 OUString aVal = itr->second;
129 ::sfx2::sidebar::Sidebar::ShowPanel(aVal, pViewFrm->GetFrame().GetFrameInterface());
130 }
131 }
132 else
133 WindowUIObject::execute(rAction, rParameters);
134}
135
136OUString SwEditWinUIObject::get_name() const
137{
138 return "SwEditWinUIObject";
139}
140
141std::unique_ptr<UIObject> SwEditWinUIObject::create(vcl::Window* pWindow)
142{
143 SwEditWin* pEditWin = dynamic_cast<SwEditWin*>(pWindow);
144 assert(pEditWin)(static_cast <bool> (pEditWin) ? void (0) : __assert_fail
("pEditWin", "/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
, 144, __extension__ __PRETTY_FUNCTION__))
;
145 return std::unique_ptr<UIObject>(new SwEditWinUIObject(pEditWin));
146}
147
148SwNavigationPIUIObject::SwNavigationPIUIObject(const VclPtr<SwNavigationPI>& xSwNavigationPI):
149 WindowUIObject(xSwNavigationPI),
150 mxSwNavigationPI(xSwNavigationPI)
151{
152}
153
154StringMap SwNavigationPIUIObject::get_state()
155{
156 StringMap aMap = WindowUIObject::get_state();
157
158 aMap["selectioncount"] = OUString::number(mxSwNavigationPI->m_xContentTree->count_selected_rows());
159 aMap["selectedtext"] = mxSwNavigationPI->m_xContentTree->get_selected_text();
160
161 return aMap;
162}
163
164void SwNavigationPIUIObject::execute(const OUString& rAction,
165 const StringMap& rParameters)
166{
167 if (rAction == "ROOT")
168 {
169 mxSwNavigationPI->m_xContentTree->grab_focus();
170 mxSwNavigationPI->ToolBoxSelectHdl("root");
171 }
172 else
173 WindowUIObject::execute(rAction, rParameters);
174}
175
176std::unique_ptr<UIObject> SwNavigationPIUIObject::create(vcl::Window* pWindow)
177{
178 SwNavigationPI* pSwNavigationPI = dynamic_cast<SwNavigationPI*>(pWindow);
179 assert(pSwNavigationPI)(static_cast <bool> (pSwNavigationPI) ? void (0) : __assert_fail
("pSwNavigationPI", "/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
, 179, __extension__ __PRETTY_FUNCTION__))
;
180 return std::unique_ptr<UIObject>(new SwNavigationPIUIObject(pSwNavigationPI));
181}
182
183OUString SwNavigationPIUIObject::get_name() const
184{
185 return "SwNavigationPIUIObject";
186}
187
188CommentUIObject::CommentUIObject(const VclPtr<sw::annotation::SwAnnotationWin>& xCommentUIObject):
189 WindowUIObject(xCommentUIObject),
4
Calling implicit destructor for 'VclPtr<vcl::Window>'
5
Calling '~Reference'
12
Returning from '~Reference'
13
Returning from destructor for 'VclPtr<vcl::Window>'
190 mxCommentUIObject(xCommentUIObject)
14
Calling implicit copy constructor for 'VclPtr<sw::annotation::SwAnnotationWin>'
15
Calling copy constructor for 'Reference<sw::annotation::SwAnnotationWin>'
191{
192}
193
194StringMap CommentUIObject::get_state()
195{
196 StringMap aMap = WindowUIObject::get_state();
197 aMap["Author"] = mxCommentUIObject->GetAuthor();
198 aMap["ReadOnly"] = OUString::boolean(mxCommentUIObject->IsReadOnly());
199 aMap["Resolved"] = OUString::boolean(mxCommentUIObject->IsResolved());
200 aMap["Visible"] = OUString::boolean(mxCommentUIObject->IsVisible());
201
202 aMap["Text"] = mxCommentUIObject->GetOutliner()->GetEditEngine().GetText();
203 aMap["SelectedText"] = mxCommentUIObject->GetOutlinerView()->GetEditView().GetSelected();
204 return aMap;
205}
206
207void CommentUIObject::execute(const OUString& rAction,
208 const StringMap& rParameters)
209{
210 if (rAction == "SELECT")
211 {
212 if (rParameters.find("FROM") != rParameters.end() &&
213 rParameters.find("TO") != rParameters.end())
214 {
215 long nMin = rParameters.find("FROM")->second.toInt32();
216 long nMax = rParameters.find("TO")->second.toInt32();
217 ESelection aNewSelection( 0 , nMin, mxCommentUIObject->GetOutliner()->GetParagraphCount()-1, nMax );
218 mxCommentUIObject->GetOutlinerView()->SetSelection( aNewSelection );
219 }
220 }
221 else if (rAction == "LEAVE")
222 {
223 mxCommentUIObject->SwitchToFieldPos();
224 }
225 else if (rAction == "HIDE")
226 {
227 mxCommentUIObject->HideNote();
228 }
229 else if (rAction == "SHOW")
230 {
231 mxCommentUIObject->ShowNote();
232 }
233 else if (rAction == "DELETE")
234 {
235 mxCommentUIObject->Delete();
236 }
237 else if (rAction == "RESOLVE")
238 {
239 mxCommentUIObject->SetResolved(true);
240 }
241 else
242 WindowUIObject::execute(rAction, rParameters);
243}
244
245std::unique_ptr<UIObject> CommentUIObject::create(vcl::Window* pWindow)
246{
247 sw::annotation::SwAnnotationWin* pCommentUIObject = dynamic_cast<sw::annotation::SwAnnotationWin*>(pWindow);
248 assert(pCommentUIObject)(static_cast <bool> (pCommentUIObject) ? void (0) : __assert_fail
("pCommentUIObject", "/home/maarten/src/libreoffice/core/sw/source/uibase/uitest/uiobject.cxx"
, 248, __extension__ __PRETTY_FUNCTION__))
;
1
Assuming 'pCommentUIObject' is non-null
2
'?' condition is true
249 return std::unique_ptr<UIObject>(new CommentUIObject(pCommentUIObject));
3
Calling constructor for 'CommentUIObject'
250}
251
252OUString CommentUIObject::get_name() const
253{
254 return "CommentUIObject";
255}
256
257
258
259/* 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
15.1
Field 'm_pBody' is non-null
15.1
Field 'm_pBody' is non-null
15.1
Field 'm_pBody' is non-null
)
16
Taking true branch
77 m_pBody->acquire();
17
Use of memory after it is freed
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
5.1
Field 'm_pBody' is non-null
5.1
Field 'm_pBody' is non-null
5.1
Field 'm_pBody' is non-null
)
6
Taking true branch
113 m_pBody->release();
7
Calling 'VclReferenceBase::release'
11
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)
8
Assuming the condition is true
9
Taking true branch
40 delete this;
10
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