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

memcmp wrongly used #14

Closed
vteromero opened this issue Mar 3, 2020 · 0 comments · Fixed by #17
Closed

memcmp wrongly used #14

vteromero opened this issue Mar 3, 2020 · 0 comments · Fixed by #17
Labels
bug Something isn't working

Comments

@vteromero
Copy link
Owner

memcmp compare stream of bytes, not arrays of other bigger types like uint16_t, uint32_t or uint64_t. You can still use it with those types, but you need to properly calculate the number of bytes to compare For example:

const size_t len = 4;
uint32_t a[len] = {0, 1, 2, 3};
uint32_t b[len] = {0, 10, 20, 30};

// WRONG: will output "Arrays are equal"
if (memcmp(a, b, len) == 0) {
  printf("Arrays are equal\n");
} else {
  printf("Arrays are different\n");
}

// CORRECT: will output "Arrays are different"
if (memcmp(a, b, len * sizeof(uint32_t)) == 0) {
  printf("Arrays are equal\n");
} else {
  printf("Arrays are different\n");
}

There are a couple of places where memcmp is being used incorrectly:

  • tests/gov2.c
  • tests/timestamps.c
@vteromero vteromero added the bug Something isn't working label Mar 3, 2020
@vteromero vteromero mentioned this issue Mar 18, 2020
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant