From 216c87cbb7228d9130eead5e374dfd7a11a96b98 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 18 Sep 2021 14:25:24 +0100 Subject: Add JT recommended warnings --- libpqpp/Jamfile.jam | 15 ++++++++++++++ libpqpp/pq-binarycolumn.cpp | 6 +++--- libpqpp/pq-bulkselectcommand.cpp | 8 ++++---- libpqpp/pq-column.cpp | 15 +++++++------- libpqpp/pq-column.h | 11 ++++------- libpqpp/pq-command.cpp | 10 +++++----- libpqpp/pq-connection.cpp | 4 ++-- libpqpp/pq-cursorselectcommand.cpp | 8 ++++---- libpqpp/pq-mock.cpp | 2 +- libpqpp/pq-modifycommand.cpp | 8 ++++---- libpqpp/pq-prepared.cpp | 3 ++- libpqpp/pq-selectbase.cpp | 4 ++-- libpqpp/unittests/testpq.cpp | 40 +++++++++++++++++++------------------- 13 files changed, 74 insertions(+), 60 deletions(-) (limited to 'libpqpp') diff --git a/libpqpp/Jamfile.jam b/libpqpp/Jamfile.jam index ca1bd6a..86f8f92 100644 --- a/libpqpp/Jamfile.jam +++ b/libpqpp/Jamfile.jam @@ -17,6 +17,21 @@ project release:on debug:extra debug:on + debug:-Wnon-virtual-dtor + debug:-Wold-style-cast + debug:-Wcast-align + debug:-Wunused + debug:-Woverloaded-virtual + debug:-Wpedantic + debug:-Wconversion + debug:-Wsign-conversion + debug:-Wnull-dereference + debug:-Wdouble-promotion + debug:-Wformat=2 + gcc,debug:-Wduplicated-cond + gcc,debug:-Wduplicated-branches + gcc,debug:-Wlogical-op + gcc,debug:-Wuseless-cast coverage:on ; diff --git a/libpqpp/pq-binarycolumn.cpp b/libpqpp/pq-binarycolumn.cpp index 1e75343..ce4e8c1 100644 --- a/libpqpp/pq-binarycolumn.cpp +++ b/libpqpp/pq-binarycolumn.cpp @@ -22,13 +22,13 @@ PQ::BinaryColumn::apply(DB::HandleField & h) const h.boolean(valueAs()); break; case 21: // INT2OID: - h.integer(be16toh(valueAs())); + h.integer(static_cast(be16toh(valueAs()))); break; case 23: // INT4OID: - h.integer(be32toh(valueAs())); + h.integer(static_cast(be32toh(valueAs()))); break; case 20: // INT8OID: - h.integer(be64toh(valueAs())); + h.integer(static_cast(be64toh(valueAs()))); break; case 17: // BYTEAOID h.blob(DB::Blob(value(), length())); diff --git a/libpqpp/pq-bulkselectcommand.cpp b/libpqpp/pq-bulkselectcommand.cpp index ff9f2f4..de2ecb4 100644 --- a/libpqpp/pq-bulkselectcommand.cpp +++ b/libpqpp/pq-bulkselectcommand.cpp @@ -14,11 +14,11 @@ void PQ::BulkSelectCommand::execute() { if (!executed) { - execRes = c->checkResult(PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), - &formats.front(), binary), + execRes = c->checkResult(PQexecPrepared(c->conn, prepare(), static_cast(values.size()), &values.front(), + &lengths.front(), &formats.front(), binary), PGRES_TUPLES_OK); - nTuples = PQntuples(execRes); - tuple = -1; + nTuples = static_cast(PQntuples(execRes)); + tuple = static_cast(-1); createColumns(execRes); executed = true; } diff --git a/libpqpp/pq-column.cpp b/libpqpp/pq-column.cpp index e4d85d8..c1e0d40 100644 --- a/libpqpp/pq-column.cpp +++ b/libpqpp/pq-column.cpp @@ -5,7 +5,8 @@ #include PQ::Column::Column(const SelectBase * s, unsigned int i) : - DB::Column(PQfname(s->execRes, (int)i), i), sc(s), oid(PQftype(sc->execRes, (int)colNo)), buf(nullptr) + DB::Column(PQfname(s->execRes, static_cast(i)), i), sc(s), oid(PQftype(sc->execRes, static_cast(colNo))), + buf(nullptr) { } @@ -19,19 +20,19 @@ PQ::Column::~Column() bool PQ::Column::isNull() const { - return PQgetisnull(sc->execRes, (int)sc->tuple, (int)colNo); + return PQgetisnull(sc->execRes, static_cast(sc->tuple), static_cast(colNo)); } std::size_t PQ::Column::length() const { - return PQgetlength(sc->execRes, (int)sc->tuple, (int)colNo); + return static_cast(PQgetlength(sc->execRes, static_cast(sc->tuple), static_cast(colNo))); } const char * PQ::Column::value() const { - return PQgetvalue(sc->execRes, (int)sc->tuple, (int)colNo); + return PQgetvalue(sc->execRes, static_cast(sc->tuple), static_cast(colNo)); } void @@ -74,8 +75,8 @@ PQ::Column::apply(DB::HandleField & h) const >= 4) { h.interval(boost::posix_time::time_duration((24 * days) + hours, minutes, seconds, fractions - * (long)pow(10, - boost::posix_time::time_res_traits::num_fractional_digits() + flen1 - flen2))); + * static_cast(pow(10, + boost::posix_time::time_res_traits::num_fractional_digits() + flen1 - flen2)))); } else { h.interval(boost::posix_time::duration_from_string(val)); @@ -96,7 +97,7 @@ PQ::Column::apply(DB::HandleField & h) const PQfreemem(buf); } size_t len; - buf = PQunescapeBytea(valueAsPtr(), &len); + buf = PQunescapeBytea(reinterpret_cast(value()), &len); h.blob(DB::Blob(buf, len)); break; } diff --git a/libpqpp/pq-column.h b/libpqpp/pq-column.h index b43880e..f893fef 100644 --- a/libpqpp/pq-column.h +++ b/libpqpp/pq-column.h @@ -2,6 +2,7 @@ #define PG_COLUMN_H #include +#include #include namespace PQ { @@ -19,13 +20,9 @@ namespace PQ { inline T valueAs() const { - return *(T *)(value()); - } - template - inline T * - valueAsPtr() const - { - return (T *)(value()); + T v {}; + std::memcpy(&v, value(), sizeof(T)); + return v; } const char * value() const; std::size_t length() const; diff --git a/libpqpp/pq-command.cpp b/libpqpp/pq-command.cpp index 98cd725..aaa8260 100644 --- a/libpqpp/pq-command.cpp +++ b/libpqpp/pq-command.cpp @@ -6,7 +6,7 @@ #include #include -NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory); +NAMEDFACTORY("postgresql", PQ::CommandOptions, DB::CommandOptionsFactory) AdHocFormatter(PQCommondStatement, "pStatement_id%?"); PQ::Command::Command(Connection * conn, const std::string & sql, const DB::CommandOptionsCPtr & opts) : @@ -16,7 +16,7 @@ PQ::Command::Command(Connection * conn, const std::string & sql, const DB::Comma } PQ::CommandOptions::CommandOptions(std::size_t hash, const DB::CommandOptionsMap & map) : - DB::CommandOptions(hash), fetchTuples(get(map, "page-size", 35)), useCursor(!isSet(map, "no-cursor")), + DB::CommandOptions(hash), fetchTuples(get(map, "page-size", 35U)), useCursor(!isSet(map, "no-cursor")), fetchBinary(isSet(map, "fetch-binary")) { } @@ -68,7 +68,7 @@ PQ::Command::paramSet(unsigned int n, const T &... v) { paramsAtLeast(n); bufs[n] = std::make_unique(PQCommandParamFmt::get(v...)); - lengths[n] = bufs[n]->length(); + lengths[n] = static_cast(bufs[n]->length()); formats[n] = 0; values[n] = bufs[n]->data(); } @@ -78,7 +78,7 @@ PQ::Command::paramSet(unsigned int n, const std::string_view & b) { paramsAtLeast(n); bufs[n] = std::make_unique(b); - lengths[n] = b.length(); + lengths[n] = static_cast(b.length()); formats[n] = 0; values[n] = bufs[n]->data(); } @@ -152,7 +152,7 @@ void PQ::Command::bindParamBLOB(unsigned int n, const DB::Blob & v) { paramsAtLeast(n); - lengths[n] = v.len; + lengths[n] = static_cast(v.len); formats[n] = 1; values[n] = static_cast(v.data); bufs[n].reset(); diff --git a/libpqpp/pq-connection.cpp b/libpqpp/pq-connection.cpp index 46e1917..4b201f5 100644 --- a/libpqpp/pq-connection.cpp +++ b/libpqpp/pq-connection.cpp @@ -8,7 +8,7 @@ #include #include -NAMEDFACTORY("postgresql", PQ::Connection, DB::ConnectionFactory); +NAMEDFACTORY("postgresql", PQ::Connection, DB::ConnectionFactory) static void setup() __attribute__((constructor(101))); static void @@ -161,7 +161,7 @@ size_t PQ::Connection::bulkUploadData(const char * data, size_t len) const { int rc; - while (!(rc = PQputCopyData(conn, data, len))) { + while (!(rc = PQputCopyData(conn, data, static_cast(len)))) { sleep(1); } if (rc != 1) { diff --git a/libpqpp/pq-cursorselectcommand.cpp b/libpqpp/pq-cursorselectcommand.cpp index 1981bc8..9ae9655 100644 --- a/libpqpp/pq-cursorselectcommand.cpp +++ b/libpqpp/pq-cursorselectcommand.cpp @@ -38,8 +38,8 @@ PQ::CursorSelectCommand::execute() if (s_declare.empty()) { s_declare = mkdeclare(); } - c->checkResultFree(PQexecParams(c->conn, s_declare.c_str(), values.size(), nullptr, &values.front(), - &lengths.front(), &formats.front(), binary), + c->checkResultFree(PQexecParams(c->conn, s_declare.c_str(), static_cast(values.size()), nullptr, + &values.front(), &lengths.front(), &formats.front(), binary), PGRES_COMMAND_OK); fetchTuples(); createColumns(execRes); @@ -52,8 +52,8 @@ PQ::CursorSelectCommand::fetchTuples() { execRes = c->checkResult( PQexecParams(c->conn, s_fetch.c_str(), 0, nullptr, nullptr, nullptr, nullptr, binary), PGRES_TUPLES_OK); - nTuples = PQntuples(execRes); - tuple = -1; + nTuples = static_cast(PQntuples(execRes)); + tuple = static_cast(-1); } bool diff --git a/libpqpp/pq-mock.cpp b/libpqpp/pq-mock.cpp index 34d903e..fcff52e 100644 --- a/libpqpp/pq-mock.cpp +++ b/libpqpp/pq-mock.cpp @@ -6,7 +6,7 @@ #include #include -NAMEDFACTORY("postgresql", PQ::Mock, DB::MockDatabaseFactory); +NAMEDFACTORY("postgresql", PQ::Mock, DB::MockDatabaseFactory) namespace PQ { diff --git a/libpqpp/pq-modifycommand.cpp b/libpqpp/pq-modifycommand.cpp index 4c25a7e..8697b10 100644 --- a/libpqpp/pq-modifycommand.cpp +++ b/libpqpp/pq-modifycommand.cpp @@ -11,13 +11,13 @@ PQ::ModifyCommand::ModifyCommand(Connection * conn, const std::string & sql, con unsigned int PQ::ModifyCommand::execute(bool anc) { - PGresult * res - = PQexecPrepared(c->conn, prepare(), values.size(), &values.front(), &lengths.front(), &formats.front(), 0); + PGresult * res = PQexecPrepared(c->conn, prepare(), static_cast(values.size()), &values.front(), + &lengths.front(), &formats.front(), 0); c->checkResult(res, PGRES_COMMAND_OK, PGRES_TUPLES_OK); - unsigned int rows = atoi(PQcmdTuples(res)); + auto rows = atoi(PQcmdTuples(res)); PQclear(res); if (rows == 0 && !anc) { throw DB::NoRowsAffected(); } - return rows; + return static_cast(rows); } diff --git a/libpqpp/pq-prepared.cpp b/libpqpp/pq-prepared.cpp index 287eda8..640b3db 100644 --- a/libpqpp/pq-prepared.cpp +++ b/libpqpp/pq-prepared.cpp @@ -19,6 +19,7 @@ PQ::PreparedStatement::prepare() const std::stringstream psql; prepareSql(psql, sql); c->checkResultFree( - PQprepare(c->conn, stmntName.c_str(), psql.str().c_str(), values.size(), nullptr), PGRES_COMMAND_OK); + PQprepare(c->conn, stmntName.c_str(), psql.str().c_str(), static_cast(values.size()), nullptr), + PGRES_COMMAND_OK); return (pstmt = c->preparedStatements.insert({hash, stmntName}).first->second.c_str()); } diff --git a/libpqpp/pq-selectbase.cpp b/libpqpp/pq-selectbase.cpp index 49b78d2..4d4cada 100644 --- a/libpqpp/pq-selectbase.cpp +++ b/libpqpp/pq-selectbase.cpp @@ -19,8 +19,8 @@ PQ::SelectBase::~SelectBase() void PQ::SelectBase::createColumns(PGresult * execRes) { - unsigned int nFields = PQnfields(execRes); - for (unsigned int f = 0; f < nFields; f += 1) { + auto nFields = PQnfields(execRes); + for (decltype(nFields) f = 0; f < nFields; f += 1) { if (binary) { insertColumn(std::make_unique(this, f)); } diff --git a/libpqpp/unittests/testpq.cpp b/libpqpp/unittests/testpq.cpp index 76a5d5d..6b86814 100644 --- a/libpqpp/unittests/testpq.cpp +++ b/libpqpp/unittests/testpq.cpp @@ -25,7 +25,7 @@ public: BOOST_GLOBAL_FIXTURE(StandardMockDatabase); -BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore); +BOOST_FIXTURE_TEST_SUITE(Core, DB::TestCore) BOOST_AUTO_TEST_CASE(transactions) { @@ -55,23 +55,23 @@ BOOST_AUTO_TEST_CASE(bindAndSend) mod->bindParamT(4, testDateTime); mod->bindParamT(5, testInterval); mod->execute(); - mod->bindParamI(0, (unsigned int)(testInt + 10)); - mod->bindParamF(1, (float)(testDouble + 10)); + mod->bindParamI(0, static_cast(testInt + 10)); + mod->bindParamF(1, static_cast(testDouble + 10)); mod->bindParamS(2, std::string(testString) + " something"); mod->bindParamB(3, true); mod->bindParamT(4, testDateTime); mod->bindParamT(5, testInterval); mod->execute(); mod->bindNull(0); - mod->bindParamI(1, (long long unsigned int)(testDouble + 10)); - mod->bindParamI(2, (long long int)testInt); + mod->bindParamI(1, static_cast(testDouble + 10)); + mod->bindParamI(2, static_cast(testInt)); mod->bindParamB(3, true); mod->bindNull(4); mod->bindNull(5); mod->execute(); mod->bindNull(0); - mod->bindParamI(1, (long unsigned int)(testDouble + 10)); - mod->bindParamI(2, (long int)testInt); + mod->bindParamI(1, static_cast(testDouble + 10)); + mod->bindParamI(2, testInt); mod->bindParamB(3, true); mod->bindParamS(4, "2016-01-01T12:34:56"); mod->bindNull(5); @@ -174,7 +174,7 @@ BOOST_AUTO_TEST_CASE(bulkload) } std::array buf {}; for (std::streamsize r; (r = in.readsome(buf.data(), buf.size())) > 0;) { - ro->bulkUploadData(buf.data(), r); + ro->bulkUploadData(buf.data(), static_cast(r)); } ro->endBulkUpload(nullptr); assertScalarValueHelper(*count, 800); @@ -274,7 +274,7 @@ BOOST_AUTO_TEST_CASE(bulkSelect) auto co = std::make_shared(0, 35, false); auto sel = ro->select("SELECT * FROM test WHERE id > ?", co); sel->bindParamI(0, 1); - int totalInt = 0, count = 0; + int64_t totalInt = 0, count = 0; sel->forEachRow([&totalInt, &count](auto i) { totalInt += i; count += 1; @@ -289,7 +289,7 @@ BOOST_AUTO_TEST_CASE(selectWithSmallPages) auto co = std::make_shared(0, 1, true); auto sel = ro->select("SELECT * FROM test WHERE id > ?", co); sel->bindParamI(0, 1); - int totalInt = 0, count = 0; + int64_t totalInt = 0, count = 0; sel->forEachRow([&totalInt, &count](auto i) { totalInt += i; count += 1; @@ -313,7 +313,7 @@ BOOST_AUTO_TEST_CASE(insertReturning) auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto co = std::make_shared(0, 35, false); auto sel = ro->select("INSERT INTO test(id, fl) VALUES(1, 3) RETURNING id + fl", co); - int totalInt = 0, count = 0; + int64_t totalInt = 0, count = 0; sel->forEachRow([&totalInt, &count](auto i) { totalInt += i; count += 1; @@ -324,7 +324,7 @@ BOOST_AUTO_TEST_CASE(insertReturning) BOOST_AUTO_TEST_CASE(closeOnError) { - auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); + auto ro = DB::MockDatabase::openConnectionTo("PQmock"); BOOST_REQUIRE_THROW( { ro->select("SELECT * FROM test")->forEachRow<>([&ro]() { @@ -363,7 +363,7 @@ BOOST_AUTO_TEST_CASE(closeOnError) BOOST_AUTO_TEST_CASE(blobs) { - auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); + auto ro = DB::MockDatabase::openConnectionTo("PQmock"); std::vector buf(29); memcpy(&buf[0], "This is some binary text data", 29); auto ins = ro->modify("INSERT INTO blobtest(data) VALUES(?)"); @@ -380,7 +380,7 @@ BOOST_AUTO_TEST_CASE(blobs) BOOST_REQUIRE_EQUAL(r.value<1>(), "37c7c3737f93e8d17e845deff8fa74d2"); // Assert the fetch of the data is correct BOOST_REQUIRE_EQUAL(r.value<0>().len, buf.size()); - std::string str((const char *)r.value<0>().data, r.value<0>().len); + std::string_view str(reinterpret_cast(r.value<0>().data), r.value<0>().len); BOOST_REQUIRE_EQUAL(str, "This is some binary text data"); BOOST_REQUIRE_EQUAL(r.value<0>(), blob); } @@ -388,7 +388,7 @@ BOOST_AUTO_TEST_CASE(blobs) BOOST_AUTO_TEST_CASE(fetchAsBinary) { - auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); + auto ro = DB::MockDatabase::openConnectionTo("PQmock"); std::vector buf(29); memcpy(&buf[0], "This is some binary text data", 29); DB::Blob blob(buf); @@ -432,10 +432,10 @@ BOOST_AUTO_TEST_CASE(fetchAsBinary) BOOST_AUTO_TEST_CASE(largeBlob) { - auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); + auto ro = DB::MockDatabase::openConnectionTo("PQmock"); ro->execute("TRUNCATE TABLE blobtest"); AdHoc::FileUtils::MemMap f("/proc/self/exe"); - DB::Blob blob(f.data, f.getStat().st_size); + DB::Blob blob(f.data, static_cast(f.getStat().st_size)); BOOST_REQUIRE(blob.len > 140000); // Just assert the mapped file is actually "large" auto ins = ro->modify("INSERT INTO blobtest(data) VALUES(?)"); ins->bindParamBLOB(0, blob); @@ -453,7 +453,7 @@ BOOST_AUTO_TEST_CASE(largeBlob) BOOST_AUTO_TEST_CASE(bulkPerfTest) { - auto ro = DB::ConnectionPtr(DB::MockDatabase::openConnectionTo("PQmock")); + auto ro = DB::MockDatabase::openConnectionTo("PQmock"); auto sel = ro->select(R"SQL(select s a, cast(s as numeric(7,1)) b, cast(s as text) c, make_interval(secs => s) d, make_timestamp(2019,1,1,1,1,1) + make_interval(mins=>s) e, s % 2 = 0 f @@ -462,12 +462,12 @@ BOOST_AUTO_TEST_CASE(bulkPerfTest) int64_t tot = 0; for (const auto & [a, b, c, d, e, f] : sel->as()) { - tot += a + b + c.length() + d.hours() + e.time_of_day().hours() + f; + tot += a + static_cast(b) + static_cast(c.length()) + d.hours() + e.time_of_day().hours() + f; } BOOST_REQUIRE_EQUAL(tot, 1013265); } -BOOST_AUTO_TEST_SUITE_END(); +BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_CASE(connfail) { -- cgit v1.2.3