3 ms·
> > malloc(sizeof(Entry) + strlen(key)); > Don't forget +1 for the terminator. Also should check for arithmetic overflow, or at least static_assert that sizeo
by wahern 3y ago
> > malloc(sizeof(Entry) + strlen(key));
> Don't forget +1 for the terminator.
Also should check for arithmetic overflow, or at least static_assert that sizeof(Entry) is "too small" to matter. (Hopefully the process of deciding what "too small" should be results in just adding the overflow check.)
- dahfizz 3y agoNot really. We defined Entry, so we know it's only a few bytes. And we know size_t is large enough to iterate over all of main memory. So the only way that statement overflows is if the key is literally larger than main memory. Not a concern.
- asveikau 3y agoI agree with your reasoning, but it's not "literally", it's main memory minus (sizeof(struct Entry) - 1). But it's impossible to find a C string that big. Any point in memory is pretty clearly going to hit a 0 or a page fault before then.