- Code: Select all
class Book {
public:
void setTime(const char * tt1) { memcpy(_tt1, tt1, 32); }
private:
char _tt1[32];
}
// and a bunch of stuff...
Book book;
RWCstring time = order.getTime(); // order.getTime returns a const char* of the time of this order.
book.setTime(time);
When used in a char* context, an RWCString returns the internal char* that it uses to store the string, similar to a std::string (I have no idea why this was written with RogueWave, I just go with the flow)
It crashes in the memcpy. The size of the RWCString is 12 bytes.
When I look up the data in gdb, the first 27 bytes of time and _tt1 are the same. The 28th byte is different. The 28th byte of time is at memory location 0x44500000. I don't believe that to be a coincidence.
So I'm pretty sure it faulted because it tried to look up memory that didn't exist in the TLB, or something. Tried to look up a different page? See, this is my problem, I understand physically why it crashed, but can't think of the right words to explain it.
And the fix is obvious: either ensure the RWCString has a capacity of 32 (which can be done in its constructor) or change memcpy to a strncpy.
