Skip to content

Commit

Permalink
Fix mg_random() for ESP32
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Oct 4, 2021
1 parent a204c80 commit 6d398bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
7 changes: 3 additions & 4 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -4215,6 +4215,7 @@ void mg_random(void *buf, size_t len) {
unsigned char *p = (unsigned char *) buf;
#if MG_ARCH == MG_ARCH_ESP32
while (len--) *p++ = (unsigned char) (esp_random() & 255);
done = true;
#elif MG_ARCH == MG_ARCH_WIN32
#elif MG_ARCH_UNIX
FILE *fp = fopen("/dev/urandom", "rb");
Expand All @@ -4223,10 +4224,8 @@ void mg_random(void *buf, size_t len) {
fclose(fp);
}
#endif
// Fallback to a pseudo random gen
if (!done) {
while (len--) *p++ = (unsigned char) (rand() & 255);
}
// If everything above did not work, fallback to a pseudo random generator
while (!done && len--) *p++ = (unsigned char) (rand() & 255);
}
#endif

Expand Down
7 changes: 3 additions & 4 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ void mg_random(void *buf, size_t len) {
unsigned char *p = (unsigned char *) buf;
#if MG_ARCH == MG_ARCH_ESP32
while (len--) *p++ = (unsigned char) (esp_random() & 255);
done = true;
#elif MG_ARCH == MG_ARCH_WIN32
#elif MG_ARCH_UNIX
FILE *fp = fopen("/dev/urandom", "rb");
Expand All @@ -72,10 +73,8 @@ void mg_random(void *buf, size_t len) {
fclose(fp);
}
#endif
// Fallback to a pseudo random gen
if (!done) {
while (len--) *p++ = (unsigned char) (rand() & 255);
}
// If everything above did not work, fallback to a pseudo random generator
while (!done && len--) *p++ = (unsigned char) (rand() & 255);
}
#endif

Expand Down

0 comments on commit 6d398bd

Please sign in to comment.