Files
libreoffice/binaryurp/source/bridgefactory.cxx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

193 lines
5.8 KiB
C++
Raw Normal View History

2011-06-02 16:49:28 +01:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2012-06-12 17:51:46 +01:00
/*
* 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/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <sal/config.h>
#include <algorithm>
#include <cassert>
#include <com/sun/star/bridge/BridgeExistsException.hpp>
#include <com/sun/star/connection/XConnection.hpp>
#include <com/sun/star/lang/IllegalArgumentException.hpp>
#include <com/sun/star/uno/Exception.hpp>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/uno/RuntimeException.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/uno/XInterface.hpp>
#include <cppuhelper/supportsservice.hxx>
New loplugin:unsignedcompare "Find explicit casts from signed to unsigned integer in comparison against unsigned integer, where the cast is presumably used to avoid warnings about signed vs. unsigned comparisons, and could thus be replaced with o3tl::make_unsigned for clairty." (compilerplugins/clang/unsignedcompare.cxx) o3tl::make_unsigned requires its argument to be non-negative, and there is a chance that some original code like static_cast<sal_uInt32>(n) >= c used the explicit cast to actually force a (potentially negative) value of sal_Int32 to be interpreted as an unsigned sal_uInt32, rather than using the cast to avoid a false "signed vs. unsigned comparison" warning in a case where n is known to be non-negative. It appears that restricting this plugin to non- equality comparisons (<, >, <=, >=) and excluding equality comparisons (==, !=) is a useful heuristic to avoid such false positives. The only remainging false positive I found was 0288c8ffecff4956a52b9147d441979941e8b87f "Rephrase cast from sal_Int32 to sal_uInt32". But which of course does not mean that there were no further false positivies that I missed. So this commit may accidentally introduce some false hits of the assert in o3tl::make_unsigned. At least, it passed a full (Linux ASan+UBSan --enable-dbgutil) `make check && make screenshot`. It is by design that o3tl::make_unsigned only accepts signed integer parameter types (and is not defined as a nop for unsigned ones), to avoid unnecessary uses which would in general be suspicious. But the STATIC_ARRAY_SELECT macro in include/oox/helper/helper.hxx is used with both signed and unsigned types, so needs a little oox::detail::make_unsigned helper function for now. (The ultimate fix being to get rid of the macro in the first place.) Change-Id: Ia4adc9f44c70ad1dfd608784cac39ee922c32175 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87556 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-01-27 09:30:39 +01:00
#include <o3tl/safeint.hxx>
#include <rtl/ref.hxx>
#include <sal/log.hxx>
#include <sal/types.h>
#include "bridge.hxx"
#include "bridgefactory.hxx"
namespace binaryurp {
void BridgeFactory::removeBridge(
css::uno::Reference< css::bridge::XBridge > const & bridge)
{
assert(bridge.is());
OUString n(bridge->getName());
osl::MutexGuard g(m_aMutex);
if (n.isEmpty())
{
unnamed_.erase(std::remove(unnamed_.begin(), unnamed_.end(), bridge), unnamed_.end());
}
else
{
BridgeMap::iterator i(named_.find(n));
if (i != named_.end() && i->second == bridge)
named_.erase(i);
}
}
BridgeFactory::BridgeFactory():
BridgeFactoryBase(m_aMutex)
{
}
BridgeFactory::~BridgeFactory() {}
OUString BridgeFactory::getImplementationName()
{
return "com.sun.star.comp.bridge.BridgeFactory";
}
sal_Bool BridgeFactory::supportsService(OUString const & ServiceName)
{
return cppu::supportsService(this, ServiceName);
}
css::uno::Sequence< OUString > BridgeFactory::getSupportedServiceNames()
{
return { "com.sun.star.bridge.BridgeFactory" };
}
css::uno::Reference< css::bridge::XBridge > BridgeFactory::createBridge(
OUString const & sName, OUString const & sProtocol,
css::uno::Reference< css::connection::XConnection > const & aConnection,
css::uno::Reference< css::bridge::XInstanceProvider > const &
anInstanceProvider)
{
rtl::Reference< Bridge > b;
{
osl::MutexGuard g(m_aMutex);
if (rBHelper.bDisposed) {
throw css::lang::DisposedException(
"BridgeFactory disposed",
getXWeak());
}
if (named_.find(sName) != named_.end()) {
throw css::bridge::BridgeExistsException(
sName, getXWeak());
}
if (sProtocol != "urp" || !aConnection.is()) {
throw css::lang::IllegalArgumentException(
("BridgeFactory::createBridge: sProtocol != urp ||"
" aConnection == null"),
getXWeak(), -1);
}
b.set(new Bridge(this, sName, aConnection, anInstanceProvider));
if (sName.isEmpty()) {
unnamed_.emplace_back(b.get());
} else {
named_[sName] = b.get();
}
}
b->start();
return b;
}
css::uno::Reference< css::bridge::XBridge > BridgeFactory::getBridge(
OUString const & sName)
{
osl::MutexGuard g(m_aMutex);
BridgeMap::iterator i(named_.find(sName));
return i == named_.end()
? css::uno::Reference< css::bridge::XBridge >() : i->second;
}
css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > >
BridgeFactory::getExistingBridges() {
osl::MutexGuard g(m_aMutex);
if (unnamed_.size() > SAL_MAX_INT32) {
throw css::uno::RuntimeException(
"BridgeFactory::getExistingBridges: too many",
getXWeak());
}
sal_Int32 n = static_cast< sal_Int32 >(unnamed_.size());
New loplugin:unsignedcompare "Find explicit casts from signed to unsigned integer in comparison against unsigned integer, where the cast is presumably used to avoid warnings about signed vs. unsigned comparisons, and could thus be replaced with o3tl::make_unsigned for clairty." (compilerplugins/clang/unsignedcompare.cxx) o3tl::make_unsigned requires its argument to be non-negative, and there is a chance that some original code like static_cast<sal_uInt32>(n) >= c used the explicit cast to actually force a (potentially negative) value of sal_Int32 to be interpreted as an unsigned sal_uInt32, rather than using the cast to avoid a false "signed vs. unsigned comparison" warning in a case where n is known to be non-negative. It appears that restricting this plugin to non- equality comparisons (<, >, <=, >=) and excluding equality comparisons (==, !=) is a useful heuristic to avoid such false positives. The only remainging false positive I found was 0288c8ffecff4956a52b9147d441979941e8b87f "Rephrase cast from sal_Int32 to sal_uInt32". But which of course does not mean that there were no further false positivies that I missed. So this commit may accidentally introduce some false hits of the assert in o3tl::make_unsigned. At least, it passed a full (Linux ASan+UBSan --enable-dbgutil) `make check && make screenshot`. It is by design that o3tl::make_unsigned only accepts signed integer parameter types (and is not defined as a nop for unsigned ones), to avoid unnecessary uses which would in general be suspicious. But the STATIC_ARRAY_SELECT macro in include/oox/helper/helper.hxx is used with both signed and unsigned types, so needs a little oox::detail::make_unsigned helper function for now. (The ultimate fix being to get rid of the macro in the first place.) Change-Id: Ia4adc9f44c70ad1dfd608784cac39ee922c32175 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/87556 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
2020-01-27 09:30:39 +01:00
if (named_.size() > o3tl::make_unsigned(SAL_MAX_INT32 - n)) {
throw css::uno::RuntimeException(
"BridgeFactory::getExistingBridges: too many",
getXWeak());
}
n = static_cast< sal_Int32 >(n + named_.size());
css::uno::Sequence< css::uno::Reference< css::bridge::XBridge > > s(n);
auto r = asNonConstRange(s);
sal_Int32 i = 0;
for (auto const& item : unnamed_)
r[i++] = item;
for (auto const& item : named_)
r[i++] = item.second;
return s;
}
void BridgeFactory::disposing() {
BridgeVector l1;
BridgeMap l2;
{
osl::MutexGuard g(m_aMutex);
l1.swap(unnamed_);
l2.swap(named_);
}
for (auto const& item : l1)
{
try {
css::uno::Reference<css::lang::XComponent>(
item, css::uno::UNO_QUERY_THROW)->dispose();
} catch (css::uno::Exception & e) {
SAL_WARN("binaryurp", "ignoring " << e);
}
}
for (auto const& item : l2)
{
try {
css::uno::Reference<css::lang::XComponent>(
item.second, css::uno::UNO_QUERY_THROW)->dispose();
} catch (css::uno::Exception & e) {
SAL_WARN("binaryurp", "ignoring " << e);
}
}
}
}
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
com_sun_star_comp_bridge_BridgeFactory_get_implementation(
css::uno::XComponentContext* , css::uno::Sequence<css::uno::Any> const&)
{
return cppu::acquire(new binaryurp::BridgeFactory);
}
2011-06-02 16:49:28 +01:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */