Skip to content

[BUG] Google Test ChainTest::EqualityOperator crashes #9

@FrozenLemonTee

Description

@FrozenLemonTee

When enable Rlease optimization:

set(CMAKE_BUILD_TYPE Release)

This test crashes:

// 测试是否等于操作符
TEST(ChainTest, EqualityOperator) {
chain c1 = {1, 2, 3};
chain c2 = {1, 2, 3};
const chain c3 = c2;
EXPECT_TRUE(c1 == c2);
EXPECT_TRUE(c1 == c3);
c2.pushEnd(4);
EXPECT_FALSE(c1 == c2);
c1.pushEnd(5);
EXPECT_FALSE(c1 == c3);
} // TODO: Also

But test it in a simple file whitout gtest:

int main(){
      original::chain c1 = {1, 2, 3};
      original::chain c2 = {1, 2, 3};
      const original::chain c3 = c2;
      std::cout << "Expect c1 == c2 true, actual: " <<  original::printable::formatString(c1 == c2)  << std::endl;
      std::cout << "Expect c1 == c3 true, actual: " <<  original::printable::formatString(c1 == c3)  << std::endl;
      c2.pushEnd(4);
      std::cout << "Expect c1 == c2 false, actual: " <<  original::printable::formatString(c1 == c2)  << std::endl;
      c1.pushEnd(5);
      std::cout << "Expect c1 == c3 false, actual: " <<  original::printable::formatString(c1 == c3)  << std::endl;
}

It's OK.
Toggle at least one comparison assertion line to comment:

  // 测试是否等于操作符
  TEST(ChainTest, EqualityOperator) {
      chain c1 = {1, 2, 3};
      chain c2 = {1, 2, 3};
      const chain c3 = c2;
      EXPECT_TRUE(c1 == c2);
      EXPECT_TRUE(c1 == c3);
      c2.pushEnd(4);
      // EXPECT_FALSE(c1 == c2); // Toggle to comment
      c1.pushEnd(5);
      EXPECT_FALSE(c1 == c3);
  }

Also OK.

Check Asan after crash:

chain.h

/dev/src/core/chain.h

Unknown-crash on address 0x7ffc32b4bd78 at pc 0x558094fdb0f8 bp 0x7ffc32b4bae0 sp 0x7ffc32b4bad0
WRITE of size 4 at 0x7ffc32b4bd78 thread T0
0x558094fdb0f7 original::chain::chainInit chain.h:497:

this->size_ = 0;

0x558094fdb0f7 original::chain::chain chain.h:574:

chainInit();

0x558094fdb0f7 original::chain::chain chain.h:578:

original::chain<TYPE, ALLOC>::chain(const chain& other) : chain(){

0x558094fdb0f7 original::ChainTest_EqualityOperator_Test::TestBody test_chain.cpp:190:

Add lines for debug:

original/src/core/chain.h

Lines 579 to 584 in 6d0cb84

template <typename TYPE, typename ALLOC>
original::chain<TYPE, ALLOC>::chain(const chain& other) : chain(){
std::cout << "[DEBUG] " << "allocator_ (this): " << &this->allocator << std::endl;
std::cout << "[DEBUG] " << "allocator_ (other): " << &other.allocator << std::endl;
this->operator=(other);
}

original/src/core/chain.h

Lines 620 to 651 in 6d0cb84

template <typename TYPE, typename ALLOC>
original::chain<TYPE, ALLOC>& original::chain<TYPE, ALLOC>::operator=(const chain& other){
std::cout << "[DEBUG] copy ctor this=" << this
<< " allocator=" << &this->allocator
<< " from other=" << &other
<< " allocator=" << &other.allocator << std::endl;
if (this == &other) return *this;
this->chainDestroy();
this->size_ = other.size_;
if (this->size() != 0){
auto other_ = other.begin_->getPPrev();
auto pivot = this->createNode(other_->getVal());
other_ = other_->getPNext();
chainNode::connect(pivot, this->createNode(other_->getVal()));
this->begin_ = pivot->getPNext();
auto this_ = this->begin_;
while (other_ != other.end_){
other_ = other_->getPNext();
chainNode::connect(this_, this->createNode(other_->getVal()));
this_ = this_->getPNext();
}
this->end_ = this_;
} else{
this->chainInit();
}
if constexpr (ALLOC::propagate_on_container_copy_assignment::value){
this->allocator = other.allocator;
this->rebind_alloc = other.rebind_alloc;
}
std::cout << "[DEBUG] chain copy ctor end this=" << this << std::endl;
return *this;
}

allocator() {
std::cout << "[DEBUG] constructor of allocator called, type info: " << typeid(TYPE).name() << ", this: " << this << std::endl;
}

Outputs:

/home/frozenlemontee/Projects/original/cmake-build-debug/test/unit_test/test_core/core_tests --gtest_filter=ChainTest.EqualityOperator:ChainTest/*.EqualityOperator:ChainTest.EqualityOperator/*:*/ChainTest.EqualityOperator/*:*/ChainTest/*.EqualityOperator --gtest_color=no
Testing started at 09:11 ...
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe492687b8
[DEBUG] allocator_ (other): 0x7ffe49268728
[DEBUG] copy ctor this=0x7ffe492687b0 allocator=0x7ffe492687b8 from other=0x7ffe49268720 allocator=0x7ffe49268728
[DEBUG] chain copy ctor end this=0x7ffe492687b0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268810
[DEBUG] allocator_ (other): 0x7ffe49268780
[DEBUG] copy ctor this=0x7ffe49268808 allocator=0x7ffe49268810 from other=0x7ffe49268778 allocator=0x7ffe49268780
[DEBUG] chain copy ctor end this=0x7ffe49268808
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268868
[DEBUG] allocator_ (other): 0x7ffe492687d8
[DEBUG] copy ctor this=0x7ffe49268860 allocator=0x7ffe49268868 from other=0x7ffe492687d0 allocator=0x7ffe492687d8
[DEBUG] chain copy ctor end this=0x7ffe49268860
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe492688c0
[DEBUG] allocator_ (other): 0x7ffe49268830
[DEBUG] copy ctor this=0x7ffe492688b8 allocator=0x7ffe492688c0 from other=0x7ffe49268828 allocator=0x7ffe49268830
[DEBUG] chain copy ctor end this=0x7ffe492688b8
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268918
[DEBUG] allocator_ (other): 0x7ffe49268888
[DEBUG] copy ctor this=0x7ffe49268910 allocator=0x7ffe49268918 from other=0x7ffe49268880 allocator=0x7ffe49268888
[DEBUG] chain copy ctor end this=0x7ffe49268910
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268970
[DEBUG] allocator_ (other): 0x7ffe492688e0
[DEBUG] copy ctor this=0x7ffe49268968 allocator=0x7ffe49268970 from other=0x7ffe492688d8 allocator=0x7ffe492688e0
[DEBUG] chain copy ctor end this=0x7ffe49268968
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe492689c8
[DEBUG] allocator_ (other): 0x7ffe49268938
[DEBUG] copy ctor this=0x7ffe492689c0 allocator=0x7ffe492689c8 from other=0x7ffe49268930 allocator=0x7ffe49268938
[DEBUG] chain copy ctor end this=0x7ffe492689c0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268a20
[DEBUG] allocator_ (other): 0x7ffe49268990
[DEBUG] copy ctor this=0x7ffe49268a18 allocator=0x7ffe49268a20 from other=0x7ffe49268988 allocator=0x7ffe49268990
[DEBUG] chain copy ctor end this=0x7ffe49268a18
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268a78
[DEBUG] allocator_ (other): 0x7ffe492689e8
[DEBUG] copy ctor this=0x7ffe49268a70 allocator=0x7ffe49268a78 from other=0x7ffe492689e0 allocator=0x7ffe492689e8
[DEBUG] chain copy ctor end this=0x7ffe49268a70
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268ad0
[DEBUG] allocator_ (other): 0x7ffe49268a40
[DEBUG] copy ctor this=0x7ffe49268ac8 allocator=0x7ffe49268ad0 from other=0x7ffe49268a38 allocator=0x7ffe49268a40
[DEBUG] chain copy ctor end this=0x7ffe49268ac8
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268b28
[DEBUG] allocator_ (other): 0x7ffe49268a98
[DEBUG] copy ctor this=0x7ffe49268b20 allocator=0x7ffe49268b28 from other=0x7ffe49268a90 allocator=0x7ffe49268a98
[DEBUG] chain copy ctor end this=0x7ffe49268b20
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268b80
[DEBUG] allocator_ (other): 0x7ffe49268af0
[DEBUG] copy ctor this=0x7ffe49268b78 allocator=0x7ffe49268b80 from other=0x7ffe49268ae8 allocator=0x7ffe49268af0
[DEBUG] chain copy ctor end this=0x7ffe49268b78
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268bd8
[DEBUG] allocator_ (other): 0x7ffe49268b48
[DEBUG] copy ctor this=0x7ffe49268bd0 allocator=0x7ffe49268bd8 from other=0x7ffe49268b40 allocator=0x7ffe49268b48
[DEBUG] chain copy ctor end this=0x7ffe49268bd0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268c30
[DEBUG] allocator_ (other): 0x7ffe49268ba0
[DEBUG] copy ctor this=0x7ffe49268c28 allocator=0x7ffe49268c30 from other=0x7ffe49268b98 allocator=0x7ffe49268ba0
[DEBUG] chain copy ctor end this=0x7ffe49268c28
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268c88
[DEBUG] allocator_ (other): 0x7ffe49268bf8
[DEBUG] copy ctor this=0x7ffe49268c80 allocator=0x7ffe49268c88 from other=0x7ffe49268bf0 allocator=0x7ffe49268bf8
[DEBUG] chain copy ctor end this=0x7ffe49268c80
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268ce0
[DEBUG] allocator_ (other): 0x7ffe49268c50
[DEBUG] copy ctor this=0x7ffe49268cd8 allocator=0x7ffe49268ce0 from other=0x7ffe49268c48 allocator=0x7ffe49268c50
[DEBUG] chain copy ctor end this=0x7ffe49268cd8
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0
[DEBUG] allocator_ (this): 0x7ffe49268d38
[DEBUG] allocator_ (other): 0x7ffe49268ca8
[DEBUG] copy ctor this=0x7ffe49268d30 allocator=0x7ffe49268d38 from other=0x7ffe49268ca0 allocator=0x7ffe49268ca8
[DEBUG] chain copy ctor end this=0x7ffe49268d30
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684a0
[DEBUG] constructor of allocator called, type info: i, this: 0x7ffe49268580
[DEBUG] constructor of allocator called, type info: N8original5chainIiNS_9allocatorIiEEE9chainNodeE, this: 0x7ffe492684d0

Check Asan:

chain.h
Stack-overflow on address 0x7fff2f921000 (pc 0x55fbc7ada260 bp 0x7fff2f91d790 sp 0x7fff2f91d760 T0)
0x55fbc7ada260 original::chain::chain chain.h:574 
0x55fbc7ada9e5 original::chain::chain chain.h:580 
0x55fbc7c421b1 original::ChainTest_EqualityOperator_Test::TestBody test_chain.cpp:190 
0x55fbc7c41faf (core_tests+0x67afaf) (BuildId: f88e393d31a64fc2b7f50f89801e75d18767d230) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions