From f11a7055a1f47130221eaeb39c84b1c12cc06b31 Mon Sep 17 00:00:00 2001 From: badaix Date: Mon, 25 Apr 2016 19:27:21 +0200 Subject: [PATCH] string compat functions throw --- client/clientConnection.cpp | 2 +- client/snapClient.cpp | 2 +- common/sampleFormat.cpp | 2 +- common/{compat.h => strCompat.h} | 32 +++++++++++++++++++++------ externals/json.hpp | 2 +- server/config.cpp | 2 +- server/encoder/flacEncoder.cpp | 2 +- server/encoder/oggEncoder.cpp | 2 +- server/streamreader/pcmStream.cpp | 2 +- server/streamreader/pipeStream.cpp | 2 +- server/streamreader/streamManager.cpp | 2 +- server/streamreader/streamUri.cpp | 2 +- 12 files changed, 36 insertions(+), 18 deletions(-) rename common/{compat.h => strCompat.h} (54%) diff --git a/client/clientConnection.cpp b/client/clientConnection.cpp index 1a30325a..2695a4b2 100644 --- a/client/clientConnection.cpp +++ b/client/clientConnection.cpp @@ -19,7 +19,7 @@ #include #include #include "clientConnection.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/snapException.h" #include "message/hello.h" #include "common/log.h" diff --git a/client/snapClient.cpp b/client/snapClient.cpp index c1b2bd4e..c6159382 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -32,7 +32,7 @@ #endif #include "common/log.h" #include "common/signalHandler.h" -#include "common/compat.h" +#include "common/strCompat.h" using namespace std; diff --git a/common/sampleFormat.cpp b/common/sampleFormat.cpp index cb4f0a93..c98d8746 100644 --- a/common/sampleFormat.cpp +++ b/common/sampleFormat.cpp @@ -21,7 +21,7 @@ #include #include "sampleFormat.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/utils.h" #include "common/log.h" diff --git a/common/compat.h b/common/strCompat.h similarity index 54% rename from common/compat.h rename to common/strCompat.h index 6962cbaa..a8066490 100644 --- a/common/compat.h +++ b/common/strCompat.h @@ -7,6 +7,10 @@ #ifdef NO_CPP11_STRING #include #include +#include +#include +#include +#include #endif @@ -24,19 +28,26 @@ namespace cpt #endif } - static long stoul(const std::string& s) + static long stoul(const std::string& str) { #ifdef NO_CPP11_STRING - return atol(s.c_str()); + errno = 0; + char *temp; + long val = strtol(str.c_str(), &temp, 10); + if (temp == str.c_str() || *temp != '\0') + throw std::invalid_argument("stoi"); + if (((val == LONG_MIN) || (val == LONG_MAX)) && (errno == ERANGE)) + throw std::out_of_range("stoi"); + return val; #else - return std::stoul(s); + return std::stoul(str); #endif } static int stoi(const std::string& str) { #ifdef NO_CPP11_STRING - return strtol(str.c_str(), 0, 10); + return cpt::stoul(str); #else return std::stoi(str); #endif @@ -45,7 +56,14 @@ namespace cpt static double stod(const std::string& str) { #ifdef NO_CPP11_STRING - return strtod(str.c_str(), NULL); + errno = 0; + char *temp; + double val = strtod(str.c_str(), &temp); + if (temp == str.c_str() || *temp != '\0') + throw std::invalid_argument("stod"); + if ((val == HUGE_VAL) && (errno == ERANGE)) + throw std::out_of_range("stod"); + return val; #else return std::stod(str.c_str()); #endif @@ -54,7 +72,7 @@ namespace cpt static long double strtold(const char* str, char** endptr) { #ifdef NO_CPP11_STRING - return strtod(str, endptr); + return cpt::stod(str); #else return std::strtold(str, endptr); #endif @@ -63,7 +81,7 @@ namespace cpt static float strtof(const char* str, char** endptr) { #ifdef NO_CPP11_STRING - return (float)strtod(str, endptr); + return (float)cpt::stod(str); #else return std::strtof(str, endptr); #endif diff --git a/externals/json.hpp b/externals/json.hpp index 62628541..d3d48cc6 100644 --- a/externals/json.hpp +++ b/externals/json.hpp @@ -61,7 +61,7 @@ Class @ref nlohmann::basic_json is a good entry point for the documentation. #include #include -#include "common/compat.h" +#include "common/strCompat.h" // enable ssize_t on MinGW #ifdef __GNUC__ diff --git a/server/config.cpp b/server/config.cpp index c3fb0ca4..6b0c7fc8 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -22,7 +22,7 @@ #include #include #include "common/snapException.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/log.h" using namespace std; diff --git a/server/encoder/flacEncoder.cpp b/server/encoder/flacEncoder.cpp index 780bdee5..7e5715b7 100644 --- a/server/encoder/flacEncoder.cpp +++ b/server/encoder/flacEncoder.cpp @@ -19,7 +19,7 @@ #include #include "flacEncoder.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/snapException.h" #include "common/log.h" diff --git a/server/encoder/oggEncoder.cpp b/server/encoder/oggEncoder.cpp index bcaa87f0..201ba615 100644 --- a/server/encoder/oggEncoder.cpp +++ b/server/encoder/oggEncoder.cpp @@ -21,7 +21,7 @@ #include "oggEncoder.h" #include "common/snapException.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/utils.h" #include "common/log.h" diff --git a/server/streamreader/pcmStream.cpp b/server/streamreader/pcmStream.cpp index f0d7e5bf..2cc8e801 100644 --- a/server/streamreader/pcmStream.cpp +++ b/server/streamreader/pcmStream.cpp @@ -23,7 +23,7 @@ #include "encoder/encoderFactory.h" #include "common/snapException.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "pcmStream.h" #include "common/log.h" diff --git a/server/streamreader/pipeStream.cpp b/server/streamreader/pipeStream.cpp index 518dbcc3..98f073f2 100644 --- a/server/streamreader/pipeStream.cpp +++ b/server/streamreader/pipeStream.cpp @@ -26,7 +26,7 @@ #include "encoder/encoderFactory.h" #include "common/log.h" #include "common/snapException.h" -#include "common/compat.h" +#include "common/strCompat.h" using namespace std; diff --git a/server/streamreader/streamManager.cpp b/server/streamreader/streamManager.cpp index 82a05cb0..6965153b 100644 --- a/server/streamreader/streamManager.cpp +++ b/server/streamreader/streamManager.cpp @@ -20,7 +20,7 @@ #include "pipeStream.h" #include "fileStream.h" #include "common/utils.h" -#include "common/compat.h" +#include "common/strCompat.h" #include "common/log.h" #include "common/snapException.h" diff --git a/server/streamreader/streamUri.cpp b/server/streamreader/streamUri.cpp index 5be69c2c..f7fb707f 100644 --- a/server/streamreader/streamUri.cpp +++ b/server/streamreader/streamUri.cpp @@ -18,7 +18,7 @@ #include -#include +#include #include #include "streamUri.h"