提交 69d39053 编写于 作者: A arhag

ensure primary key does not change on modify

上级 28771835
......@@ -620,15 +620,17 @@ class multi_index
return index_type::extract_secondary_key( obj );
});
auto pk = obj.primary_key();
auto& mutableobj = const_cast<T&>(obj); // Do not forget the auto& otherwise it would make a copy and thus not update at all.
updater( mutableobj );
eosio_assert( pk == obj.primary_key(), "updater cannot change primary key when modifying an object" );
char tmp[ pack_size( obj ) ];
datastream<char*> ds( tmp, sizeof(tmp) );
ds << obj;
auto pk = obj.primary_key();
db_update_i64( objitem.__primary_itr, payer, tmp, sizeof(tmp) );
if( pk >= _next_primary_key )
......
......@@ -214,9 +214,18 @@ void test_multi_index::idx128_autoincrement_test()
auto itr = table.find(3);
eosio_assert( itr != table.end(), "idx128_autoincrement_test - could not find object with primary key of 3" );
// The modification below would trigger an error:
/*
table.modify(itr, payer, [&]( auto& r ) {
r.id = 100;
});
*/
table.emplace( payer, [&]( auto& r) {
r.id = 100;
r.sec = itr->sec;
});
table.erase(itr);
eosio_assert( table.available_primary_key() == 101, "idx128_autoincrement_test - next_primary_key was not correct after record modify" );
}
......@@ -299,9 +308,11 @@ void test_multi_index::idx128_autoincrement_test_part2()
auto itr = table.find(3);
eosio_assert( itr != table.end(), "idx128_autoincrement_test_part2 - could not find object with primary key of 3" );
table.modify(itr, payer, [&]( auto& r ) {
r.id = 100;
table.emplace( payer, [&]( auto& r) {
r.id = 100;
r.sec = itr->sec;
});
table.erase(itr);
eosio_assert( table.available_primary_key() == 101, "idx128_autoincrement_test_part2 - next_primary_key was not correct after record update" );
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册