Skip to content

Remove string length limit from levenshtein() #6286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions ext/standard/levenshtein.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
#include "php.h"
#include "php_string.h"

#define LEVENSHTEIN_MAX_LENGTH 255

/* {{{ reference_levdist
* reference implementation, only optimized for memory usage, not speed */
static zend_long reference_levdist(const zend_string *string1, const zend_string *string2, zend_long cost_ins, zend_long cost_rep, zend_long cost_del )
Expand Down Expand Up @@ -75,24 +73,12 @@ PHP_FUNCTION(levenshtein)
zend_long cost_ins = 1;
zend_long cost_rep = 1;
zend_long cost_del = 1;
zend_long distance = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lll", &string1, &string2, &cost_ins, &cost_rep, &cost_del) == FAILURE) {
RETURN_THROWS();
}

if (ZSTR_LEN(string1) > LEVENSHTEIN_MAX_LENGTH) {
zend_argument_value_error(1, "must be less than %d characters", LEVENSHTEIN_MAX_LENGTH + 1);
RETURN_THROWS();
}

if (ZSTR_LEN(string2) > LEVENSHTEIN_MAX_LENGTH) {
zend_argument_value_error(2, "must be less than %d characters", LEVENSHTEIN_MAX_LENGTH + 1);
RETURN_THROWS();
}

distance = reference_levdist(string1, string2, cost_ins, cost_rep, cost_del);

RETURN_LONG(distance);
RETURN_LONG(reference_levdist(string1, string2, cost_ins, cost_rep, cost_del));
}
/* }}} */
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
--TEST--
levenshtein() error conditions
levenshtein() former error conditions
--FILE--
<?php

// levenshtein no longer has a maximum string length limit.

echo '--- String 1 ---' . \PHP_EOL;
var_dump(levenshtein('AbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrstuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsuvwxyzAbcdefghijklmnopqrtsu', 'A'));
try {
Expand All @@ -25,7 +27,7 @@ try {
--EXPECT--
--- String 1 ---
int(254)
levenshtein(): Argument #1 ($string1) must be less than 256 characters
int(255)
--- String 2 ---
int(254)
levenshtein(): Argument #2 ($string2) must be less than 256 characters
int(255)