Browse Source

Changed to use snprintf.

yhirose 13 years ago
parent
commit
12ac86943e
1 changed files with 8 additions and 6 deletions
  1. 8 6
      httpsvrkit.h

+ 8 - 6
httpsvrkit.h

@@ -9,7 +9,7 @@
 #define HTTPSVRKIT_H
 #define HTTPSVRKIT_H
 
 
 #ifdef _WIN32
 #ifdef _WIN32
-//#define _CRT_SECURE_NO_WARNINGS
+#define _CRT_SECURE_NO_WARNINGS
 #define _CRT_NONSTDC_NO_DEPRECATE
 #define _CRT_NONSTDC_NO_DEPRECATE
 
 
 #ifndef SO_SYNCHRONOUS_NONALERT
 #ifndef SO_SYNCHRONOUS_NONALERT
@@ -24,6 +24,7 @@
 #include <winsock2.h>
 #include <winsock2.h>
 
 
 typedef SOCKET socket_t;
 typedef SOCKET socket_t;
+#define snprintf sprintf_s
 #else
 #else
 #include <pthread.h>
 #include <pthread.h>
 #include <unistd.h>
 #include <unistd.h>
@@ -178,21 +179,21 @@ std::string dump_request(Context& cxt)
 
 
     s += "================================\n";
     s += "================================\n";
 
 
-    sprintf(buf, "%s %s", req.method.c_str(), req.url.c_str());
+    snprintf(buf, sizeof(buf), "%s %s", req.method.c_str(), req.url.c_str());
     s += buf;
     s += buf;
 
 
     std::string query;
     std::string query;
     for (auto it = req.query.begin(); it != req.query.end(); ++it) {
     for (auto it = req.query.begin(); it != req.query.end(); ++it) {
        const auto& x = *it;
        const auto& x = *it;
-       sprintf(buf, "%c%s=%s", (it == req.query.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
+       snprintf(buf, sizeof(buf), "%c%s=%s", (it == req.query.begin()) ? '?' : '&', x.first.c_str(), x.second.c_str());
        query += buf;
        query += buf;
     }
     }
-    sprintf(buf, "%s\n", query.c_str());
+    snprintf(buf, sizeof(buf), "%s\n", query.c_str());
     s += buf;
     s += buf;
 
 
     for (auto it = req.headers.begin(); it != req.headers.end(); ++it) {
     for (auto it = req.headers.begin(); it != req.headers.end(); ++it) {
        const auto& x = *it;
        const auto& x = *it;
-       sprintf(buf, "%s: %s\n", x.first.c_str(), x.second.c_str());
+       snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
        s += buf;
        s += buf;
     }
     }
 
 
@@ -279,8 +280,9 @@ inline bool Server::run()
 
 
 inline void Server::stop()
 inline void Server::stop()
 {
 {
-    close_socket(sock_);
+    auto sock = sock_;
     sock_ = -1;
     sock_ = -1;
+    close_socket(sock);
 }
 }
 
 
 inline bool read_request_line(FILE* fp, Request& request)
 inline bool read_request_line(FILE* fp, Request& request)