From:             php at clayst dot com
Operating system: Windows XP Pro
PHP version:      4.4.0
PHP Bug Type:     PCRE related
Bug description:  preg_match result depends on input string length when it 
should not

Description:
------------
A regular expression which involves some unnecessary extra loops in
execution will give the proper result (a match) with a given input string
but will fail to match when it should if the input is one character
longer.

In the example, the pattern is intended to match a string which contains:

(1) An optional substring consisting of one or more groups of word
characters separated by dashes, with the entire substring (if present)
terminated by a period; followed by

(2) A required substring consisting of one or more groups of word
characters separated by dashes.

The 'good' pattern shown does this properly.  The 'bad' pattern
incorrectly makes the dashes between substrings optional -- i.e. there's
an extra '?" after the '-' in both portions of the pattern.

This error makes the pattern inefficient as there are many possible
matching substrings, but I believe it should still match a simple alpha
string.  In fact it fails to match if the input string is more than 20
characters long.


Reproduce code:
---------------
<?php
$goodpattern = '/^((\w+(-\w+)*)\.)?(\w+(-\w+)*)$/';
$badpattern = '/^((\w+(-?\w+)*)\.)?(\w+(-?\w+)*)$/';
$string1 = str_repeat('a', 20);
$string2 = str_repeat('a', 21);

print('Good pattern, 20 characters: ' . preg_match($goodpattern, $string1)
. "\n");
print('Good pattern, 21 characters: ' . preg_match($goodpattern, $string2)
. "\n");

print('Bad pattern, 20 characters:  ' . preg_match($badpattern, $string1)
. "\n");
print('Bad pattern, 21 characters:  ' . preg_match($badpattern, $string2)
. "\n");
?>

Expected result:
----------------
All matches should return a 1 because they all match the given string.

Actual result:
--------------
The first three matches return a 1 but the last returns a 0:

Good pattern, 20 characters: 1
Good pattern, 21 characters: 1
Bad pattern, 20 characters:  1
Bad pattern, 21 characters:  0

-- 
Edit bug report at http://bugs.php.net/?id=34530&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=34530&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=34530&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=34530&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=34530&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=34530&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=34530&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=34530&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=34530&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=34530&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=34530&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=34530&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=34530&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=34530&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=34530&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=34530&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=34530&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=34530&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=34530&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=34530&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=34530&r=mysqlcfg

Reply via email to