Petr Onderka has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78953


Change subject: Use at() for bounds checking
......................................................................

Use at() for bounds checking

Change-Id: If5b475263e3256d72d3880cf5aed12815e42aa2b
---
M DumpObjects/DumpTraits.h
M DumpObjects/FileHeader.cpp
M DumpWriters/DumpWriter.cpp
M Indexes/IndexInnerNode.tpp
M Objects/IpV4User.cpp
M Objects/IpV6User.cpp
M Objects/Timestamp.cpp
M XmlUtils.cpp
M main.cpp
9 files changed, 65 insertions(+), 61 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/operations/dumps/incremental 
refs/changes/53/78953/1

diff --git a/DumpObjects/DumpTraits.h b/DumpObjects/DumpTraits.h
index 1f2cff2..4916e04 100644
--- a/DumpObjects/DumpTraits.h
+++ b/DumpObjects/DumpTraits.h
@@ -54,14 +54,14 @@
 public:
     static T Read(istream &stream)
     {
-        char bytes[Size];
+        std::array<char, Size> bytes;
 
-        stream.read(bytes, Size);
+        stream.read(&bytes.at(0), Size);
 
         T result = 0;
         for (int i = 0; i < Size; i++)
         {
-            result |= (T)(std::uint8_t)bytes[i] << (8 * i);
+            result |= (T)(std::uint8_t)bytes.at(i) << (8 * i);
         }
 
         return result;
@@ -69,14 +69,14 @@
 
     static void Write(ostream &stream, const T value)
     {
-        char bytes[Size];
+        std::array<char, Size> bytes;
 
         for (int i = 0; i < Size; i++)
         {
-            bytes[i] = (value >> (8 * i)) & 0xFF;
+            bytes.at(i) = (value >> (8 * i)) & 0xFF;
         }
 
-        stream.write(bytes, Size);
+        stream.write(&bytes.at(0), Size);
     }
 
     static uint32_t DumpSize(const T value = 0)
@@ -186,15 +186,16 @@
 };
 
 template<>
-class DumpTraits<string>
+class DumpTraits<std::string>
 {
 private:
-    static string ReadCore(istream &stream, uint32_t count)
+    static string ReadCore(std::istream &stream, std::uint32_t count)
     {
-        auto bytes = unique_ptr<char[]>(new char[count]);
-        stream.read(bytes.get(), count);
+        std::string bytes(count, '\0');
 
-        return string(bytes.get(), count);
+        stream.read(&bytes.at(0), count);
+
+        return bytes;
     }
 
 public:
@@ -319,7 +320,7 @@
 
         for (int i = 0; i < N; i++)
         {
-            result[i] = DumpTraits<T>::Read(stream);
+            result.at(i) = DumpTraits<T>::Read(stream);
         }
 
         return result;
diff --git a/DumpObjects/FileHeader.cpp b/DumpObjects/FileHeader.cpp
index 82af112..dde780d 100644
--- a/DumpObjects/FileHeader.cpp
+++ b/DumpObjects/FileHeader.cpp
@@ -42,9 +42,9 @@
 {
     istream &stream = *(dump.stream);
 
-    char bytes[6];
-    stream.read(bytes, 6);
-    if (strncmp(bytes, "WMID", 4) != 0 || bytes[4] != FileFormatVersion || 
bytes[5] != FileDataVersion)
+    std::string bytes(6, '\0');
+    stream.read(&bytes.at(0), 6);
+    if (bytes.substr(0, 4) != "WMID" || bytes.at(4) != FileFormatVersion || 
bytes.at(5) != FileDataVersion)
         throw new DumpException();
 
     DumpKind kind = DumpTraits<DumpKind>::Read(stream);
diff --git a/DumpWriters/DumpWriter.cpp b/DumpWriters/DumpWriter.cpp
index 48c6925..a8db886 100644
--- a/DumpWriters/DumpWriter.cpp
+++ b/DumpWriters/DumpWriter.cpp
@@ -12,7 +12,7 @@
         std::uint32_t pageId = pair.first;
         if (unvisitedPageIds.size() <= pageId)
             unvisitedPageIds.resize(pageId + 1);
-        unvisitedPageIds[pageId] = true;
+        unvisitedPageIds.at(pageId) = true;
     }
 }
 
@@ -29,7 +29,7 @@
     oldRevisionIds = this->page->page.RevisionIds;
     this->page->page = *page;
     if (pageId < unvisitedPageIds.size())
-        unvisitedPageIds[pageId] = false;
+        unvisitedPageIds.at(pageId) = false;
 }
 
 void DumpWriter::AddRevision(const std::shared_ptr<const Revision> revision)
@@ -71,7 +71,7 @@
 {
     for (std::uint32_t i = 0; i < unvisitedPageIds.size(); i++)
     {
-        if (unvisitedPageIds[i])
+        if (unvisitedPageIds.at(i))
             dump->DeletePage(i);
     }
 
diff --git a/Indexes/IndexInnerNode.tpp b/Indexes/IndexInnerNode.tpp
index 5e82fed..65c2fae 100644
--- a/Indexes/IndexInnerNode.tpp
+++ b/Indexes/IndexInnerNode.tpp
@@ -9,12 +9,12 @@
 template<typename TKey, typename TValue>
 IndexNode<TKey, TValue>* IndexInnerNode<TKey, 
TValue>::GetChildByIndex(std::uint16_t index)
 {
-    if (cachedChildren[index] == nullptr)
+    if (cachedChildren.at(index) == nullptr)
     {
-        cachedChildren[index] = IndexNode<TKey, TValue>::Read(this->dump, 
childOffsets[index].value);
+        cachedChildren.at(index) = IndexNode<TKey, TValue>::Read(this->dump, 
childOffsets.at(index).value);
     }
 
-    return cachedChildren[index].get();
+    return cachedChildren.at(index).get();
 }
 
 template<typename TKey, typename TValue>
@@ -48,8 +48,8 @@
 
         insert_at(keys, updatedChildIndex, splitted.SplitKey);
 
-        childOffsets[updatedChildIndex] = splitted.LeftNode->SavedOffset();
-        cachedChildren[updatedChildIndex] = std::move(splitted.LeftNode);
+        childOffsets.at(updatedChildIndex) = splitted.LeftNode->SavedOffset();
+        cachedChildren.at(updatedChildIndex) = std::move(splitted.LeftNode);
 
         insert_at(childOffsets, updatedChildIndex + 1, 
splitted.RightNode->SavedOffset());
         insert_at(cachedChildren, updatedChildIndex + 1, 
std::move(splitted.RightNode));
@@ -188,27 +188,27 @@
 
     for (; i < middle; i++)
     {
-        left->keys.push_back(keys[i]);
-        left->childOffsets.push_back(childOffsets[i]);
-        left->cachedChildren.push_back(std::move(cachedChildren[i]));
+        left->keys.push_back(keys.at(i));
+        left->childOffsets.push_back(childOffsets.at(i));
+        left->cachedChildren.push_back(std::move(cachedChildren.at(i)));
     }
 
-    left->childOffsets.push_back(childOffsets[i]);
-    left->cachedChildren.push_back(std::move(cachedChildren[i]));
+    left->childOffsets.push_back(childOffsets.at(i));
+    left->cachedChildren.push_back(std::move(cachedChildren.at(i)));
 
-    TKey middleKey = keys[i];
+    TKey middleKey = keys.at(i);
 
     i++;
 
     for (; i < keys.size(); i++)
     {
-        right->keys.push_back(keys[i]);
-        right->childOffsets.push_back(childOffsets[i]);
-        right->cachedChildren.push_back(std::move(cachedChildren[i]));
+        right->keys.push_back(keys.at(i));
+        right->childOffsets.push_back(childOffsets.at(i));
+        right->cachedChildren.push_back(std::move(cachedChildren.at(i)));
     }
 
-    right->childOffsets.push_back(childOffsets[i]);
-    right->cachedChildren.push_back(std::move(cachedChildren[i]));
+    right->childOffsets.push_back(childOffsets.at(i));
+    right->cachedChildren.push_back(std::move(cachedChildren.at(i)));
 
     return SplitResult(
         std::unique_ptr<IndexNode<TKey, TValue>>(left),
diff --git a/Objects/IpV4User.cpp b/Objects/IpV4User.cpp
index e32395f..849d759 100644
--- a/Objects/IpV4User.cpp
+++ b/Objects/IpV4User.cpp
@@ -20,7 +20,7 @@
     for (int i = 0; i < 4; i++)
     {
         bool success;
-        long intPart = tryParseLong(stringParts[i], success);
+        long intPart = tryParseLong(stringParts.at(i), success);
         if (!success || intPart > 255 || intPart < 0)
             return 0;
         
diff --git a/Objects/IpV6User.cpp b/Objects/IpV6User.cpp
index 3d8c7bf..25270c3 100644
--- a/Objects/IpV6User.cpp
+++ b/Objects/IpV6User.cpp
@@ -20,11 +20,11 @@
     for (int i = 0; i < 8; i++)
     {
         bool success;
-        long intPart = tryParseLong(stringParts[i], success, 16);
+        long intPart = tryParseLong(stringParts.at(i), success, 16);
         if (!success || intPart > 0xFFFF || intPart < 0)
             return std::array<uint16_t, 8>();
         
-        result[i] = intPart;
+        result.at(i) = intPart;
     }
 
     success = true;
@@ -53,7 +53,7 @@
         if (i != 0)
             s << ':';
 
-        s << address[i];
+        s << address.at(i);
     }
 
     return s.str();
diff --git a/Objects/Timestamp.cpp b/Objects/Timestamp.cpp
index 29aa013..f1837cc 100644
--- a/Objects/Timestamp.cpp
+++ b/Objects/Timestamp.cpp
@@ -2,6 +2,7 @@
 
 #include <sstream>
 #include <cstdio>
+#include <array>
 
 using std::istringstream;
 
@@ -32,20 +33,20 @@
 {
     istringstream stream(s);
 
-    unsigned dateParts[6];
+    std::array<unsigned, 6> dateParts;
 
     for (int i = 0; i < 6; i++)
     {
         char delim;
-        stream >> dateParts[i] >> delim;
+        stream >> dateParts.at(i) >> delim;
     }
 
-    Year = dateParts[0];
-    Month = dateParts[1];
-    Day = dateParts[2];
-    Hour = dateParts[3];
-    Minute = dateParts[4];
-    Second = dateParts[5];
+    Year = std::get<0>(dateParts);
+    Month = std::get<1>(dateParts);;
+    Day = std::get<2>(dateParts);;
+    Hour = std::get<3>(dateParts);;
+    Minute = std::get<4>(dateParts);;
+    Second = std::get<5>(dateParts);;
 }
 
 uint32_t Timestamp::ToInteger() const
diff --git a/XmlUtils.cpp b/XmlUtils.cpp
index f0304c3..8d613af 100644
--- a/XmlUtils.cpp
+++ b/XmlUtils.cpp
@@ -100,13 +100,13 @@
         pos = foundPos;
         stream << original.substr(prevPos, pos - prevPos);
 
-        if (original[pos] == '<')
+        if (original.at(pos) == '<')
             stream << "&lt;";
-        else if (original[pos] == '>')
+        else if (original.at(pos) == '>')
             stream << "&gt;";
-        else if (original[pos] == '"')
+        else if (original.at(pos) == '"')
             stream << "&quot;";
-        else if (original[pos] == '&')
+        else if (original.at(pos) == '&')
             stream << "&amp;";
         else
             throw DumpException();
diff --git a/main.cpp b/main.cpp
index 5288871..e7852d1 100644
--- a/main.cpp
+++ b/main.cpp
@@ -40,17 +40,17 @@
     parameters.pop();
 
     bool withText;
-    if (spec[0] == 'p')
+    if (spec.at(0) == 'p')
         withText = true;
-    else if (spec[0] == 's')
+    else if (spec.at(0) == 's')
         withText = false;
     else
         return nullResult;
 
     bool current;
-    if (spec[1] == 'c')
+    if (spec.at(1) == 'c')
         current = true;
-    else if (spec[1] == 'h')
+    else if (spec.at(1) == 'h')
         current = false;
     else
         return nullResult;
@@ -58,7 +58,7 @@
     bool articles = false;
     if (spec.length() == 3)
     {
-        if (spec[2] == 'a')
+        if (spec.at(2) == 'a')
             articles = true;
         else
             return nullResult;
@@ -176,14 +176,16 @@
         return 0;
     }
 
-    string action = argv[1];
+    std::vector<std::string> args(argv, argv + argc);
+
+    string action = args.at(1);
 
     if (action == "c" || action == "create")
     {
         std::queue<std::string> parameters;
 
-        for (int i = 2; i < argc; i++)
-            parameters.push(argv[i]);
+        for (size_t i = 2; i < args.size(); i++)
+            parameters.push(args.at(i));
 
         if (!createDump(parameters))
         {
@@ -195,8 +197,8 @@
     {
         std::queue<std::string> parameters;
 
-        for (int i = 2; i < argc; i++)
-            parameters.push(argv[i]);
+        for (size_t i = 2; i < args.size(); i++)
+            parameters.push(args.at(i));
 
         if (!updateDump(parameters))
         {
@@ -213,7 +215,7 @@
         }
         else
         {
-            readDump(argv[2], argv[3]);
+            readDump(args.at(2), args.at(3));
         }
     }
     else

-- 
To view, visit https://gerrit.wikimedia.org/r/78953
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If5b475263e3256d72d3880cf5aed12815e42aa2b
Gerrit-PatchSet: 1
Gerrit-Project: operations/dumps/incremental
Gerrit-Branch: gsoc
Gerrit-Owner: Petr Onderka <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to