=== modified file 'modules/filter/filter.module'
--- modules/filter/filter.module	2009-04-30 21:03:07 +0000
+++ modules/filter/filter.module	2009-04-30 21:05:20 +0000
@@ -895,7 +895,8 @@
       $chunk = preg_replace('!(<' . $block . '[^>]*>)!', "\n$1", $chunk); // Space things out a little
       $chunk = preg_replace('!(</' . $block . '>)!', "$1\n\n", $chunk); // Space things out a little
       $chunk = preg_replace("/\n\n+/", "\n\n", $chunk); // take care of duplicates
-      $chunk = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $chunk); // make paragraphs, including one at the end
+      $chunk = preg_replace('/^\n|\n\s*\n$/', '', $chunk);
+      $chunk = '<p>' . preg_replace('/\n\s*\n\n?(.)/', "</p>\n<p>$1", $chunk) . "</p>\n"; // make paragraphs, including one at the end
       $chunk = preg_replace("|<p>(<li.+?)</p>|", "$1", $chunk); // problem with nested lists
       $chunk = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $chunk);
       $chunk = str_replace('</blockquote></p>', '</p></blockquote>', $chunk);

=== modified file 'modules/filter/filter.test'
--- modules/filter/filter.test	2009-04-30 21:03:07 +0000
+++ modules/filter/filter.test	2009-04-30 21:03:32 +0000
@@ -208,6 +208,10 @@
 
     $f = _filter_autop('<blockquote><pre>aaa</pre></blockquote>');
     $this->assertEqual(substr_count($f, '<p>'), substr_count($f, '</p>'), t('Make sure line breaking produces matching paragraph tags.'));
+
+    $limit = max(ini_get('pcre.backtrack_limit'), ini_get('pcre.recursion_limit'));
+    $f = _filter_autop($this->randomName($limit));
+    $this->assertNotEqual($f, '', t('Make sure line breaking can process long strings.'));
   }
 
   /**

