From: Ilia Alshanetsky Date: Sun, 14 Sep 2008 05:15:15 +0000 (+0000) Subject: MFB: Fixed bug #45580 (levenshtein() crashes with invalid argument) X-Git-Tag: php-5.2.7RC1~39 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=929a21247ccca440ae3d55faf23b21255e679a12;p=php MFB: Fixed bug #45580 (levenshtein() crashes with invalid argument) --- diff --git a/NEWS b/NEWS index f2ea9d0858..9cc9e81a13 100644 --- a/NEWS +++ b/NEWS @@ -45,6 +45,7 @@ PHP NEWS - Fixed bug #45691 (Some per-dir or runtime settings may leak into other requests). (Moriyoshi) - Fixed bug #45581 (htmlspecialchars() double encoding &#x hex items). (Arnaud) +- Fixed bug #45580 (levenshtein() crashes with invalid argument). (Ilia) - Fixed bug #45568 (ISAPI doesn't properly clear auth_digest in header). (Patch by: navara at emclient dot com) - Fixed bug #45556 (Return value from callback isn't freed). (Felipe) diff --git a/ext/standard/levenshtein.c b/ext/standard/levenshtein.c index 11e322fbc9..c0a91b8197 100644 --- a/ext/standard/levenshtein.c +++ b/ext/standard/levenshtein.c @@ -23,43 +23,51 @@ #include #include "php_string.h" -#define LEVENSHTEIN_MAX_LENTH 255 +#define LEVENSHTEIN_MAX_LENGTH 255 /* {{{ reference_levdist * reference implementation, only optimized for memory usage, not speed */ -static int reference_levdist(const char *s1, int l1, - const char *s2, int l2, - int cost_ins, int cost_rep, int cost_del ) +static int reference_levdist(const char *s1, int l1, const char *s2, int l2, int cost_ins, int cost_rep, int cost_del ) { int *p1, *p2, *tmp; int i1, i2, c0, c1, c2; - - if(l1==0) return l2*cost_ins; - if(l2==0) return l1*cost_del; - if((l1>LEVENSHTEIN_MAX_LENTH)||(l2>LEVENSHTEIN_MAX_LENTH)) + if (l1 == 0) { + return l2 * cost_ins; + } + if (l2 == 0) { + return l1 * cost_del; + } + + if ((l1 > LEVENSHTEIN_MAX_LENGTH) || (l2 > LEVENSHTEIN_MAX_LENGTH)) { return -1; + } + p1 = safe_emalloc((l2 + 1), sizeof(int), 0); + p2 = safe_emalloc((l2 + 1), sizeof(int), 0); - p1 = safe_emalloc((l2+1), sizeof(int), 0); - p2 = safe_emalloc((l2+1), sizeof(int), 0); - - for(i2=0;i2<=l2;i2++) - p1[i2] = i2*cost_ins; - - for(i1=0;i1