[SR-Dev] git:janakj/flatstore: - flatstore: uses now pathmax() + additional buffer checks

Jan Janak jan at iptel.org
Sun Feb 15 18:56:45 CET 2009


Module: sip-router
Branch: janakj/flatstore
Commit: 8f8d2e7a3897a6ee704638d0bb005c3438cce2a8
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=8f8d2e7a3897a6ee704638d0bb005c3438cce2a8

Author: Andrei Pelinescu-Onciul <andrei at iptel.org>
Committer: Andrei Pelinescu-Onciul <andrei at iptel.org>
Date:   Wed Dec  1 10:58:23 2004 +0000

- flatstore: uses now pathmax() + additional buffer checks
- pike: old gcc warning fix

---

 modules/db_flatstore/flat_con.c |   32 +++++++++++++++++++++++++++++---
 1 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/modules/db_flatstore/flat_con.c b/modules/db_flatstore/flat_con.c
index 8d375ec..d691c22 100644
--- a/modules/db_flatstore/flat_con.c
+++ b/modules/db_flatstore/flat_con.c
@@ -28,7 +28,6 @@
  */
 
 #include <string.h>
-#include <linux/limits.h>
 #include <errno.h>
 #include "../../mem/mem.h"
 #include "../../dprint.h"
@@ -39,17 +38,33 @@
 #define FILE_SUFFIX ".log"
 #define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
 
-
+/* returns a pkg_malloc'ed file name */
 static char* get_name(struct flat_id* id)
 {
-	static char buf[PATH_MAX];
+	char* buf;
+	int buf_len;
 	char* num, *ptr;
 	int num_len;
+	int total_len;
 
+	buf_len=pathmax();
 	if (!id) {
 		LOG(L_ERR, "get_name: Invalid parameter value\n");
 		return 0;
 	}
+	total_len=id->dir.len+1 /* / */+id->table.len+1 /* _ */+
+				FILE_SUFFIX_LEN+1 /* \0 */; /* without pid*/
+	if (buf_len<total_len){
+		LOG(L_ERR, "get_name: the path is too long (%d and PATHMAX is %d)\n",
+					total_len, buf_len);
+		return 0;
+	}
+	
+	buf=pkg_malloc(buf_len);
+	if (buf==0){
+		LOG(L_ERR, "ERROR: get_name: memory allocation failure\n");
+		return 0;
+	}
 
 	ptr = buf;
 
@@ -63,6 +78,12 @@ static char* get_name(struct flat_id* id)
 	*ptr++ = '_';
 	
 	num = int2str(flat_pid, &num_len);
+	if (buf_len<(total_len+num_len)){
+		LOG(L_ERR, "ERROR:  get_name: the path is too long (%d and PATHMAX is"
+				" %d)\n", total_len+num_len, buf_len);
+		pkg_free(buf);
+		return 0;
+	}
 	memcpy(ptr, num, num_len);
 	ptr += num_len;
 
@@ -97,8 +118,13 @@ struct flat_con* flat_new_connection(struct flat_id* id)
 	res->id = id;
 
 	fn = get_name(id);
+	if (fn==0){
+		LOG(L_ERR, "flat_new_connection: get_name() failed\n");
+		return 0;
+	}
 
 	res->file = fopen(fn, "a");
+	pkg_free(fn); /* we don't need fn anymore */
 	if (!res->file) {
 		LOG(L_ERR, "flat_new_connection: %s\n", strerror(errno));
 		pkg_free(res);




More information about the sr-dev mailing list