diff --git a/include/private/soci-cstrtoi.h b/include/private/soci-cstrtoi.h index 2d507f7a..ca28756e 100644 --- a/include/private/soci-cstrtoi.h +++ b/include/private/soci-cstrtoi.h @@ -10,6 +10,7 @@ #include "soci/error.h" +#include #include #include @@ -30,7 +31,7 @@ bool cstring_to_integer(T& result, char const* buf) char * end; const long long t = strtoll(buf, &end, 10); - if (end == buf || *end != '\0') + if (end == buf || *end != '\0' || errno == ERANGE) return false; // successfully converted to long long @@ -53,7 +54,7 @@ bool cstring_to_unsigned(T& result, char const* buf) char * end; const unsigned long long t = strtoull(buf, &end, 10); - if (end == buf || *end != '\0') + if (end == buf || *end != '\0' || errno == ERANGE) return false; // successfully converted to unsigned long long diff --git a/src/backends/mysql/common.h b/src/backends/mysql/common.h index b847129f..8797544c 100644 --- a/src/backends/mysql/common.h +++ b/src/backends/mysql/common.h @@ -10,12 +10,13 @@ #include "soci/mysql/soci-mysql.h" #include "soci-cstrtod.h" +#include "soci-cstrtoi.h" #include "soci-compiler.h" // std #include #include #include -#include +#include #include namespace soci @@ -48,12 +49,19 @@ bool is_infinity_or_nan(T x) SOCI_GCC_WARNING_RESTORE(float-equal) } -template +template ::value, int> = 0> void parse_num(char const *buf, T &x) { - std::istringstream iss(buf); - iss >> x; - if (iss.fail() || (iss.eof() == false)) + if (!cstring_to_integer(x, buf)) + { + throw soci_error("Cannot convert data."); + } +} + +template ::value, int> = 0> +void parse_num(char const *buf, T &x) +{ + if (!cstring_to_unsigned(x, buf)) { throw soci_error("Cannot convert data."); } diff --git a/src/backends/mysql/standard-into-type.cpp b/src/backends/mysql/standard-into-type.cpp index 5146555f..7bde99b5 100644 --- a/src/backends/mysql/standard-into-type.cpp +++ b/src/backends/mysql/standard-into-type.cpp @@ -87,28 +87,10 @@ void mysql_standard_into_type_backend::post_fetch( } break; case x_int8: - { - int32_t tmp = 0; - parse_num(buf, tmp); - if (tmp < (std::numeric_limits::min)() || - tmp > (std::numeric_limits::max)()) - { - throw soci_error("Cannot convert data."); - } - exchange_type_cast(data_) = static_cast(tmp); - } + parse_num(buf, exchange_type_cast(data_)); break; case x_uint8: - { - uint32_t tmp = 0; - parse_num(buf, tmp); - if (tmp < (std::numeric_limits::min)() || - tmp > (std::numeric_limits::max)()) - { - throw soci_error("Cannot convert data."); - } - exchange_type_cast(data_) = static_cast(tmp); - } + parse_num(buf, exchange_type_cast(data_)); break; case x_int16: parse_num(buf, exchange_type_cast(data_)); diff --git a/src/backends/mysql/vector-into-type.cpp b/src/backends/mysql/vector-into-type.cpp index 4e7cb518..812434ac 100644 --- a/src/backends/mysql/vector-into-type.cpp +++ b/src/backends/mysql/vector-into-type.cpp @@ -109,17 +109,15 @@ void mysql_vector_into_type_backend::post_fetch(bool gotData, indicator *ind) break; case x_int8: { - int32_t tmp = 0; - parse_num(buf, tmp); - int8_t val = static_cast(tmp); + int8_t val; + parse_num(buf, val); set_invector_(data_, i, val); } break; case x_uint8: { - uint32_t tmp = 0; - parse_num(buf, tmp); - uint8_t val = static_cast(tmp); + uint8_t val; + parse_num(buf, val); set_invector_(data_, i, val); } break; diff --git a/src/backends/sqlite3/session.cpp b/src/backends/sqlite3/session.cpp index 4e107e4c..099a9158 100644 --- a/src/backends/sqlite3/session.cpp +++ b/src/backends/sqlite3/session.cpp @@ -11,10 +11,11 @@ #include "soci-cstrtoi.h" -#include -#include #include -#include + +#include + +#include #include using namespace soci; @@ -24,15 +25,10 @@ using namespace sqlite_api; namespace // anonymous { -// Callback function used to construct the error message in the provided stream. -// -// SQLite3 own error message will be appended to it. -using error_callback = std::function; - // helper function for hardcoded queries: this is a simple wrapper for // sqlite3_exec() which throws an exception on error. void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query, - error_callback const& errCallback, + char const* const errMsg, int (*callback)(void*, int, char**, char**) = nullptr, void* callback_arg = nullptr) { @@ -42,39 +38,16 @@ void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query, std::unique_ptr zErrMsgPtr(zErrMsg, sqlite3_free); if (res != SQLITE_OK) { - std::ostringstream ss; - errCallback(ss); - - throw sqlite3_soci_error(conn, ss.str(), zErrMsg); + throw sqlite3_soci_error(conn, errMsg, zErrMsg); } } -// Simpler to use overload which uses a hard coded error message. -void execute_hardcoded(sqlite_api::sqlite3* conn, char const* const query, char const* const errMsg, - int (*callback)(void*, int, char**, char**) = nullptr, - void* callback_arg = nullptr) +// overload allowing to pass std::string instead of const char* +void execute_hardcoded(sqlite_api::sqlite3* conn, + std::string const& query, + std::string const& errMsg) { - return execute_hardcoded(conn, query, - [errMsg](std::ostream& ostr) { ostr << errMsg; }, - callback, callback_arg - ); -} - -void check_sqlite_err(sqlite_api::sqlite3* conn, int res, - error_callback const& errCallback) -{ - if (SQLITE_OK != res) - { - std::ostringstream ss; - errCallback(ss); - - throw sqlite3_soci_error(conn, ss.str()); - } -} - -void check_sqlite_err(sqlite_api::sqlite3* conn, int res, char const* const errMsg) -{ - return check_sqlite_err(conn, res, [errMsg](std::ostream& ostr) { ostr << errMsg; }); + execute_hardcoded(conn, query.c_str(), errMsg.c_str()); } } // namespace anonymous @@ -131,8 +104,8 @@ sqlite3_session_backend::sqlite3_session_backend( } if (params.get_option("timeout", val)) { - std::istringstream converter(val); - converter >> timeout; + if (!cstring_to_integer(timeout, val.c_str()) || timeout < 0) + throw soci_error("Invalid value for timeout option: {}", val); } if (params.get_option("synchronous", val)) { @@ -165,36 +138,31 @@ sqlite3_session_backend::sqlite3_session_backend( std::unique_ptr conn_ptr(conn, sqlite3_close); - check_sqlite_err(conn, res, - [&dbname](std::ostream& ostr) - { - ostr << "Cannot establish connection to \"" << dbname << "\""; - } - ); + if (res != SQLITE_OK) + throw sqlite3_soci_error(conn, fmt::format(R"(Cannot establish connection to "{}")", dbname)); // Set the timeout first to have effect on the following queries. res = sqlite3_busy_timeout(conn, timeout * 1000); - check_sqlite_err(conn, res, "Failed to set busy timeout for connection. "); + if (res != SQLITE_OK) + throw sqlite3_soci_error(conn, "Failed to set busy timeout for connection."); if (!synchronous.empty()) { - std::string const query("pragma synchronous=" + synchronous); - execute_hardcoded(conn, query.c_str(), - [&synchronous](std::ostream& ostr) - { - ostr << "Setting synchronous pragma to \"" << synchronous << "\" failed"; - } + execute_hardcoded + ( + conn, + fmt::format("pragma synchronous={}", synchronous), + fmt::format(R"(Setting synchronous pragma to "{}" failed)", synchronous) ); } if (!foreignKeys.empty()) { - std::string const query("pragma foreign_keys=" + foreignKeys); - execute_hardcoded(conn, query.c_str(), - [&foreignKeys](std::ostream& ostr) - { - ostr << "Setting foreign_keys pragma to \"" << foreignKeys << "\" failed"; - } + execute_hardcoded + ( + conn, + fmt::format("pragma foreign_keys={}", foreignKeys), + fmt::format(R"(Setting foreign_keys pragma to "{}" failed)", foreignKeys) ); } diff --git a/src/core/statement.cpp b/src/core/statement.cpp index 26750013..85cbd6a1 100644 --- a/src/core/statement.cpp +++ b/src/core/statement.cpp @@ -596,7 +596,7 @@ void statement_impl::do_add_query_parameters() for (std::size_t i = 0; i != usize; ++i) { std::string name = get_name(*uses_[i], i, backEnd_); - std::stringstream value; + std::ostringstream value; uses_[i]->dump_value(value, backEnd_->get_row_to_dump()); session_.add_query_parameter(std::move(name), value.str()); }