cvs: php4 /ext/standard levenshtein.c
| From: | Hartmut Holzgraefe | Date: | Thu, 14 Dec 2000 16:38:16 +0000 |
| Subject: | cvs: php4 /ext/standard levenshtein.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-3158@lists.php.net to get a copy of this message | ||
hholzgra Thu Dec 14 08:38:16 2000 EDT
Modified files:
/php4/ext/standard levenshtein.c
Log:
enforce lenght limit and catch out-ouf-memory conditions
Index: php4/ext/standard/levenshtein.c
diff -u php4/ext/standard/levenshtein.c:1.13 php4/ext/standard/levenshtein.c:1.14
--- php4/ext/standard/levenshtein.c:1.13 Wed Dec 13 15:26:19 2000
+++ php4/ext/standard/levenshtein.c Thu Dec 14 08:38:15 2000
@@ -15,7 +15,7 @@
| Author: Hartmut Holzgraefe <hartmut@six.de> |
+----------------------------------------------------------------------+
*/
-/* $Id: levenshtein.c,v 1.13 2000/12/13 23:26:19 hholzgra Exp $ */
+/* $Id: levenshtein.c,v 1.14 2000/12/14 16:38:15 hholzgra Exp $ */
#include "php.h"
#include <stdlib.h>
@@ -23,6 +23,8 @@
#include <ctype.h>
#include "php_string.h"
+#define LEVENSHTEIN_MAX_LENTH 255
+
/* reference implementation, only optimized for memory usage, not speed */
static int reference_levdist(const char *s1, int l1,
const char *s2, int l2,
@@ -33,9 +35,17 @@
if(l1==0) return l2*cost_ins;
if(l2==0) return l1*cost_del;
+
+ if((l1>LEVENSHTEIN_MAX_LENTH)||(l2>LEVENSHTEIN_MAX_LENTH))
+ return -1;
- p1=malloc(l2*sizeof(int));
- p2=malloc(l2*sizeof(int));
+ if(!(p1=emalloc(l2*sizeof(int)))) {
+ return -2;
+ }
+ if(!(p2=emalloc(l2*sizeof(int)))) {
+ free(p1);
+ return -2;
+ }
p1[0]=(s1[0]==s2[0])?0:cost_rep;
@@ -57,8 +67,8 @@
c0=p1[l2-1];
- free(p1);
- free(p2);
+ efree(p1);
+ efree(p2);
return c0;
}