diff --git a/src/header/crypt/md5.rs b/src/header/crypt/md5.rs index eabbb9e467..ac557a2ae5 100644 --- a/src/header/crypt/md5.rs +++ b/src/header/crypt/md5.rs @@ -135,8 +135,9 @@ pub fn crypt_md5(passw: &[u8], setting: &str) -> Option { } let cursor = 3; + let end = setting.len().min(cursor + SALT_MAX); let slen = cursor - + setting[cursor..cursor + SALT_MAX] + + setting[cursor..end] .chars() .take_while(|c| *c != '$') .count(); diff --git a/tests/crypt/md5.c b/tests/crypt/md5.c index 4251d70f9d..f179d9965c 100644 --- a/tests/crypt/md5.c +++ b/tests/crypt/md5.c @@ -15,6 +15,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -34,5 +35,16 @@ int main () { printf("Success!\n"); } + /* short salt must not panic (index out of bounds) */ + cp = crypt("test", "$1$ab"); + if (cp) { + assert(strncmp(cp, "$1$ab$", 6) == 0); + } + + cp = crypt("test", "$1$"); + if (cp) { + assert(strncmp(cp, "$1$$", 4) == 0); + } + return result; } \ No newline at end of file