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

Made srand(...) and rand() platform independent #711

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/liblwm2m.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ lwm2m_context_t * lwm2m_init(void * userData)
{
memset(contextP, 0, sizeof(lwm2m_context_t));
contextP->userData = userData;
srand((int)lwm2m_gettime());
srand(lwm2m_seed());
contextP->nextMID = rand();
}

Expand Down
10 changes: 10 additions & 0 deletions examples/shared/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ time_t lwm2m_gettime(void)
return time(NULL);
}

int lwm2m_seed(void) {
/*
* Return a seed for random number generation, the seed must be a
* different number at every boot and unpredictable, time(NULL) may not be
* a reliable source as a seed.
* See: https://github.com/eclipse/wakaama/pull/711
*/
return time(NULL);
}

void lwm2m_printf(const char * format, ...)
{
va_list ap;
Expand Down
2 changes: 2 additions & 0 deletions include/liblwm2m.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ int lwm2m_strcasecmp(const char * str1, const char * str2);
// In case of error, this must return a negative value.
// Per POSIX specifications, time_t is a signed integer.
time_t lwm2m_gettime(void);
// Get a seed (which must not repeat when the device reboots) for generating a random number
int lwm2m_seed(void);

#ifdef LWM2M_WITH_LOGS
// Same usage as C89 printf()
Expand Down
Loading