Browse code

bound memory usage

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@997 77e5149b-7576-45b1-b177-96237e5ba77b

Tomasz Kojm authored on 2004/10/14 06:00:55
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Oct 13 22:57:21 CEST 2004 (tk)
2
+----------------------------------
3
+  * libclamav/matcher-bm.c: bound memory usage
4
+
1 5
 Wed Oct 13 22:20:17 CEST 2004 (tk)
2 6
 ----------------------------------
3 7
   * libclamav/str.c: include cli_strtokbuf() from Nigel
... ...
@@ -28,6 +28,7 @@
28 28
 
29 29
 #define MIN(a,b) (a < b) ? a : b
30 30
 #define HASH(a,b,c) 211 * (unsigned char) a + 37 * (unsigned char) b + (unsigned char) c
31
+#define DHASH(a,b,c) 211 * a + 37 * b + c
31 32
 
32 33
 
33 34
 int cli_bm_addpatt(struct cl_node *root, struct cli_bm_patt *pattern)
... ...
@@ -74,19 +75,21 @@ int cli_bm_addpatt(struct cl_node *root, struct cli_bm_patt *pattern)
74 74
 int cli_bm_init(struct cl_node *root)
75 75
 {
76 76
 	int i;
77
+	unsigned int size = DHASH(256, 256, 256);
77 78
 
78 79
 
79 80
     cli_dbgmsg("in cli_bm_init()\n");
81
+    cli_dbgmsg("BM: Number of indexes = %d\n", size);
80 82
 
81
-    if(!(root->bm_shift = (int *) cli_malloc(65536 * sizeof(int))))
83
+    if(!(root->bm_shift = (int *) cli_malloc(size * sizeof(int))))
82 84
 	return CL_EMEM;
83 85
 
84
-    if(!(root->bm_suffix = (struct cli_bm_patt **) cli_calloc(65536, sizeof(struct cli_bm_patt *)))) {
86
+    if(!(root->bm_suffix = (struct cli_bm_patt **) cli_calloc(size, sizeof(struct cli_bm_patt *)))) {
85 87
 	free(root->bm_shift);
86 88
 	return CL_EMEM;
87 89
     }
88 90
 
89
-    for(i = 0; i < 65536; i++)
91
+    for(i = 0; i < size; i++)
90 92
 	root->bm_shift[i] = BM_MIN_LENGTH - BM_BLOCK_SIZE + 1;
91 93
 
92 94
     return 0;
... ...
@@ -96,13 +99,14 @@ void cli_bm_free(struct cl_node *root)
96 96
 {
97 97
 	struct cli_bm_patt *b1, *b2;
98 98
 	int i;
99
+	unsigned int size = DHASH(256, 256, 256);
99 100
 
100 101
 
101 102
     if(root->bm_shift)
102 103
 	free(root->bm_shift);
103 104
 
104 105
     if(root->bm_suffix) {
105
-	for(i = 0; i < 65536; i++) {
106
+	for(i = 0; i < size; i++) {
106 107
 	    b1 = root->bm_suffix[i];
107 108
 	    while(b1) {
108 109
 		b2 = b1;