From ba59bf4fe2216dd86ee2083b28f85beda1e1b845 Mon Sep 17 00:00:00 2001
From: Richard Walters <rwalters@digitalstirling.com>
Date: Thu, 9 Aug 2018 13:58:45 -0700
Subject: use SystemAbstractions::ToInteger to convert strings to integers

---
 src/Uri.cpp | 54 +++++++++++++++---------------------------------------
 1 file changed, 15 insertions(+), 39 deletions(-)

diff --git a/src/Uri.cpp b/src/Uri.cpp
index 7ad1d68..143b06b 100644
--- a/src/Uri.cpp
+++ b/src/Uri.cpp
@@ -12,6 +12,7 @@
 #include <algorithm>
 #include <functional>
 #include <inttypes.h>
+#include <limits>
 #include <memory>
 #include <sstream>
 #include <string>
@@ -129,44 +130,6 @@ namespace {
         ':'
     };
 
-    /**
-     * This function parses the given string as an unsigned 16-bit
-     * integer, detecting invalid characters, overflow, etc.
-     *
-     * @param[in] numberString
-     *     This is the string containing the number to parse.
-     *
-     * @param[out] number
-     *     This is where to store the number parsed.
-     *
-     * @return
-     *     An indication of whether or not the number was parsed
-     *     successfully is returned.
-     */
-    bool ParseUint16(
-        const std::string& numberString,
-        uint16_t& number
-    ) {
-        uint32_t numberIn32Bits = 0;
-        for (auto c: numberString) {
-            if (
-                (c < '0')
-                || (c > '9')
-            ) {
-                return false;
-            }
-            numberIn32Bits *= 10;
-            numberIn32Bits += (uint16_t)(c - '0');
-            if (
-                (numberIn32Bits & ~((1 << 16) - 1)) != 0
-            ) {
-                return false;
-            }
-        }
-        number = (uint16_t)numberIn32Bits;
-        return true;
-    }
-
     /**
      * This function checks to make sure the given string
      * is a valid rendering of an octet as a decimal number.
@@ -874,9 +837,22 @@ namespace Uri {
             if (portString.empty()) {
                 hasPort = false;
             } else {
-                if (!ParseUint16(portString, port)) {
+                intmax_t portAsInt;
+                if (
+                    SystemAbstractions::ToInteger(
+                        portString,
+                        portAsInt
+                    ) != SystemAbstractions::ToIntegerResult::Success
+                ) {
+                    return false;
+                }
+                if (
+                    (portAsInt < 0)
+                    || (portAsInt > (decltype(portAsInt))std::numeric_limits< decltype(port) >::max())
+                ) {
                     return false;
                 }
+                port = (decltype(port))portAsInt;
                 hasPort = true;
             }
             return true;
-- 
cgit v1.2.3