瀏覽代碼

handle compile warnings

ithewei 5 年之前
父節點
當前提交
4e6fdf477a
共有 7 個文件被更改,包括 41 次插入44 次删除
  1. 1 0
      .gitignore
  2. 4 3
      base/hbase.c
  3. 4 11
      http/http_content.cpp
  4. 6 1
      http/server/FileCache.cpp
  5. 5 7
      http/server/HttpServer.h
  6. 4 8
      protocol/dns.c
  7. 17 14
      utils/hmain.cpp

+ 1 - 0
.gitignore

@@ -52,6 +52,7 @@ tmp
 dist
 test
 build
+html/uploads
 
 # msvc
 *.VC.*

+ 4 - 3
base/hbase.c

@@ -258,7 +258,9 @@ char* get_executable_path(char* buf, int size) {
 #ifdef OS_WIN
     GetModuleFileName(NULL, buf, size);
 #elif defined(OS_LINUX)
-    readlink("/proc/self/exe", buf, size);
+    if (readlink("/proc/self/exe", buf, size) == -1) {
+        return NULL;
+    }
 #elif defined(OS_DARWIN)
     _NSGetExecutablePath(buf, (uint32_t*)&size);
 #endif
@@ -287,6 +289,5 @@ char* get_executable_file(char* buf, int size) {
 }
 
 char* get_run_dir(char* buf, int size) {
-    getcwd(buf, size);
-    return buf;
+    return getcwd(buf, size);
 }

+ 4 - 11
http/http_content.cpp

@@ -61,10 +61,8 @@ int parse_query_params(const char* query_string, QueryParams& query_params) {
 
 #ifndef WITHOUT_HTTP_CONTENT
 
-#include <sys/types.h>
-#include <sys/stat.h>
-
 #include "hstring.h" // for split
+#include "hfile.h"
 #include "httpdef.h" // for http_content_type_str_by_suffix
 
 std::string dump_multipart(MultiPart& mp, const char* boundary) {
@@ -80,14 +78,9 @@ std::string dump_multipart(MultiPart& mp, const char* boundary) {
         auto& form = pair.second;
         if (form.filename.size() != 0) {
             if (form.content.size() == 0) {
-                FILE* fp = fopen(form.filename.c_str(), "r");
-                if (fp) {
-                    struct stat st;
-                    if (stat(form.filename.c_str(), &st) == 0 && st.st_size != 0) {
-                        form.content.resize(st.st_size);
-                        fread((void*)form.content.data(), 1, st.st_size, fp);
-                    }
-                    fclose(fp);
+                HFile file;
+                if (file.open(form.filename.c_str(), "r") == 0) {
+                    file.readall(form.content);
                 }
             }
             snprintf(c_str, sizeof(c_str), "; filename=\"%s\"", basename(form.filename).c_str());

+ 6 - 1
http/server/FileCache.cpp

@@ -2,6 +2,7 @@
 
 #include "hscope.h"
 #include "htime.h"
+#include "hlog.h"
 
 #include "httpdef.h" // for http_content_type_str_by_suffix
 #include "http_page.h" //make_index_of_page
@@ -53,7 +54,11 @@ file_cache_t* FileCache::Open(const char* filepath, void* ctx) {
         if (S_ISREG(fc->st.st_mode)) {
             // FILE
             fc->resize_buf(fc->st.st_size);
-            read(fd, fc->filebuf.base, fc->filebuf.len);
+            int nread = read(fd, fc->filebuf.base, fc->filebuf.len);
+            if (nread != fc->filebuf.len) {
+                hloge("Too large file: %s", filepath);
+                return NULL;
+            }
             const char* suffix = strrchr(filepath, '.');
             if (suffix) {
                 fc->content_type = http_content_type_str_by_suffix(++suffix);

+ 5 - 7
http/server/HttpServer.h

@@ -36,15 +36,13 @@ typedef struct http_server_s {
 /*
 #include "HttpServer.h"
 
-int http_api_hello(HttpRequest* req, HttpResponse* res) {
-    res->body = "hello";
-    return 0;
-}
-
 int main() {
     HttpService service;
     service.base_url = "/v1/api";
-    service.AddApi("/hello", HTTP_GET, http_api_hello);
+    service.GET("/ping", [](HttpRequest* req, HttpResponse* resp) {
+        resp->body = "pong";
+        return 200;
+    });
 
     http_server_t server;
     server.port = 8080;
@@ -53,7 +51,7 @@ int main() {
     http_server_run(&server);
     return 0;
 }
- */
+*/
 HV_EXPORT int http_server_run(http_server_t* server, int wait = 1);
 
 // just for worker_processes = 0

+ 4 - 8
protocol/dns.c

@@ -190,8 +190,7 @@ int dns_unpack(char* buf, int len, dns_t* dns) {
     int i;
     if (hdr->nquestion) {
         int bytes = hdr->nquestion * sizeof(dns_rr_t);
-        dns->questions = (dns_rr_t*)malloc(bytes);
-        memset(dns->questions, 0, bytes);
+        SAFE_ALLOC(dns->questions, bytes);
         for (i = 0; i < hdr->nquestion; ++i) {
             int packetlen = dns_rr_unpack(buf+off, len-off, dns->questions+i, 1);
             if (packetlen < 0) return -1;
@@ -200,8 +199,7 @@ int dns_unpack(char* buf, int len, dns_t* dns) {
     }
     if (hdr->nanswer) {
         int bytes = hdr->nanswer * sizeof(dns_rr_t);
-        dns->answers = (dns_rr_t*)malloc(bytes);
-        memset(dns->answers, 0, bytes);
+        SAFE_ALLOC(dns->answers, bytes);
         for (i = 0; i < hdr->nanswer; ++i) {
             int packetlen = dns_rr_unpack(buf+off, len-off, dns->answers+i, 0);
             if (packetlen < 0) return -1;
@@ -210,8 +208,7 @@ int dns_unpack(char* buf, int len, dns_t* dns) {
     }
     if (hdr->nauthority) {
         int bytes = hdr->nauthority * sizeof(dns_rr_t);
-        dns->authorities = (dns_rr_t*)malloc(bytes);
-        memset(dns->authorities, 0, bytes);
+        SAFE_ALLOC(dns->authorities, bytes);
         for (i = 0; i < hdr->nauthority; ++i) {
             int packetlen = dns_rr_unpack(buf+off, len-off, dns->authorities+i, 0);
             if (packetlen < 0) return -1;
@@ -220,8 +217,7 @@ int dns_unpack(char* buf, int len, dns_t* dns) {
     }
     if (hdr->naddtional) {
         int bytes = hdr->naddtional * sizeof(dns_rr_t);
-        dns->addtionals = (dns_rr_t*)malloc(bytes);
-        memset(dns->addtionals, 0, bytes);
+        SAFE_ALLOC(dns->addtionals, bytes);
         for (i = 0; i < hdr->naddtional; ++i) {
             int packetlen = dns_rr_unpack(buf+off, len-off, dns->addtionals+i, 0);
             if (packetlen < 0) return -1;

+ 17 - 14
utils/hmain.cpp

@@ -16,9 +16,8 @@ main_ctx_t  g_main_ctx;
 int main_ctx_init(int argc, char** argv) {
     if (argc == 0 || argv == NULL) {
         argc = 1;
-        argv = (char**)malloc(2*sizeof(char*));
-        argv[0] = (char*)malloc(MAX_PATH);
-        argv[1] = NULL;
+        SAFE_ALLOC(argv, 2 * sizeof(char*));
+        SAFE_ALLOC(argv[0], MAX_PATH);
         get_executable_path(argv[0], MAX_PATH);
     }
 
@@ -64,10 +63,11 @@ int main_ctx_init(int argc, char** argv) {
         g_main_ctx.arg_len += strlen(argv[i]) + 1;
     }
     g_main_ctx.argc = i;
-    char* argp = (char*)malloc(g_main_ctx.arg_len);
-    memset(argp, 0, g_main_ctx.arg_len);
-    g_main_ctx.save_argv = (char**)malloc((g_main_ctx.argc+1) * sizeof(char*));
-    char* cmdline = (char*)malloc(g_main_ctx.arg_len);
+    char* argp = NULL;
+    SAFE_ALLOC(argp, g_main_ctx.arg_len);
+    SAFE_ALLOC(g_main_ctx.save_argv, (g_main_ctx.argc + 1) * sizeof(char*));
+    char* cmdline = NULL;
+    SAFE_ALLOC(cmdline, g_main_ctx.arg_len);
     g_main_ctx.cmdline = cmdline;
     for (i = 0; argv[i]; ++i) {
         g_main_ctx.save_argv[i] = argp;
@@ -91,9 +91,9 @@ int main_ctx_init(int argc, char** argv) {
         g_main_ctx.env_len += strlen(environ[i]) + 1;
     }
     g_main_ctx.envc = i;
-    char* envp = (char*)malloc(g_main_ctx.env_len);
-    memset(envp, 0, g_main_ctx.env_len);
-    g_main_ctx.save_envp = (char**)malloc((g_main_ctx.envc+1) * sizeof(char*));
+    char* envp = NULL;
+    SAFE_ALLOC(envp, g_main_ctx.env_len);
+    SAFE_ALLOC(g_main_ctx.save_envp, (g_main_ctx.envc + 1) * sizeof(char*));
     for (i = 0; environ[i]; ++i) {
         g_main_ctx.save_envp[i] = envp;
         strcpy(g_main_ctx.save_envp[i], environ[i]);
@@ -307,8 +307,12 @@ int create_pidfile() {
 
     g_main_ctx.pid = hv_getpid();
     char pid[16] = {0};
-    snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
-    fwrite(pid, 1, strlen(pid), fp);
+    int len = snprintf(pid, sizeof(pid), "%d\n", g_main_ctx.pid);
+    int nwrite = fwrite(pid, 1, len, fp);
+    if (nwrite != len) {
+        fprintf(stderr, "fwrite failed!\n");
+        exit(-1);
+    }
     fclose(fp);
     hlogi("create_pidfile('%s') pid=%d", g_main_ctx.pidfile, g_main_ctx.pid);
     atexit(delete_pidfile);
@@ -598,8 +602,7 @@ int master_workers_run(procedure_t worker_fn, void* worker_userdata,
 #endif
         g_main_ctx.worker_processes = worker_processes;
         int bytes = g_main_ctx.worker_processes * sizeof(proc_ctx_t);
-        g_main_ctx.proc_ctxs = (proc_ctx_t*)malloc(bytes);
-        memset(g_main_ctx.proc_ctxs, 0, bytes);
+        SAFE_ALLOC(g_main_ctx.proc_ctxs, bytes);
         proc_ctx_t* ctx = g_main_ctx.proc_ctxs;
         for (int i = 0; i < g_main_ctx.worker_processes; ++i, ++ctx) {
             ctx->init = worker_init;