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

[FIXED] nuid: fix wrong sizeof in memset #505

Merged
merged 1 commit into from
Jan 16, 2022

Conversation

paoloteti
Copy link
Contributor

Fixes following Coverity issue:

Passing argument &globalNUID of type natsLockedNUID * and
argument 32UL (sizeof (natsNUID)) to function memset is suspicious
because sizeof (natsLockedNUID) /40/ is expected.

Signed-off-by: Paolo Teti paolo.teti@gmail.com

src/nuid.c Outdated
@@ -208,7 +208,7 @@ natsNUID_init(void)
natsStatus s;
unsigned int seed = (unsigned int) nats_NowInNanoSeconds();

memset(&globalNUID, 0, sizeof(natsNUID));
memset(&(globalNUID.nuid), 0, sizeof(globalNUID.nuid));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch in that the memset was wrong, but it was the sizeof() that was wrong, not the address of the object. Here, I wanted to initialize globalNUID, which is a natsLockedNUID object, which itself, is a pointer to a mutex and the natsNUID structure. So the proper initialization is:

memset(&globalNUID, 0, sizeof(natsLockedNUID));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ll update, but mutex is initialized one line after, so I used memset() in this way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that was the point of the memset: if the initialization of the mutex fails, calling natsNUID_free() in case of error would then be dangerous because globalNUID.mu pointer may not have been initialized to NULL.

Fixes following Coverity issue:

Passing argument &globalNUID of type natsLockedNUID * and
argument 32UL (sizeof (natsNUID)) to function memset is suspicious
because sizeof (natsLockedNUID) /*40*/ is expected.

Signed-off-by: Paolo Teti <paolo.teti@gmail.com>
Copy link
Member

@kozlovic kozlovic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks again for your continuous contributions!

@kozlovic kozlovic merged commit aa8c797 into nats-io:main Jan 16, 2022
@paoloteti paoloteti deleted the fix-memset branch January 16, 2022 19:45
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

Successfully merging this pull request may close these issues.

None yet

2 participants