@@ -3219,8 +3219,8 @@ PHP_FUNCTION(mb_levenshtein)
3219
3219
3220
3220
zend_long c0 , c1 , c2 ;
3221
3221
3222
- p1 = safe_emalloc (strlen_1 , sizeof (zend_long ), 0 );
3223
- p2 = safe_emalloc (strlen_2 , sizeof (zend_long ), 0 );
3222
+ p1 = safe_emalloc (strlen_1 + 1 , sizeof (zend_long ), 0 );
3223
+ p2 = safe_emalloc (strlen_2 + 1 , sizeof (zend_long ), 0 );
3224
3224
3225
3225
for (i2 = 0 ; i2 <= strlen_2 ; i2 ++ ) {
3226
3226
p1 [i2 ] = i2 * cost_ins ;
@@ -3232,29 +3232,43 @@ PHP_FUNCTION(mb_levenshtein)
3232
3232
3233
3233
while (in_len_1 ) {
3234
3234
tmp_wchar_len_1 = enc -> to_wchar (& in_1 , & in_len_1 , wchar_buf_1 , 128 , & state );
3235
- ZEND_ASSERT (in_len_1 <= 128 );
3235
+ ZEND_ASSERT (tmp_wchar_len_1 <= 128 );
3236
3236
tmp_wchar_len_2 = enc -> to_wchar (& in_2 , & in_len_2 , wchar_buf_2 , 128 , & state );
3237
3237
len_2 += tmp_wchar_len_2 ;
3238
- ZEND_ASSERT (in_len_2 <= 128 );
3238
+ ZEND_ASSERT (tmp_wchar_len_2 <= 128 );
3239
3239
3240
3240
for (i1 = 0 ; i1 < tmp_wchar_len_1 ; i1 ++ ) {
3241
3241
/* First loop that does not cross a 128 code points */
3242
3242
if (first ) {
3243
3243
p2 [0 ] = p1 [0 ] + cost_del ;
3244
3244
}
3245
- /* Insertion process when there is a surplus of 128 code points. */
3246
3245
if (tmp_wchar_len_2 == 0 ) {
3246
+ /* Insertion process when there is a surplus of 128 code points. */
3247
3247
for (i2 = 0 ; i2 < tmp_wchar_len_1 ; i2 ++ ) {
3248
- c0 = p1 [i2 + (len_2 - tmp_wchar_len_1 )] + cost_rep ;
3249
- c1 = p1 [i2 + (len_2 - tmp_wchar_len_1 ) + 1 ] + cost_del ;
3248
+ /* for overflow */
3249
+ if (len_2 < tmp_wchar_len_1 ) {
3250
+ c0 = p1 [i2 ] + cost_rep ;
3251
+ c1 = p1 [i2 + 1 ] + cost_del ;
3252
+ } else {
3253
+ c0 = p1 [i2 + (len_2 - tmp_wchar_len_1 )] + cost_rep ;
3254
+ c1 = p1 [i2 + (len_2 - tmp_wchar_len_1 ) + 1 ] + cost_del ;
3255
+ }
3250
3256
if (c1 < c0 ) {
3251
3257
c0 = c1 ;
3252
3258
}
3253
- c2 = p2 [i2 + (len_2 - tmp_wchar_len_1 )] + cost_ins ;
3259
+ if (len_2 < tmp_wchar_len_1 ) {
3260
+ c2 = p2 [i2 ] + cost_ins ;
3261
+ } else {
3262
+ c2 = p2 [i2 ] + cost_ins ;
3263
+ }
3254
3264
if (c2 < c0 ) {
3255
3265
c0 = c2 ;
3256
3266
}
3257
- p2 [i2 + (len_2 - tmp_wchar_len_1 ) + 1 ] = c0 ;
3267
+ if (len_2 < tmp_wchar_len_1 ) {
3268
+ p2 [i2 + 1 ] = c0 ;
3269
+ } else {
3270
+ p2 [i2 + (len_2 - tmp_wchar_len_1 ) + 1 ] = c0 ;
3271
+ }
3258
3272
}
3259
3273
} else {
3260
3274
for (i2 = 0 ; i2 < tmp_wchar_len_2 ; i2 ++ ) {
0 commit comments