Hi

Second part of the mallocless change.

- Lauri
>From ce7459926ea6e80902c9a12fb8464cad33d9e4af Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <[email protected]>
Date: Tue, 8 May 2012 20:53:05 +0300
Subject: [PATCH 2/2] http/request: Remove malloc from request_index with short 
paths


Signed-off-by: Lauri Kasanen <[email protected]>
---
 src/include/mk_request.h |    2 +-
 src/mk_http.c            |   17 +++++++++++++++--
 src/mk_request.c         |   12 ++++++------
 3 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/src/include/mk_request.h b/src/include/mk_request.h
index 8e7f6ba..f721eda 100644
--- a/src/include/mk_request.h
+++ b/src/include/mk_request.h
@@ -271,7 +271,7 @@ struct handler
 };
 
 int mk_request_header_toc_parse(struct headers_toc *toc, const char *data, int 
len);
-mk_pointer mk_request_index(char *pathfile);
+mk_pointer mk_request_index(char *pathfile, char *file_aux, const unsigned int 
flen);
 mk_pointer mk_request_header_get(struct headers_toc *toc,
                                  const char *key_name, int key_len);
 
diff --git a/src/mk_http.c b/src/mk_http.c
index bdc81ee..403d819 100644
--- a/src/mk_http.c
+++ b/src/mk_http.c
@@ -220,14 +220,27 @@ int mk_http_init(struct client_session *cs, struct 
session_request *sr)
 
         /* looking for a index file */
         mk_pointer index_file;
-        index_file = mk_request_index(sr->real_path.data);
+        char tmppath[MAX_PATH];
+        index_file = mk_request_index(sr->real_path.data, tmppath, MAX_PATH);
 
         if (index_file.data) {
             if (sr->real_path.data != sr->real_path_static) {
                 mk_pointer_free(&sr->real_path);
+                sr->real_path = index_file;
+                sr->real_path.data = strdup(index_file.data);
+            }
+            /* If it's static, and still fits */
+            else if (index_file.len < MK_PATH_BASE) {
+                memcpy(sr->real_path_static, index_file.data, index_file.len);
+                sr->real_path_static[index_file.len] = '\0';
+                sr->real_path.len = index_file.len;
+            }
+            /* It was static, but didn't fit */
+            else {
+                sr->real_path = index_file;
+                sr->real_path.data = strdup(index_file.data);
             }
 
-            sr->real_path = index_file;
             mk_file_get_info(sr->real_path.data, &sr->file_info);
         }
     }
diff --git a/src/mk_request.c b/src/mk_request.c
index 21cfa70..b5220a0 100644
--- a/src/mk_request.c
+++ b/src/mk_request.c
@@ -676,10 +676,9 @@ int mk_handler_write(int socket, struct client_session *cs)
 }
 
 /* Look for some  index.xxx in pathfile */
-mk_pointer mk_request_index(char *pathfile)
+mk_pointer mk_request_index(char *pathfile, char *file_aux, const unsigned int 
flen)
 {
     unsigned long len;
-    char *file_aux = NULL;
     mk_pointer f;
     struct mk_string_line *entry;
     struct mk_list *head;
@@ -688,16 +687,17 @@ mk_pointer mk_request_index(char *pathfile)
 
     mk_list_foreach(head, config->index_files) {
         entry = mk_list_entry(head, struct mk_string_line, _head);
-        mk_string_build(&file_aux, &len, "%s%s",
-                        pathfile, entry->val);
+        len = snprintf(file_aux, flen, "%s%s", pathfile, entry->val);
+        if (len > flen) {
+            len = flen;
+            mk_warn("Path too long, truncated! '%s'", file_aux);
+        }
 
         if (access(file_aux, F_OK) == 0) {
             f.data = file_aux;
             f.len = len;
             return f;
         }
-        mk_mem_free(file_aux);
-        file_aux = NULL;
     }
 
     return f;
-- 
1.7.2.1

_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to