From 8e46ea6b013276588adad43a8cf455c6a015d1ad Mon Sep 17 00:00:00 2001
From: Bernd Flemisch <bernd@iws.uni-stuttgart.de>
Date: Fri, 5 Jan 2018 11:35:46 +0100
Subject: [PATCH] incoroporate suggestions from cppcheck.

Make one-parameter constructors explicit. Pass parameters by const
reference if copying is potentially expensive. Pass string instead
of const char* to function that expects a string. Fix out-of-bounds
access.
---
 dumux/common/exceptions.hh                    |  4 +-
 dumux/common/properties/propertysystem.hh     |  4 +-
 dumux/io/gnuplotinterface.hh                  | 37 +++++++++----------
 dumux/material/components/co2.hh              |  2 +-
 dumux/material/components/iapws/region1.hh    |  2 +-
 dumux/material/components/iapws/region2.hh    |  2 +-
 .../material/components/tabulatedcomponent.hh |  4 +-
 7 files changed, 27 insertions(+), 28 deletions(-)

diff --git a/dumux/common/exceptions.hh b/dumux/common/exceptions.hh
index 7a5f6fe94b..4cefff885e 100644
--- a/dumux/common/exceptions.hh
+++ b/dumux/common/exceptions.hh
@@ -48,7 +48,7 @@ public:
     {}
 
     // constructor with error message
-    NumericalProblem(const std::string &s)
+    explicit NumericalProblem(const std::string &s)
     { this->message(s); }
 };
 
@@ -69,7 +69,7 @@ public:
     {}
 
     // constructor with error message
-    ParameterException(const std::string &s)
+    explicit ParameterException(const std::string &s)
     { this->message(s); }
 };
 
diff --git a/dumux/common/properties/propertysystem.hh b/dumux/common/properties/propertysystem.hh
index e5adf3cddc..791bdede50 100644
--- a/dumux/common/properties/propertysystem.hh
+++ b/dumux/common/properties/propertysystem.hh
@@ -919,7 +919,7 @@ std::string canonicalTypeTagNameToName_(const std::string &canonicalName)
 inline bool getDiagnostic_(const std::string &typeTagName,
                            const std::string &propTagName,
                            std::string &result,
-                           const std::string indent)
+                           const std::string &indent)
 {
     const PropertyRegistryKey *key = 0;
 
@@ -996,7 +996,7 @@ const std::string getDiagnostic(std::string propTagName)
 
 inline void print_(const std::string &typeTagName,
                    std::ostream &os,
-                   const std::string indent,
+                   const std::string &indent,
                    std::set<std::string> &printedProperties)
 {
     if (indent == "")
diff --git a/dumux/io/gnuplotinterface.hh b/dumux/io/gnuplotinterface.hh
index fc7f150e79..a64dce5ffe 100644
--- a/dumux/io/gnuplotinterface.hh
+++ b/dumux/io/gnuplotinterface.hh
@@ -63,7 +63,7 @@ public:
     using CurveTypeVector = std::vector<CurveType>;
 
     //! \brief The constructor
-    GnuplotInterface(bool persist = true) :
+    explicit GnuplotInterface(bool persist = true) :
         pipe_(0), openPlotWindow_(true), persist_(persist), createImage_(true),
         terminalType_("x11"), outputDirectory_("./"),
         datafileSeparator_(' '), linetype_("solid"),
@@ -151,7 +151,7 @@ public:
 
         interactivePlot += plot;
         if (openPlotWindow_)
-            executeGnuplot(interactivePlot.c_str());
+            executeGnuplot(interactivePlot);
 
         // create a gnuplot file if a filename is specified
         if (filename.compare("") != 0)
@@ -168,7 +168,7 @@ public:
 
           // live plot of the results
           if (createImage_)
-              executeGnuplot(filePlot.c_str());
+              executeGnuplot(filePlot);
         }
     }
 
@@ -213,9 +213,9 @@ public:
     }
 
     DUNE_DEPRECATED_MSG("The signature of addFunctionToPlot(string, string, string) has been changed to addFunctionToPlot(string, string).")
-    void addFunctionToPlot(const std::string function,
-                           const std::string plotName,
-                           const std::string plotOptions)
+    void addFunctionToPlot(const std::string& function,
+                           const std::string& plotName,
+                           const std::string& plotOptions)
     {
         addFunctionToPlot(function, "title '" + plotName + "' " + plotOptions);
     }
@@ -226,8 +226,8 @@ public:
      * \param function Function to be plotted
      * \param options Specific gnuplot options passed to this plot
      */
-    void addFunctionToPlot(const std::string function,
-                           const std::string options = "with lines")
+    void addFunctionToPlot(const std::string& function,
+                           const std::string& options = "with lines")
     {
         curve_.push_back(function);
         curveOptions_.push_back(options);
@@ -235,9 +235,9 @@ public:
     }
 
     DUNE_DEPRECATED_MSG("The signature of addFileToPlot(string, string, string) has been changed to addFileToPlot(string, string).")
-    void addFileToPlot(const std::string file,
-                       const std::string plotName,
-                       const std::string plotOptions)
+    void addFileToPlot(const std::string& file,
+                       const std::string& plotName,
+                       const std::string& plotOptions)
     {
         addFileToPlot(file, "title '" + plotName + "' " + plotOptions);
     }
@@ -248,8 +248,8 @@ public:
      * \param fileName Name and path of the file to be plotted
      * \param options Specific gnuplot options passed to this plot
      */
-    void addFileToPlot(const std::string fileName,
-                       const std::string options = "with lines")
+    void addFileToPlot(const std::string& fileName,
+                       const std::string& options = "with lines")
     {
         curve_.push_back(fileName);
         curveOptions_.push_back(options);
@@ -268,8 +268,8 @@ public:
      */
     void addDataSetToPlot(const std::vector<Scalar>& x,
                           const std::vector<Scalar>& y,
-                          const std::string fileName,
-                          const std::string options = "with lines")
+                          const std::string& fileName,
+                          const std::string& options = "with lines")
     {
         assert(x.size() == y.size());
 
@@ -341,7 +341,7 @@ public:
      *
      * \param option Additional line of option in gnuplot language
      */
-    void setOption(std::string option)
+    void setOption(const std::string& option)
     {
         plotOptions_ += option + "\n";
     }
@@ -391,7 +391,7 @@ public:
      *
      * \param outputDirectory The user-specified terminal
      */
-    void setOutputDirectory(std::string outputDirectory)
+    void setOutputDirectory(const std::string& outputDirectory)
     {
         outputDirectory_ = outputDirectory + "/";
     }
@@ -417,7 +417,7 @@ private:
     }
 
     // Check validity of number
-    void checkNumber(Scalar number, std::string text = "") const
+    void checkNumber(Scalar number, const std::string& text = "") const
     {
         using std::isnan;
         using std::isinf;
@@ -447,7 +447,6 @@ private:
     StringVector curve_;
     StringVector curveOptions_;
     CurveTypeVector curveType_;
-    bool interaction_;
     Scalar xRangeMin_;
     Scalar xRangeMax_;
     Scalar xRangeIsSet_;
diff --git a/dumux/material/components/co2.hh b/dumux/material/components/co2.hh
index 02b340a4dc..dbd8a46cc2 100644
--- a/dumux/material/components/co2.hh
+++ b/dumux/material/components/co2.hh
@@ -136,7 +136,7 @@ public:
         Scalar Tred = T/criticalTemperature();
 
         using std::pow;
-        for (int i = 0; i < 5; ++i)
+        for (int i = 0; i < 4; ++i)
             exponent += a[i]*pow(1 - Tred, t[i]);
         exponent *= 1.0/Tred;
 
diff --git a/dumux/material/components/iapws/region1.hh b/dumux/material/components/iapws/region1.hh
index bdc4f99605..9ca0aa7c73 100644
--- a/dumux/material/components/iapws/region1.hh
+++ b/dumux/material/components/iapws/region1.hh
@@ -60,7 +60,7 @@ public:
      * \param propertyName the name for which property the check is performed
      */
     static void checkValidityRange(Scalar temperature, Scalar pressure,
-                                   std::string propertyName = "This property")
+                                   const std::string& propertyName = "This property")
     {
         // actually this is:
         /* 273.15 <= temperature &&
diff --git a/dumux/material/components/iapws/region2.hh b/dumux/material/components/iapws/region2.hh
index 33c50889ca..076cbb5996 100644
--- a/dumux/material/components/iapws/region2.hh
+++ b/dumux/material/components/iapws/region2.hh
@@ -61,7 +61,7 @@ public:
      * \param propertyName the name for which property the check is performed
      */
     static void checkValidityRange(Scalar temperature, Scalar pressure,
-                                   std::string propertyName = "This property")
+                                   const std::string& propertyName = "This property")
     {
         // actually this is:
         /* (273.15 <= temperature && temperature <= 623.15 && pressure <= vaporPressure(temperature)) ||
diff --git a/dumux/material/components/tabulatedcomponent.hh b/dumux/material/components/tabulatedcomponent.hh
index feeb2c47d9..eaf559061b 100644
--- a/dumux/material/components/tabulatedcomponent.hh
+++ b/dumux/material/components/tabulatedcomponent.hh
@@ -113,7 +113,7 @@ public:
 
             try { vaporPressure_[iT] = RawComponent::vaporPressure(temperature); }
             catch (Dune::NotImplemented) { vaporPressure_[iT] = NaN; }
-            catch (NumericalProblem e) { vaporPressure_[iT] = NaN; }
+            catch (NumericalProblem) { vaporPressure_[iT] = NaN; }
 
             Scalar pgMax = maxGasPressure_(iT);
             Scalar pgMin = minGasPressure_(iT);
@@ -569,7 +569,7 @@ public:
 private:
     // prints a warning if the result is not in range or the table has
     // not been initialized
-    static void printWarning_(std::string quantity, Scalar arg1, Scalar arg2)
+    static void printWarning_(const std::string& quantity, Scalar arg1, Scalar arg2)
     {
 #ifndef NDEBUG
         if (warningPrinted_)
-- 
GitLab