From: Eduardo Ramos Testillano Date: Thu, 20 Apr 2017 00:04:02 +0000 (+0200) Subject: Avoid use of static attribute in functions X-Git-Url: https://git.teslayout.com/public/public/public/?p=anna.git;a=commitdiff_plain;h=61c8b6e85a24b5b6b9e9bc40dbeda77726e57283 Avoid use of static attribute in functions Dynamic libraries could share the objects doing double free corruption on app stop. --- diff --git a/example/diameter/launcher/main.cpp b/example/diameter/launcher/main.cpp index 6f2a653..7b13e12 100644 --- a/example/diameter/launcher/main.cpp +++ b/example/diameter/launcher/main.cpp @@ -26,6 +26,8 @@ int main(int argc, const char** argv) { Launcher app; anna::http::functions::initialize(); + std::cout << "XXXXXXXXXXXXXXXXXXXXXXX " << anna::functions::hash("hola que tal") << std::endl; + try { anna::CommandLine& commandLine(anna::CommandLine::instantiate()); // General diff --git a/include/anna/core/functions.hpp b/include/anna/core/functions.hpp index 0ceb358..6b4a4e2 100644 --- a/include/anna/core/functions.hpp +++ b/include/anna/core/functions.hpp @@ -320,18 +320,6 @@ struct functions { */ static S64 hash(const std::string& str) throw() { return hash(str.c_str()); } - /** - Calcula la funcion hash exclusive de la cadena recibida como parametro. - \param str Cadena a la que aplicar la funcion hash exclusiva. - */ - static unsigned long exclusiveHash(const std::string& str) throw() { return st_stringExclusiveHash.calcule(str); } - - /** - Calcula la funcion hash exclusive de la cadena recibida como parametro. - \param str Cadena a la que aplicar la funcion hash exclusiva. - */ - static unsigned long exclusiveHash(const char* str) throw() { return st_stringExclusiveHash.calcule(std::string(str)); } - /** Devuelve la cadena que contiene el resultado de aplicar la especificacion \em format sobre el resto de los parametros. @@ -663,25 +651,6 @@ struct functions { return functions::componentByName (T::getClassName(), fromFile, fromLine); } - - /** - * Gets exclusive hash for string provided on integer range - * - * @param str String hashed - * - * @return Hash unique value - */ - static int exclusiveHashInt(const std::string& str) throw() { return st_string2intExclusiveHash.calcule(str); } - - /** - * Gets exclusive hash for string (char pointer) provided on integer range - * - * @param str String hashed - * - * @return Hash unique value - */ - static int exclusiveHashInt(const char* str) throw() { return st_string2intExclusiveHash.calcule(std::string(str)); } - /** Finds string at the end of another @@ -1144,10 +1113,6 @@ struct functions { * @param target Isup number octet string. */ static void codeIsupNumber(const isup_number_t & isupNumber, bool calledOrCalling, std::string & target) throw(RuntimeException); - -private: - static ExclusiveHash st_stringExclusiveHash; - static ExclusiveHash st_string2intExclusiveHash; }; } diff --git a/include/anna/diameter.comm/Engine.hpp b/include/anna/diameter.comm/Engine.hpp index 72e3ad9..bcb91ce 100644 --- a/include/anna/diameter.comm/Engine.hpp +++ b/include/anna/diameter.comm/Engine.hpp @@ -771,7 +771,6 @@ private: // CLIENT FUNCTIONALITY // ////////////////////////// - //typedef int clientSession_key; // exclusiveHash('ADDR:PORT|id') typedef std::string clientSession_key; // 'ADDR:PORT|id' typedef std::map clientSession_container; typedef clientSession_container::value_type clientSession_value_type; @@ -803,7 +802,6 @@ private: const_server_iterator server_end() const throw() { return a_servers.end(); } static const Server* server(const_server_iterator ii) throw() { return ii->second; } - //typedef int entity_key; // exclusiveHash('IP1:PORT1 IP2:PORT2 IP3:PORT3 ...') typedef std::string entity_key; // 'ADDR1:PORT1 ADDR2:PORT2 ADDR3:PORT3 ...' entity_key getEntityKey(const socket_v &) const throw(); entity_key getEntityKey(const std::string & addr1, int port1, const std::string & addr2, int port2) const throw(); diff --git a/source/core/functions.cpp b/source/core/functions.cpp index f75902d..1dd3025 100644 --- a/source/core/functions.cpp +++ b/source/core/functions.cpp @@ -37,9 +37,6 @@ using namespace std; #define PAGE_WIDTH_LENGTH 80 -ExclusiveHash functions::st_stringExclusiveHash; -ExclusiveHash functions::st_string2intExclusiveHash; - string functions::getVersion() throw() { static const int version = ANNA_VERSION; string result; diff --git a/source/diameter.comm/Engine.cpp b/source/diameter.comm/Engine.cpp index 56aa1b6..4bd70ed 100644 --- a/source/diameter.comm/Engine.cpp +++ b/source/diameter.comm/Engine.cpp @@ -352,8 +352,7 @@ throw(anna::RuntimeException) { result->setWatchdogPeriod(a_watchdogPeriod); result->a_parent = server; result->a_socketId = socketId; - result->initializeSequences(); // despu�s de asignar el server y el socketId (*) - // (*) Las secuencias se basan en la semilla: srand(::time(NULL) + anna::functions::exclusiveHash(anna::functions::asString("%s:%d|%d", getAddress().c_str(), getPort(), a_socketId))); + result->initializeSequences(); // despues de asignar el server y el socketId (sequences are seed-based by mean exclusive hash) result->a_engine = this; clientSession_key key = ClientSession::getKey(server->getAddress(), server->getPort(), socketId); a_clientSessions.insert(clientSession_value_type(key, result)); @@ -919,7 +918,6 @@ comm::Engine::entity_key comm::Engine::getEntityKey(const socket_v &v) const thr } result.erase(result.size() - 1, 1); // remove last space - //return anna::functions::exclusiveHash(result); return result; } diff --git a/source/diameter.comm/LocalServer.cpp b/source/diameter.comm/LocalServer.cpp index 9e03084..787755b 100644 --- a/source/diameter.comm/LocalServer.cpp +++ b/source/diameter.comm/LocalServer.cpp @@ -81,7 +81,7 @@ LocalServer::serverSession_iterator LocalServer::serverSession_find(const server LocalServer::serverSession_key LocalServer::getServerSessionKey(const anna::comm::ClientSocket &clientSocket) const throw() { - return (anna::functions::exclusiveHash(clientSocket.getRemoteAccessPoint().getINetAddress().serialize())); + return (anna::functions::hash(clientSocket.getRemoteAccessPoint().getINetAddress().serialize().c_str())); } @@ -273,8 +273,7 @@ ServerSession *LocalServer::createServerSession(const anna::comm::ClientSocket & result->setClientSocket((anna::comm::ClientSocket*)(&clientSocket)); result->a_parent = this; result->a_socketId = key; // de momento... - result->initializeSequences(); // despu�s de asignar el LocalServer y el socketId (*) - // (*) Las secuencias se basan en la semilla: srand(::time(NULL) + anna::functions::exclusiveHash(anna::functions::asString("%s:%d|%d", getAddress().c_str(), getPort(), a_socketId))); + result->initializeSequences(); // despues de asignar el LocalServer y el socketId (sequences are seed-based by mean exclusive hash) result->a_engine = a_engine; a_serverSessions.insert(serverSession_value_type(key, result)); newConnection(); diff --git a/source/diameter.comm/Session.cpp b/source/diameter.comm/Session.cpp index 11fe035..778a834 100644 --- a/source/diameter.comm/Session.cpp +++ b/source/diameter.comm/Session.cpp @@ -109,7 +109,7 @@ void Session::initializeSequences() throw() { // affect any state that was set when the original request was // processed. Duplicate answer messages that are to be locally // consumed (see Section 6.2) SHOULD be silently discarded. - srand(::time(NULL) + anna::functions::exclusiveHash(anna::functions::asString("%s:%d|%d", getAddress().c_str(), getPort(), a_socketId))); + srand(::time(NULL) + anna::functions::hash(anna::functions::asString("%s:%d|%d", getAddress().c_str(), getPort(), a_socketId).c_str())); a_nextHopByHop = rand(); a_nextEndToEnd = ((::time(NULL) & 0xFFF) << 20) + (rand() & 0xFFFFF); }