Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialize and deserialize in part3 #42

Closed
stardustman opened this issue Jul 18, 2019 · 2 comments
Closed

serialize and deserialize in part3 #42

stardustman opened this issue Jul 18, 2019 · 2 comments

Comments

@stardustman
Copy link
Contributor

void serialize_row(Row* source, void* destination) {
  memcpy(destination + ID_OFFSET, &(source->id), ID_SIZE);
  memcpy(destination + USERNAME_OFFSET, &(source->username), USERNAME_SIZE);
  memcpy(destination + EMAIL_OFFSET, &(source->email), EMAIL_SIZE);
}

these code confused me.
memcpy(destination + USERNAME_OFFSET, &(source->username), USERNAME_SIZE);
when the input is "insert 1 csstack foo@bar.com " the length of Row.username is 7, but the args for memcpy copy size is USERNAME_SIZE, which is 32. Is there is some problem? The size of the given username(csstack) is smaller than the USER_SIZE. This may be copy some bytes that doesn't need. Same problem for the EMAIL_SIZE. I was blocked by these code. Could you help me? Thanks a lot.

@cstack
Copy link
Owner

cstack commented Jul 18, 2019

You're right. This code copies the entire buffer rather than the length of the string. I wasn't focusing on performance to start, and I thought this was simpler.

@cstack cstack closed this as completed Jul 18, 2019
@stardustman
Copy link
Contributor Author

void deserialize_row(void* source, Row* destination) {
memcpy(&(destination->id), source + ID_OFFSET, ID_SIZE);
memcpy(&(destination->username), source + USERNAME_OFFSET, USERNAME_SIZE);
memcpy(&(destination->email), source + EMAIL_OFFSET, EMAIL_SIZE);
}

We know that the clang stirng was terminated by '\0'. Copy the entire buffer in the deserialize_row method. char username[USERNAME_SIZE] in Row is csstack, username[7] was not set '\0' explicitly. If the value of username[7] is not '\0', when execute 'select', this may be wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants