tdutils: as<T> is safer now

GitOrigin-RevId: 8bdb4ce3e84c8a753d76502e830bcfcd3c0bd28b
This commit is contained in:
Arseny Smirnov 2018-12-19 23:43:19 +03:00
parent cba0f62ba9
commit bc991da146
2 changed files with 4 additions and 2 deletions

View File

@ -113,11 +113,11 @@ class As {
As(void *ptr) : ptr_(ptr) {
}
As(As &&) = default;
const As<T> &operator=(const As &new_value) const {
As<T> &operator=(const As &new_value) && {
memcpy(ptr_, new_value.ptr_, sizeof(T));
return *this;
}
const As<T> &operator=(const T new_value) const {
As<T> &operator=(const T new_value) && {
memcpy(ptr_, &new_value, sizeof(T));
return *this;
}

View File

@ -593,6 +593,8 @@ TEST(Misc, As) {
ASSERT_EQ(123, as<int>((const char *)buf));
ASSERT_EQ(123, as<int>((char *)buf));
char buf2[100];
//auto x = as<int>(buf2);
//x = 44342; //CE
as<int>(buf2) = as<int>(buf);
ASSERT_EQ(123, as<int>((const char *)buf2));
ASSERT_EQ(123, as<int>((char *)buf2));