Browse Source

[3430] Changes in a bid to avoid a Coverity warning

Coverity picked up that a construct like "&array[ARRAY_LENGTH]" is
an access to an invalid element, although it is a valid marker
as the end of an STL range.  This change alters that construct
to "array + ARRAY_LENGTH".
Stephen Morris 10 years ago
parent
commit
ccfd61b4b9
2 changed files with 14 additions and 14 deletions
  1. 4 4
      src/lib/dhcp/pkt4.cc
  2. 10 10
      src/lib/dhcp/tests/pkt4_unittest.cc

+ 4 - 4
src/lib/dhcp/pkt4.cc

@@ -366,8 +366,8 @@ Pkt4::setSname(const uint8_t* sname, size_t snameLen /*= MAX_SNAME_LEN*/) {
         isc_throw(InvalidParameter, "Invalid sname specified");
         isc_throw(InvalidParameter, "Invalid sname specified");
     }
     }
 
 
-    std::copy(&sname[0], &sname[snameLen], &sname_[0]);
-    std::fill(&sname_[snameLen], &sname_[MAX_SNAME_LEN], 0);
+    std::copy(sname, (sname + snameLen), sname_);
+    std::fill((sname_ + snameLen), (sname_ + MAX_SNAME_LEN), 0);
 
 
     // No need to store snameLen as any empty space is filled with 0s
     // No need to store snameLen as any empty space is filled with 0s
 }
 }
@@ -382,8 +382,8 @@ Pkt4::setFile(const uint8_t* file, size_t fileLen /*= MAX_FILE_LEN*/) {
         isc_throw(InvalidParameter, "Invalid file name specified");
         isc_throw(InvalidParameter, "Invalid file name specified");
     }
     }
 
 
-    std::copy(&file[0], &file[fileLen], &file_[0]);
-    std::fill(&file_[fileLen], &file_[MAX_FILE_LEN], 0);
+    std::copy(file, (file + fileLen), file_);
+    std::fill((file_ + fileLen), (file_ + MAX_FILE_LEN), 0);
 
 
     // No need to store fileLen as any empty space is filled with 0s
     // No need to store fileLen as any empty space is filled with 0s
 }
 }

+ 10 - 10
src/lib/dhcp/tests/pkt4_unittest.cc

@@ -491,12 +491,12 @@ TEST_F(Pkt4Test, sname) {
 
 
     scoped_ptr<Pkt4> pkt;
     scoped_ptr<Pkt4> pkt;
     // Let's test each sname length, from 0 till 64
     // Let's test each sname length, from 0 till 64
-    for (int snameLen = 0; snameLen < Pkt4::MAX_SNAME_LEN; snameLen++) {
-        for (int i = 0; i < Pkt4::MAX_SNAME_LEN; i++) {
-            sname[i] = 0;
+    for (int snameLen = 0; snameLen < Pkt4::MAX_SNAME_LEN; ++snameLen) {
+        for (int i = 0; i < snameLen; ++i) {
+            sname[i] = i + 1;
         }
         }
-        for (int i = 0; i < snameLen; i++) {
-            sname[i] = i;
+        for (int i = snameLen; i < Pkt4::MAX_SNAME_LEN; ++i) {
+            sname[i] = 0;
         }
         }
 
 
         // Type and transaction doesn't matter in this test
         // Type and transaction doesn't matter in this test
@@ -529,12 +529,12 @@ TEST_F(Pkt4Test, file) {
 
 
     scoped_ptr<Pkt4> pkt;
     scoped_ptr<Pkt4> pkt;
     // Let's test each file length, from 0 till 128.
     // Let's test each file length, from 0 till 128.
-    for (int fileLen = 0; fileLen < Pkt4::MAX_FILE_LEN; fileLen++) {
-        for (int i = 0; i < Pkt4::MAX_FILE_LEN; i++) {
-            file[i] = 0;
+    for (int fileLen = 0; fileLen < Pkt4::MAX_FILE_LEN; ++fileLen) {
+        for (int i = 0; i < fileLen; ++i) {
+            file[i] = i + 1;
         }
         }
-        for (int i = 0; i < fileLen; i++) {
-            file[i] = i;
+        for (int i = fileLen; i < Pkt4::MAX_FILE_LEN; ++i) {
+            file[i] = 0;
         }
         }
 
 
         // Type and transaction doesn't matter in this test.
         // Type and transaction doesn't matter in this test.