mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-14 10:20:01 +00:00
Fix memset in mg_iotest, add mg_random_str test
This commit is contained in:
1 parent
e30ad43d36
commit
6c796b8552
4 files changed
+19
-6
No files matched your search
+3
-3
@@ -4368,7 +4368,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
nfds_t n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
|
||||
struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0]));
|
||||
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
|
||||
memset(fds, 0, n * sizeof(fds[0]));
|
||||
n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
c->is_readable = c->is_writable = 0;
|
||||
@@ -4390,7 +4390,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
#if MG_ARCH == MG_ARCH_WIN32
|
||||
if (n == 0) Sleep(ms); // On Windows, poll fails if no sockets
|
||||
#endif
|
||||
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
|
||||
memset(fds, 0, n * sizeof(fds[0]));
|
||||
}
|
||||
n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
@@ -5409,7 +5409,7 @@ char *mg_random_str(char *buf, size_t len) {
|
||||
size_t i;
|
||||
mg_random(buf, len);
|
||||
for (i = 0; i < len; i++) {
|
||||
uint8_t c = ((uint8_t *) buf)[i] % 62;
|
||||
uint8_t c = ((uint8_t *) buf)[i] % 62U;
|
||||
buf[i] = i == len - 1 ? (char) '\0' // 0-terminate last byte
|
||||
: c < 26 ? (char) ('a' + c) // lowercase
|
||||
: c < 52 ? (char) ('A' + c - 26) // uppercase
|
||||
|
||||
+2
-2
@@ -522,7 +522,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
nfds_t n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) n++;
|
||||
struct pollfd *fds = (struct pollfd *) alloca(n * sizeof(fds[0]));
|
||||
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
|
||||
memset(fds, 0, n * sizeof(fds[0]));
|
||||
n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
c->is_readable = c->is_writable = 0;
|
||||
@@ -544,7 +544,7 @@ static void mg_iotest(struct mg_mgr *mgr, int ms) {
|
||||
#if MG_ARCH == MG_ARCH_WIN32
|
||||
if (n == 0) Sleep(ms); // On Windows, poll fails if no sockets
|
||||
#endif
|
||||
if (n > 0) memset(fds, 0, sizeof(n * sizeof(fds[0])));
|
||||
memset(fds, 0, n * sizeof(fds[0]));
|
||||
}
|
||||
n = 0;
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ char *mg_random_str(char *buf, size_t len) {
|
||||
size_t i;
|
||||
mg_random(buf, len);
|
||||
for (i = 0; i < len; i++) {
|
||||
uint8_t c = ((uint8_t *) buf)[i] % 62;
|
||||
uint8_t c = ((uint8_t *) buf)[i] % 62U;
|
||||
buf[i] = i == len - 1 ? (char) '\0' // 0-terminate last byte
|
||||
: c < 26 ? (char) ('a' + c) // lowercase
|
||||
: c < 52 ? (char) ('A' + c - 26) // uppercase
|
||||
|
||||
@@ -1758,6 +1758,19 @@ static void test_util(void) {
|
||||
ASSERT(mg_tou64(mg_str("")) == 0);
|
||||
ASSERT(mg_tou64(mg_str("-")) == 0);
|
||||
ASSERT(mg_tou64(mg_str("18446744073709551615")) == 18446744073709551615U);
|
||||
|
||||
{
|
||||
size_t i;
|
||||
memset(buf, ' ', sizeof(buf));
|
||||
mg_random_str(buf, 0);
|
||||
ASSERT(buf[0] == ' ');
|
||||
mg_random_str(buf, 1);
|
||||
ASSERT(buf[0] == '\0');
|
||||
ASSERT(buf[1] == ' ');
|
||||
mg_random_str(buf, sizeof(buf));
|
||||
ASSERT(buf[sizeof(buf) - 1] == '\0');
|
||||
for (i = 0; i < sizeof(buf) - 1; i++) ASSERT(isalnum((uint8_t) buf[i]));
|
||||
}
|
||||
}
|
||||
|
||||
static void test_crc32(void) {
|
||||
|
||||
Reference in new issue
Block a user