]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[4252] CID 1327386. CSVFile::getColumnIndex must not return negative value.
authorMarcin Siodelski <marcin@isc.org>
Tue, 6 Sep 2016 14:15:44 +0000 (16:15 +0200)
committerMarcin Siodelski <marcin@isc.org>
Tue, 6 Sep 2016 14:16:51 +0000 (16:16 +0200)
src/lib/util/csv_file.cc
src/lib/util/csv_file.h
src/lib/util/versioned_csv_file.cc

index d98363e7352beb76e1ec4aa824a78b03a674dc11..4c746e2f3ab4a3142ca341163b40c76e83dc41b7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -8,6 +8,7 @@
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/constants.hpp>
 #include <boost/algorithm/string/split.hpp>
+#include <algorithm>
 #include <fstream>
 #include <sstream>
 
@@ -122,7 +123,7 @@ CSVFile::addColumn(const std::string& col_name) {
 
 void
 CSVFile::addColumnInternal(const std::string& col_name) {
-    if (getColumnIndex(col_name) >= 0) {
+    if (std::find(cols_.begin(), cols_.end(), col_name) != cols_.end()) {
         isc_throw(CSVFileError, "attempt to add duplicate column '"
                   << col_name << "'");
     }
@@ -199,14 +200,14 @@ CSVFile::size() const {
     return (pos);
 }
 
-int
+size_t
 CSVFile::getColumnIndex(const std::string& col_name) const {
     for (size_t i = 0; i < cols_.size(); ++i) {
         if (cols_[i] == col_name) {
-            return (static_cast<int>(i));
+            return (i);
         }
     }
-    return (-1);
+    isc_throw(isc::OutOfRange, "column '" << col_name << "' doesn't exist");
 }
 
 std::string
index c32f77c47d3d4036c65b1305123721ac26fb7d2f..03a47837480680573d92620a304793e98c1dfb89 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -354,9 +354,9 @@ public:
     /// This function is exception safe.
     ///
     /// @param col_name Name of the column.
-    /// @return Index of the column or negative value if the column doesn't
-    /// exist.
-    int getColumnIndex(const std::string& col_name) const;
+    /// @return Index of the column.
+    /// @throw OutOfRange if column with such name doesn't exist.
+    size_t getColumnIndex(const std::string& col_name) const;
 
     /// @brief Returns the name of the column.
     ///
index c9c1507e64f172cba664d2bb247ec3123f449134..fc03b551262e6f93a97b43ecce3f93b4c5226980 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -29,13 +29,15 @@ VersionedCSVFile::addColumn(const std::string& name,
 
 void
 VersionedCSVFile::setMinimumValidColumns(const std::string& column_name) {
-    int index = getColumnIndex(column_name);
-    if (index <  0) {
+    try {
+        int index = getColumnIndex(column_name);
+        minimum_valid_columns_ = index + 1;
+
+    } catch (...) {
         isc_throw(VersionedCSVFileError,
-                  "setMinimumValidColumns: " << column_name << " is defined");
+                  "setMinimumValidColumns: " << column_name << " is not "
+                  "defined");
     }
-
-    minimum_valid_columns_ = index + 1;
 }
 
 size_t