diff --git a/mongoose.c b/mongoose.c index 3f4d0b0b..adee0e4c 100644 --- a/mongoose.c +++ b/mongoose.c @@ -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 diff --git a/src/sock.c b/src/sock.c index ba09dd38..21c859c3 100644 --- a/src/sock.c +++ b/src/sock.c @@ -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) { diff --git a/src/util.c b/src/util.c index 0d34b94c..4725edd6 100644 --- a/src/util.c +++ b/src/util.c @@ -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 diff --git a/test/unit_test.c b/test/unit_test.c index 49e261b0..d77dc579 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -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) {