include/o3tl/restoreguard.hxx | 40 ++++++++++++++++++++++++++++++++++++++++ vcl/source/fontsubset/cff.cxx | 7 +++---- 2 files changed, 43 insertions(+), 4 deletions(-)
New commits: commit 2469c623d7985e9e5db54ed8f96df02219def74d Author: Stephan Bergmann <sberg...@redhat.com> AuthorDate: Mon Nov 7 10:58:08 2022 +0100 Commit: Stephan Bergmann <sberg...@redhat.com> CommitDate: Tue Nov 8 07:14:21 2022 +0100 Introduce o3tl::RestoreGuard Change-Id: Ic0c1a73668e990c91ef6e90e8c01e669761fe356 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/142403 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sberg...@redhat.com> diff --git a/include/o3tl/restoreguard.hxx b/include/o3tl/restoreguard.hxx new file mode 100644 index 000000000000..19075be3dcea --- /dev/null +++ b/include/o3tl/restoreguard.hxx @@ -0,0 +1,40 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include <sal/config.h> + +// A convenience guard class that captures a given object's value on guard construction and restores +// it on guard destruction: +namespace o3tl +{ +template <typename T> class RestoreGuard +{ +public: + RestoreGuard(T& object) + : object_(object) + , value_(object) + { + } + + ~RestoreGuard() { object_ = value_; } + +private: + RestoreGuard(RestoreGuard&) = delete; + RestoreGuard(RestoreGuard&&) = delete; + void operator=(RestoreGuard&) = delete; + void operator=(RestoreGuard&&) = delete; + + T& object_; + T value_; +}; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/vcl/source/fontsubset/cff.cxx b/vcl/source/fontsubset/cff.cxx index 6d1cac5040b5..c398ccb18b98 100644 --- a/vcl/source/fontsubset/cff.cxx +++ b/vcl/source/fontsubset/cff.cxx @@ -24,6 +24,7 @@ #include <fontsubset.hxx> +#include <o3tl/restoreguard.hxx> #include <o3tl/safeint.hxx> #include <strhelper.hxx> #include <sal/log.hxx> @@ -1541,8 +1542,8 @@ const char* CffSubsetterContext::getString( int nStringID) return pStringIds[ nStringID]; // else get the string from the StringIndex table - const U8* pReadPtr = mpReadPtr; - const U8* pReadEnd = mpReadEnd; + o3tl::RestoreGuard pReadPtr(mpReadPtr); + o3tl::RestoreGuard pReadEnd(mpReadEnd); nStringID -= nStdStrings; int nLen = seekIndexData( mnStringIdxBase, nStringID); // assert( nLen >= 0); @@ -1559,8 +1560,6 @@ const char* CffSubsetterContext::getString( int nStringID) aNameBuf[i] = *(mpReadPtr++); aNameBuf[ nLen] = '\0'; } - mpReadPtr = pReadPtr; - mpReadEnd = pReadEnd; return aNameBuf; }