Skip to content

Commit

Permalink
Add mongoose pedantic fixes for style checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Nov 1, 2019
1 parent 35fad35 commit 9e04f93
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
6 changes: 2 additions & 4 deletions include/mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,8 @@ unsigned int sleep(unsigned int seconds);
/* https://stackoverflow.com/questions/16647819/timegm-cross-platform */
#define timegm _mkgmtime

#define gmtime_r(a, b) \
do { \
*(b) = *gmtime(a); \
} while (0)
#define gmtime_r(a, b) (gmtime_s((b), (a)), (b))
#define localtime_r(a, b) (localtime_s((b), (a)), (b))

#endif /* CS_PLATFORM == CS_P_WINDOWS */
#endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */
Expand Down
14 changes: 12 additions & 2 deletions src/mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -4330,19 +4330,27 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
sa2 = sa;

if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
// abort
} else if (bind(sock, &sa.sa, len) != 0) {
// abort
} else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
// abort
} else if (getsockname(sock, &sa.sa, &len) != 0) {
// abort
} else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
// abort
} else if (sock_type == SOCK_STREAM && connect(sp[0], &sa.sa, len) != 0) {
// abort
} else if (sock_type == SOCK_DGRAM &&
(bind(sp[0], &sa2.sa, len) != 0 ||
getsockname(sp[0], &sa2.sa, &len) != 0 ||
connect(sp[0], &sa.sa, len) != 0 ||
connect(sock, &sa2.sa, len) != 0)) {
// abort
} else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
sock, &sa, len))) ==
INVALID_SOCKET) {
// abort
} else {
mg_set_close_on_exec(sp[0]);
mg_set_close_on_exec(sp[1]);
Expand Down Expand Up @@ -7271,7 +7279,8 @@ static void mg_http_construct_etag(char *buf, size_t buf_len,

#ifndef WINCE
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t) {
strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(t));
struct tm tm;
strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(t, &tm));
}
#else
/* Look wince_lib.c for WindowsCE implementation */
Expand Down Expand Up @@ -7886,6 +7895,7 @@ static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name,
int is_dir = S_ISDIR(stp->st_mode);
const char *slash = is_dir ? "/" : "";
struct mg_str href;
struct tm tm;

if (is_dir) {
snprintf(size, sizeof(size), "%s", "[DIRECTORY]");
Expand All @@ -7904,7 +7914,7 @@ static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name,
snprintf(size, sizeof(size), "%.1fG", (double) fsize / 1073741824);
}
}
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&stp->st_mtime, &tm));
mg_escape(file_name, path, sizeof(path));
href = mg_url_encode(mg_mk_str(file_name));
mg_printf_http_chunk(nc,
Expand Down
76 changes: 76 additions & 0 deletions src/mongoose_pedantic.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
diff --git a/include/mongoose.h b/include/mongoose.h
index 316f5f9..03cc04e 100644
--- a/include/mongoose.h
+++ b/include/mongoose.h
@@ -352,10 +352,8 @@ unsigned int sleep(unsigned int seconds);
/* https://stackoverflow.com/questions/16647819/timegm-cross-platform */
#define timegm _mkgmtime

-#define gmtime_r(a, b) \
- do { \
- *(b) = *gmtime(a); \
- } while (0)
+#define gmtime_r(a, b) (gmtime_s((b), (a)), (b))
+#define localtime_r(a, b) (localtime_s((b), (a)), (b))

#endif /* CS_PLATFORM == CS_P_WINDOWS */
#endif /* CS_COMMON_PLATFORMS_PLATFORM_WINDOWS_H_ */
diff --git a/src/mongoose.c b/src/mongoose.c
index fe0df63..160df87 100644
--- a/src/mongoose.c
+++ b/src/mongoose.c
@@ -4330,19 +4330,27 @@ int mg_socketpair(sock_t sp[2], int sock_type) {
sa2 = sa;

if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
+ // abort
} else if (bind(sock, &sa.sa, len) != 0) {
+ // abort
} else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) {
+ // abort
} else if (getsockname(sock, &sa.sa, &len) != 0) {
+ // abort
} else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) {
+ // abort
} else if (sock_type == SOCK_STREAM && connect(sp[0], &sa.sa, len) != 0) {
+ // abort
} else if (sock_type == SOCK_DGRAM &&
(bind(sp[0], &sa2.sa, len) != 0 ||
getsockname(sp[0], &sa2.sa, &len) != 0 ||
connect(sp[0], &sa.sa, len) != 0 ||
connect(sock, &sa2.sa, len) != 0)) {
+ // abort
} else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept(
sock, &sa, len))) ==
INVALID_SOCKET) {
+ // abort
} else {
mg_set_close_on_exec(sp[0]);
mg_set_close_on_exec(sp[1]);
@@ -7271,7 +7279,8 @@ static void mg_http_construct_etag(char *buf, size_t buf_len,

#ifndef WINCE
static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t) {
- strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(t));
+ struct tm tm;
+ strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(t, &tm));
}
#else
/* Look wince_lib.c for WindowsCE implementation */
@@ -7886,6 +7895,7 @@ static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name,
int is_dir = S_ISDIR(stp->st_mode);
const char *slash = is_dir ? "/" : "";
struct mg_str href;
+ struct tm tm;

if (is_dir) {
snprintf(size, sizeof(size), "%s", "[DIRECTORY]");
@@ -7904,7 +7914,7 @@ static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name,
snprintf(size, sizeof(size), "%.1fG", (double) fsize / 1073741824);
}
}
- strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime));
+ strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime_r(&stp->st_mtime, &tm));
mg_escape(file_name, path, sizeof(path));
href = mg_url_encode(mg_mk_str(file_name));
mg_printf_http_chunk(nc,

0 comments on commit 9e04f93

Please sign in to comment.