Skip to content

Commit 6dedd1e

Browse files
authored
Fix host.db load failure due to incorrect object_version compatibility check (#12395)
* Fix host.db load failure due to incorrect object_version compatibility check * Fix unintended initialization of member variables in HostDBInfo::unmarshall
1 parent 9f6abab commit 6dedd1e

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

iocore/hostdb/I_HostDBProcessor.h

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,28 @@ struct HostDBInfo : public RefCountObj {
175175
if (size < sizeof(HostDBInfo)) {
176176
return nullptr;
177177
}
178-
HostDBInfo *ret = HostDBInfo::alloc(size - sizeof(HostDBInfo));
179-
int buf_index = ret->_iobuffer_index;
178+
HostDBInfo *ret = HostDBInfo::alloc(size - sizeof(HostDBInfo));
179+
const auto buf_index = ret->_iobuffer_index;
180180
memcpy((void *)ret, buf, size);
181-
// Reset the refcount back to 0, this is a bit ugly-- but I'm not sure we want to expose a method
182-
// to mess with the refcount, since this is a fairly unique use case
183-
ret = new (ret) HostDBInfo();
184-
ret->_iobuffer_index = buf_index;
181+
182+
// Member variables with default member initializers will be overwritten by subsequent constructor calls,
183+
// so their values are temporarily saved and restored after the constructor is executed.
184+
const auto key = ret->key;
185+
const auto hostname_offset = ret->hostname_offset;
186+
const auto ip_timestamp = ret->ip_timestamp;
187+
const auto ip_timeout_interval = ret->ip_timeout_interval;
188+
189+
// The constructor is invoked for the following reasons:
190+
// - To reinitialize the virtual function table pointer (vptr),
191+
// which may have been corrupted by a previous memcpy
192+
// - To reset the reference count to zero
193+
ret = new (ret) HostDBInfo();
194+
ret->key = key;
195+
ret->hostname_offset = hostname_offset;
196+
ret->ip_timestamp = ip_timestamp;
197+
ret->ip_timeout_interval = ip_timeout_interval;
198+
ret->_iobuffer_index = buf_index;
199+
185200
return ret;
186201
}
187202

@@ -319,6 +334,8 @@ struct HostDBInfo : public RefCountObj {
319334
}
320335
}
321336

337+
// NOTE: Using default member initializers can affect the behavior of the unmarshall method.
338+
// Ensure that all member variables are properly set within the unmarshall method.
322339
uint64_t key{0};
323340

324341
// Application specific data. NOTE: We need an integral number of

iocore/hostdb/RefCountCache.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,5 @@ RefCountCacheHeader::operator==(RefCountCacheHeader const &that) const
4949
bool
5050
RefCountCacheHeader::compatible(RefCountCacheHeader *that) const
5151
{
52-
return this->magic == that->magic && this->version == that->version && this->object_version == that->version;
52+
return this->magic == that->magic && this->version == that->version && this->object_version == that->object_version;
5353
};

0 commit comments

Comments
 (0)