[zeromq-dev] [PATCH] add uri validations

Dhammika Pathirana dhammika at gmail.com
Mon Dec 13 08:22:47 CET 2010


>From b6571910583e4d9dad094d6ec9fe5d430fe7db4c Mon Sep 17 00:00:00 2001
From: Dhammika Pathirana <dhammika at gmail.com>
Date: Sun, 12 Dec 2010 22:56:28 -0800
Subject: [PATCH 2/2] add basic uri validations


Signed-off-by: Dhammika Pathirana <dhammika at gmail.com>
---
 src/socket_base.cpp |   50 ++++++++++++++++++++++++++++----------------------
 src/socket_base.hpp |    4 ++++
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/src/socket_base.cpp b/src/socket_base.cpp
index bfaacb7..174cfd9 100644
--- a/src/socket_base.cpp
+++ b/src/socket_base.cpp
@@ -132,6 +132,26 @@ void zmq::socket_base_t::stop ()
     send_stop ();
 }

+int zmq::socket_base_t::parse_uri (const char *uri_,
+                        std::string &protocol_, std::string &address_)
+{
+    zmq_assert (uri_ != NULL);
+
+    std::string uri (uri_);
+    std::string::size_type pos = uri.find ("://");
+    if (pos == std::string::npos) {
+        errno = EINVAL;
+        return -1;
+    }
+    protocol_ = uri.substr (0, pos);
+    address_ = uri.substr (pos + 3);
+    if (protocol_.empty () || address_.empty ()) {
+        errno = EINVAL;
+        return -1;
+    }
+    return 0;
+}
+
 int zmq::socket_base_t::check_protocol (const std::string &protocol_)
 {
     //  First check out whether the protcol is something we are aware of.
@@ -263,18 +283,11 @@ int zmq::socket_base_t::bind (const char *addr_)
     //  Parse addr_ string.
     std::string protocol;
     std::string address;
-    {
-        std::string addr (addr_);
-        std::string::size_type pos = addr.find ("://");
-        if (pos == std::string::npos) {
-            errno = EINVAL;
-            return -1;
-        }
-        protocol = addr.substr (0, pos);
-        address = addr.substr (pos + 3);
-    }
+    int rc = parse_uri (addr_, protocol, address);
+    if (rc != 0)
+        return -1;

-    int rc = check_protocol (protocol);
+    rc = check_protocol (protocol);
     if (rc != 0)
         return -1;

@@ -325,18 +338,11 @@ int zmq::socket_base_t::connect (const char *addr_)
     //  Parse addr_ string.
     std::string protocol;
     std::string address;
-    {
-        std::string addr (addr_);
-        std::string::size_type pos = addr.find ("://");
-        if (pos == std::string::npos) {
-            errno = EINVAL;
-            return -1;
-        }
-        protocol = addr.substr (0, pos);
-        address = addr.substr (pos + 3);
-    }
+    int rc = parse_uri (addr_, protocol, address);
+    if (rc != 0)
+        return -1;

-    int rc = check_protocol (protocol);
+    rc = check_protocol (protocol);
     if (rc != 0)
         return -1;

diff --git a/src/socket_base.hpp b/src/socket_base.hpp
index 69de24d..795bdc1 100644
--- a/src/socket_base.hpp
+++ b/src/socket_base.hpp
@@ -128,6 +128,10 @@ namespace zmq
         //  where it doesn't intersect the object being destroyed.
         bool destroyed;

+        //  Parse URI string.
+        int parse_uri (const char *uri_,
+                       std::string &protocol_, std::string &address_);
+
         //  Check whether transport protocol, as specified in connect or
         //  bind, is available and compatible with the socket type.
         int check_protocol (const std::string &protocol_);
-- 
1.7.0.4


Dhammika



More information about the zeromq-dev mailing list