From 0e29b0b0f918e14dbb50840101bdc9497d8005b7 Mon Sep 17 00:00:00 2001 From: Alexey Antipovsky Date: Tue, 10 Nov 2020 18:23:21 +0000 Subject: [PATCH] Fix -Wtype-limits --- dbcon/ddlpackage/sqlparser.cpp | 2 +- dbcon/dmlpackage/dmlparser.cpp | 2 +- dbcon/joblist/pdictionaryscan.cpp | 3 +- dbcon/joblist/windowfunctionstep.cpp | 4 ++- procmon/main.cpp | 3 +- storage-manager/src/IOCoordinator.cpp | 3 +- utils/common/checks.h | 43 ++++++++++++++++++++++++ utils/funcexp/func_cast.cpp | 4 ++- utils/funcexp/func_elt.cpp | 8 +++-- writeengine/dictionary/we_dctnry.cpp | 3 +- writeengine/server/we_dmlcommandproc.cpp | 19 ++++++----- 11 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 utils/common/checks.h diff --git a/dbcon/ddlpackage/sqlparser.cpp b/dbcon/ddlpackage/sqlparser.cpp index 4dbd8ccfa..ba0f23093 100644 --- a/dbcon/ddlpackage/sqlparser.cpp +++ b/dbcon/ddlpackage/sqlparser.cpp @@ -126,7 +126,7 @@ int SqlFileParser::Parse(const string& sqlfile) throw length_error("SqlFileParser has file size hard limit of 16K."); } - unsigned rcount; + std::streamsize rcount; rcount = ifsql.readsome(sqlbuf, sizeof(sqlbuf) - 1); if (rcount < 0) diff --git a/dbcon/dmlpackage/dmlparser.cpp b/dbcon/dmlpackage/dmlparser.cpp index caedee11a..b21caee07 100644 --- a/dbcon/dmlpackage/dmlparser.cpp +++ b/dbcon/dmlpackage/dmlparser.cpp @@ -148,7 +148,7 @@ int DMLFileParser::parse(const string& fileName) throw length_error("DMLFileParser has file size hard limit of 16K."); } - unsigned rcount; + std::streamsize rcount; rcount = ifdml.readsome(dmlbuf, sizeof(dmlbuf) - 1); if (rcount < 0) diff --git a/dbcon/joblist/pdictionaryscan.cpp b/dbcon/joblist/pdictionaryscan.cpp index 4532b1980..d4810a0fc 100644 --- a/dbcon/joblist/pdictionaryscan.cpp +++ b/dbcon/joblist/pdictionaryscan.cpp @@ -65,6 +65,7 @@ using namespace rowgroup; using namespace querytele; #include "threadnaming.h" +#include "checks.h" namespace joblist { @@ -496,7 +497,7 @@ void pDictionaryScan::sendAPrimitiveMessage( hdr.Hdr.Priority = priority(); hdr.LBID = msgLbidStart; - idbassert(hdr.LBID >= 0); + idbassert(utils::is_nonnegative(hdr.LBID)); hdr.OutputType = OT_TOKEN; hdr.BOP = fBOP; hdr.COP1 = fCOP1; diff --git a/dbcon/joblist/windowfunctionstep.cpp b/dbcon/joblist/windowfunctionstep.cpp index 48c81327c..58b40779a 100755 --- a/dbcon/joblist/windowfunctionstep.cpp +++ b/dbcon/joblist/windowfunctionstep.cpp @@ -78,6 +78,8 @@ using namespace querytele; #include "windowfunctionstep.h" using namespace joblist; +#include "checks.h" + namespace { @@ -722,7 +724,7 @@ void WindowFunctionStep::initialize(const RowGroup& rg, JobInfo& jobInfo) string fn = boost::to_upper_copy(wc->functionName()); if ( (fn == "MEDIAN" || fn == "PERCENTILE_CONT" || fn == "PERCENTILE_DISC") && - peerIdx[0] >= 0 && peerIdx[0] < types.size() ) + utils::is_nonnegative(peerIdx[0]) && peerIdx[0] < types.size() ) ct = types[peerIdx[0]]; // create the functor based on function name diff --git a/procmon/main.cpp b/procmon/main.cpp index c278c3680..2daaaee4b 100644 --- a/procmon/main.cpp +++ b/procmon/main.cpp @@ -28,6 +28,7 @@ namespace bi = boost::interprocess; #include "IDBPolicy.h" #include "utils_utf8.h" #include "crashtrace.h" +#include "checks.h" using namespace std; using namespace messageqcpp; @@ -2594,7 +2595,7 @@ void processStatusMSG(messageqcpp::IOSocket* cfIos) } } - if ( PID < 0 ) + if (!utils::is_nonnegative(PID)) PID = 0; log.writeLog(__LINE__, "statusControl: Set Process " + moduleName + "/" + processName + + " State = " + oamState[state] + " PID = " + oam.itoa(PID), LOG_TYPE_DEBUG); diff --git a/storage-manager/src/IOCoordinator.cpp b/storage-manager/src/IOCoordinator.cpp index 5aa31dca4..25a856b75 100644 --- a/storage-manager/src/IOCoordinator.cpp +++ b/storage-manager/src/IOCoordinator.cpp @@ -29,6 +29,7 @@ #define BOOST_SPIRIT_THREADSAFE #include #include +#include "checks.h" #define max(x, y) (x > y ? x : y) #define min(x, y) (x < y ? x : y) @@ -823,7 +824,7 @@ int IOCoordinator::_truncate(const bf::path &bfpath, size_t newSize, ScopedFileL else { meta.updateEntryLength(objects[0].offset, newSize - objects[0].offset); - assert(objects[0].offset >= 0 && objectSize > (newSize - objects[0].offset)); + assert(utils::is_nonnegative(objects[0].offset) && objectSize > (newSize - objects[0].offset)); } for (uint i = 1; i < objects.size(); i++) meta.removeEntry(objects[i].offset); diff --git a/utils/common/checks.h b/utils/common/checks.h new file mode 100644 index 000000000..36b87312e --- /dev/null +++ b/utils/common/checks.h @@ -0,0 +1,43 @@ +/* Copyright (C) 2020 MariaDB Corporation + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; version 2 of + the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. */ +#ifndef UTILS_COMMON_CHECKS_H +#define UTILS_COMMON_CHECKS_H + +#include + +namespace utils { + +template +typename std::enable_if::value, bool>::type is_nonnegative(T) +{ return true; }; + +template +typename std::enable_if::value, bool>::type is_nonnegative(T v) +{ return v >= 0; }; + + +template +typename std::enable_if::value, bool>::type is_negative(T) +{ return false; }; + +template +typename std::enable_if::value, bool>::type is_negative(T v) +{ return v < 0; }; + +} // namespace utils + +#endif // UTILS_COMMON_CHECKS_H diff --git a/utils/funcexp/func_cast.cpp b/utils/funcexp/func_cast.cpp index 33dad38cd..3584b7102 100644 --- a/utils/funcexp/func_cast.cpp +++ b/utils/funcexp/func_cast.cpp @@ -43,6 +43,8 @@ using namespace logging; using namespace dataconvert; #include "collation.h" +#include "checks.h" + namespace { struct lconv* convData = localeconv(); @@ -349,7 +351,7 @@ uint64_t Func_cast_unsigned::getUintVal(Row& row, uint64_t value = d.value / pow(10.0, dscale); int lefto = (d.value - value * pow(10.0, dscale)) / pow(10.0, dscale - 1); - if ( value >= 0 && lefto > 4 ) + if ( utils::is_nonnegative(value) && lefto > 4 ) value++; return value; diff --git a/utils/funcexp/func_elt.cpp b/utils/funcexp/func_elt.cpp index 99c86af75..ccdca2cae 100644 --- a/utils/funcexp/func_elt.cpp +++ b/utils/funcexp/func_elt.cpp @@ -34,6 +34,8 @@ using namespace execplan; #include "dataconvert.h" using namespace dataconvert; +#include "checks.h" + namespace funcexp { @@ -75,10 +77,10 @@ string Func_elt::getStrVal(rowgroup::Row& row, number = d.value / pow(10.0, dscale); int lefto = (d.value - number * pow(10.0, dscale)) / pow(10.0, dscale - 1); - if ( number >= 0 && lefto > 4 ) + if ( utils::is_nonnegative(number) && lefto > 4 ) number++; - if ( number < 0 && lefto < -4 ) + if ( utils::is_negative(number) && lefto < -4 ) number--; break; @@ -102,7 +104,7 @@ string Func_elt::getStrVal(rowgroup::Row& row, } std::string ret; - stringValue(parm[number], row, isNull, ret); + stringValue(parm[number], row, isNull, ret); return ret; } diff --git a/writeengine/dictionary/we_dctnry.cpp b/writeengine/dictionary/we_dctnry.cpp index aacfdfd7a..ac54bba2c 100644 --- a/writeengine/dictionary/we_dctnry.cpp +++ b/writeengine/dictionary/we_dctnry.cpp @@ -49,6 +49,7 @@ using namespace BRM; #include "cacheutils.h" using namespace idbdatafile; #include "utils_utf8.h" +#include "checks.h" namespace { @@ -407,7 +408,7 @@ int Dctnry::closeDctnry(bool realClose) } m_hwm = (HWM)m_lastFbo; - idbassert(m_dctnryOID >= 0); + idbassert(utils::is_nonnegative(m_dctnryOID)); if (idbdatafile::IDBPolicy::useHdfs() && realClose) { diff --git a/writeengine/server/we_dmlcommandproc.cpp b/writeengine/server/we_dmlcommandproc.cpp index bf7c7f66e..1cfcd712b 100644 --- a/writeengine/server/we_dmlcommandproc.cpp +++ b/writeengine/server/we_dmlcommandproc.cpp @@ -51,6 +51,9 @@ using namespace BRM; #include "cacheutils.h" #include "IDBDataFile.h" #include "IDBPolicy.h" + +#include "checks.h" + namespace WriteEngine { //StopWatch timer; @@ -1839,7 +1842,7 @@ uint8_t WE_DMLCommandProc::processBatchInsertBinary(messageqcpp::ByteStream& bs, { bs >> val8; - if (val8 < 0 && + if (utils::is_negative(val8) && val8 != static_cast(joblist::TINYINTEMPTYROW) && val8 != static_cast(joblist::TINYINTNULL)) { @@ -1853,7 +1856,7 @@ uint8_t WE_DMLCommandProc::processBatchInsertBinary(messageqcpp::ByteStream& bs, { bs >> val16; - if (val16 < 0 && + if (utils::is_negative(val16) && val16 != static_cast(joblist::SMALLINTEMPTYROW) && val16 != static_cast(joblist::SMALLINTNULL)) { @@ -1867,9 +1870,9 @@ uint8_t WE_DMLCommandProc::processBatchInsertBinary(messageqcpp::ByteStream& bs, { bs >> val32; - if (val32 < 0 && - val32 != static_cast(joblist::INTEMPTYROW) && - val32 != static_cast(joblist::INTNULL)) + if (utils::is_negative(val32) && + val32 != joblist::INTEMPTYROW && + val32 != joblist::INTNULL) { val32 = 0; pushWarning = true; @@ -1881,9 +1884,9 @@ uint8_t WE_DMLCommandProc::processBatchInsertBinary(messageqcpp::ByteStream& bs, { bs >> val64; - if (val64 < 0 && - val64 != static_cast(joblist::BIGINTEMPTYROW) && - val64 != static_cast(joblist::BIGINTNULL)) + if (utils::is_negative(val64) && + val64 != joblist::BIGINTEMPTYROW && + val64 != joblist::BIGINTNULL) { val64 = 0; pushWarning = true;