]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
buffer::ptr::cmp only compares up to the smallest length
authorLoic Dachary <loic@dachary.org>
Sun, 17 Feb 2013 08:41:09 +0000 (09:41 +0100)
committerSage Weil <sage@inktank.com>
Sun, 17 Feb 2013 17:46:35 +0000 (09:46 -0800)
When running

  bufferptr a("A", 1);
  bufferptr ab("AB", 2);
  a.cmp(ab);

it returned zero because. cmp only compared up to the length of the
smallest buffer and returned if they are identical. The function is
modified to compare the length of the buffers instead of returning.

http://tracker.ceph.com/issues/4170 refs #4170

Signed-off-by: Loic Dachary <loic@dachary.org>
src/common/buffer.cc
src/test/bufferlist.cc

index e10d6c9038c2ffc9992c3f8fb2652603456421f4..df50cfccc422095d9979d9441a6c53833f8222c6 100644 (file)
@@ -371,7 +371,7 @@ bool buffer_track_alloc = get_env_bool("CEPH_BUFFER_TRACK");
     int l = _len < o._len ? _len : o._len;
     if (l) {
       int r = memcmp(c_str(), o.c_str(), l);
-      if (!r)
+      if (r)
        return r;
     }
     if (_len < o._len)
index 71c2e793c8260dd08b5410e28892a64bd9781ca1..7abced105548f6a9ea028c99f20d1debac20c24e 100644 (file)
@@ -9,6 +9,23 @@
 
 #define MAX_TEST 1000000
 
+TEST(BufferPtr, cmp) {
+  bufferptr empty;
+  bufferptr a("A", 1);
+  bufferptr ab("AB", 2);
+  bufferptr af("AF", 2);
+  bufferptr acc("ACC", 3);
+  EXPECT_GE(-1, empty.cmp(a));
+  EXPECT_LE(1, a.cmp(empty));
+  EXPECT_GE(-1, a.cmp(ab));
+  EXPECT_LE(1, ab.cmp(a));
+  EXPECT_EQ(0, ab.cmp(ab));
+  EXPECT_GE(-1, ab.cmp(af));
+  EXPECT_LE(1, af.cmp(ab));
+  EXPECT_GE(-1, acc.cmp(af));
+  EXPECT_LE(1, af.cmp(acc));
+}
+
 TEST(BufferList, zero) {
   //
   // void zero()