diff --git a/mongoose.c b/mongoose.c index 84978f61..9411a8a9 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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"); @@ -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 diff --git a/src/util.c b/src/util.c index 24c83b8e..97ab47c8 100644 --- a/src/util.c +++ b/src/util.c @@ -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"); @@ -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