Skip to content

Commit

Permalink
Implement #1813 for C++ (#1814)
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardnormier authored Feb 20, 2024
1 parent 67d81c5 commit 046377b
Show file tree
Hide file tree
Showing 30 changed files with 93 additions and 156 deletions.
21 changes: 2 additions & 19 deletions cpp/include/Ice/LocalException.h
Original file line number Diff line number Diff line change
Expand Up @@ -1196,25 +1196,13 @@ class ICE_CLASS(ICE_API) IllegalIdentityException : public LocalExceptionHelper<
{
}

/**
* One-shot constructor to initialize all data members.
* The file and line number are required for all local exceptions.
* @param file The file name in which the exception was raised, typically __FILE__.
* @param line The line number at which the exception was raised, typically __LINE__.
* @param id The illegal identity.
*/
IllegalIdentityException(const char* file, int line, const Identity& id) : LocalExceptionHelper<IllegalIdentityException, LocalException>(file, line),
id(id)
{
}

/**
* Obtains a tuple containing all of the exception's data members.
* @return The data members in a tuple.
*/
std::tuple<const ::Ice::Identity&> ice_tuple() const
std::tuple<> ice_tuple() const
{
return std::tie(id);
return std::tie();
}

/**
Expand All @@ -1227,11 +1215,6 @@ class ICE_CLASS(ICE_API) IllegalIdentityException : public LocalExceptionHelper<
* @param stream The target stream.
*/
ICE_MEMBER(ICE_API) virtual void ice_print(::std::ostream& stream) const override;

/**
* The illegal identity.
*/
::Ice::Identity id;
};

/**
Expand Down
21 changes: 21 additions & 0 deletions cpp/src/Ice/CheckIdentity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#include "Ice/Identity.h"
#include "Ice/LocalException.h"

namespace Ice
{

// An identity with an empty name is illegal for all Ice APIs: no Ice API returns such an identity, and no Ice API
// accepts such an identity argument.
inline void checkIdentity(const Identity& identity, const char* file, int line)
{
if (identity.name.empty())
{
throw IllegalIdentityException(file, line);
}
}

}
16 changes: 4 additions & 12 deletions cpp/src/Ice/ConnectionI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Ice/ReferenceFactory.h> // For createProxy().
#include <Ice/ProxyFactory.h> // For createProxy().
#include <Ice/BatchRequestQueue.h>
#include "CheckIdentity.h"

#ifdef ICE_HAS_BZIP2
# include <bzlib.h>
Expand Down Expand Up @@ -1282,18 +1283,9 @@ Ice::ConnectionI::getEndpoint() const noexcept
optional<ObjectPrx>
Ice::ConnectionI::createProxy(const Identity& ident) const
{
// Create a reference and return a reverse proxy for this reference.
ReferencePtr ref =
_instance->referenceFactory()->create(ident, const_cast<ConnectionI*>(this)->shared_from_this());

if (ref)
{
return ObjectPrx::_fromReference(std::move(ref));
}
else
{
return std::nullopt;
}
checkIdentity(ident, __FILE__, __LINE__);
return ObjectPrx::_fromReference(
_instance->referenceFactory()->create(ident, const_cast<ConnectionI*>(this)->shared_from_this()));
}

void
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/Ice/Exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ void
Ice::IllegalIdentityException::ice_print(ostream& out) const
{
Exception::ice_print(out);
out << ":\nillegal identity: `" << identityToString(id, ToStringMode::Unicode) << "'";
out << ":\nan identity with an empty name is not allowed";
}

void
Expand Down
3 changes: 3 additions & 0 deletions cpp/src/Ice/Initialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <Ice/PluginManagerI.h>
#include <Ice/StringUtil.h>
#include <Ice/StringConverter.h>
#include "CheckIdentity.h"

#include <mutex>

Expand Down Expand Up @@ -515,12 +516,14 @@ Ice::stringToIdentity(const string& s)
}
}

checkIdentity(ident, __FILE__, __LINE__);
return ident;
}

string
Ice::identityToString(const Identity& ident, ToStringMode toStringMode)
{
checkIdentity(ident, __FILE__, __LINE__);
if(ident.category.empty())
{
return escapeString(ident.name, "/", toStringMode);
Expand Down
9 changes: 8 additions & 1 deletion cpp/src/Ice/InputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,14 @@ Ice::InputStream::readReference()

Identity ident;
read(ident);
return _instance->referenceFactory()->create(ident, this);
if (ident.name.empty())
{
return nullptr;
}
else
{
return _instance->referenceFactory()->create(ident, this);
}
}

Int
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/Ice/Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include <Ice/UUID.h>

#include "Ice/ProxyFunctions.h"
#include "CheckIdentity.h"

#include <stdio.h>
#include <list>
Expand Down Expand Up @@ -558,10 +559,7 @@ IceInternal::Instance::createAdmin(const ObjectAdapterPtr& adminAdapter, const I
throw CommunicatorDestroyedException(__FILE__, __LINE__);
}

if(adminIdentity.name.empty())
{
throw Ice::IllegalIdentityException(__FILE__, __LINE__, adminIdentity);
}
checkIdentity(adminIdentity, __FILE__, __LINE__);

if(_adminAdapter)
{
Expand Down
24 changes: 9 additions & 15 deletions cpp/src/Ice/ObjectAdapterI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <Ice/PropertyNames.h>
#include <Ice/ConsoleUtil.h>
#include "Ice/ProxyFunctions.h"
#include "CheckIdentity.h"

#ifdef _WIN32
# include <sys/timeb.h>
Expand All @@ -43,13 +44,6 @@ using namespace IceInternal;

namespace
{
inline void checkIdentity(const Identity& ident)
{
if(ident.name.empty())
{
throw IllegalIdentityException(__FILE__, __LINE__, ident);
}
}

inline void checkServant(const shared_ptr<Object>& servant)
{
Expand Down Expand Up @@ -381,7 +375,7 @@ Ice::ObjectAdapterI::addFacet(const shared_ptr<Object>& object, const Identity&

checkForDeactivation();
checkServant(object);
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

_servantManager->addServant(object, ident, facet);

Expand Down Expand Up @@ -425,7 +419,7 @@ Ice::ObjectAdapterI::removeFacet(const Identity& ident, const string& facet)
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return _servantManager->removeServant(ident, facet);
}
Expand All @@ -436,7 +430,7 @@ Ice::ObjectAdapterI::removeAllFacets(const Identity& ident)
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return _servantManager->removeAllFacets(ident);
}
Expand All @@ -463,7 +457,7 @@ Ice::ObjectAdapterI::findFacet(const Identity& ident, const string& facet) const
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return _servantManager->findServant(ident, facet);
}
Expand All @@ -474,7 +468,7 @@ Ice::ObjectAdapterI::findAllFacets(const Identity& ident) const
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return _servantManager->findAllFacets(ident);
}
Expand Down Expand Up @@ -536,7 +530,7 @@ Ice::ObjectAdapterI::createProxy(const Identity& ident) const
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return newProxy(ident, "");
}
Expand All @@ -547,7 +541,7 @@ Ice::ObjectAdapterI::createDirectProxy(const Identity& ident) const
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return newDirectProxy(ident, "");
}
Expand All @@ -558,7 +552,7 @@ Ice::ObjectAdapterI::createIndirectProxy(const Identity& ident) const
lock_guard lock(_mutex);

checkForDeactivation();
checkIdentity(ident);
checkIdentity(ident, __FILE__, __LINE__);

return newIndirectProxy(ident, "", _id);
}
Expand Down
6 changes: 2 additions & 4 deletions cpp/src/Ice/Proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "ReferenceFactory.h"
#include "RequestHandlerCache.h"
#include "ConnectionI.h"
#include "CheckIdentity.h"

using namespace std;
using namespace Ice;
Expand Down Expand Up @@ -80,10 +81,7 @@ Ice::ObjectPrx::ice_getIdentity() const
ObjectPrx
Ice::ObjectPrx::ice_identity(const Identity& newIdentity) const
{
if (newIdentity.name.empty())
{
throw IllegalIdentityException(__FILE__, __LINE__);
}
checkIdentity(newIdentity, __FILE__, __LINE__);
if (newIdentity == _reference->getIdentity())
{
return *this;
Expand Down
32 changes: 10 additions & 22 deletions cpp/src/Ice/ReferenceFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ IceInternal::ReferenceFactory::create(const string& str, const string& propertyP
{
if(str.empty())
{
return 0;
return nullptr;
}

const string delim = " \t\r\n";
Expand Down Expand Up @@ -133,37 +133,29 @@ IceInternal::ReferenceFactory::create(const string& str, const string& propertyP
throw ProxyParseException(__FILE__, __LINE__, "no identity in `" + s + "'");
}

//
// Parsing the identity may raise IdentityParseException.
//
Identity ident = Ice::stringToIdentity(idstr);

if(ident.name.empty())
if (idstr.empty())
{
//
// An identity with an empty name and a non-empty
// category is illegal.
//
if(!ident.category.empty())
{
throw IllegalIdentityException(__FILE__, __LINE__, ident);
}
//
// Treat a stringified proxy containing two double
// quotes ("") the same as an empty string, i.e.,
// a null proxy, but only if nothing follows the
// quotes.
//
else if(s.find_first_not_of(delim, end) != string::npos)
if(s.find_first_not_of(delim, end) != string::npos)
{
throw ProxyParseException(__FILE__, __LINE__, "invalid characters after identity in `" + s + "'");
}
else
{
return 0;
return nullptr;
}
}

//
// Parsing the identity may raise IdentityParseException.
//
Identity ident = Ice::stringToIdentity(idstr);

string facet;
Reference::Mode mode = Reference::ModeTwoway;
bool secure = false;
Expand Down Expand Up @@ -533,11 +525,7 @@ IceInternal::ReferenceFactory::create(const Identity& ident, InputStream* s)
// Don't read the identity here. Operations calling this
// constructor read the identity, and pass it as a parameter.
//

if(ident.name.empty() && ident.category.empty())
{
return 0;
}
assert(!ident.name.empty());

//
// For compatibility with the old FacetPath.
Expand Down
1 change: 0 additions & 1 deletion cpp/test/Ice/exceptions/AllTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ allTests(Test::TestHelper* helper)
}
catch(const Ice::IllegalIdentityException& ex)
{
test(ex.id.name == "");
if(printException)
{
Ice::Print printer(communicator->getLogger());
Expand Down
10 changes: 10 additions & 0 deletions cpp/test/Ice/proxy/AllTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,16 @@ allTests(Test::TestHelper* helper)
id2 = Ice::stringToIdentity(idStr);
test(id == id2);

try
{
// Ice APIs can't return all illegal identity.
id = Ice::stringToIdentity("");
assert(false);
}
catch (const Ice::IllegalIdentityException&)
{
}

// Input string with various pitfalls
id = Ice::stringToIdentity("\\342\\x82\\254\\60\\x9\\60\\");
test(id.name == "\xE2\x82\xAC\60\t0\\" && id.category.empty());
Expand Down
9 changes: 1 addition & 8 deletions js/src/Ice/LocalException.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,10 @@ export namespace Ice
}

/**
* This exception is raised if an illegal identity is encountered.
* This exception is raised if an identity with an empty name is encountered.
*/
class IllegalIdentityException extends LocalException
{
/**
* One-shot constructor to initialize all data members.
* @param id The illegal identity.
* @param ice_cause The error that cause this exception.
*/
constructor(id?:Identity, ice_cause?:string|Error);
id:Identity;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions js/src/Ice/LocalException.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,10 +499,9 @@ Ice.ProxyParseException = class extends Ice.LocalException
**/
Ice.IllegalIdentityException = class extends Ice.LocalException
{
constructor(id = new Ice.Identity(), _cause = "")
constructor(_cause = "")
{
super(_cause);
this.id = id;
}

static get _parent()
Expand Down
Loading

0 comments on commit 046377b

Please sign in to comment.