Skip to main content

Bug Tracker

Side navigation

#6605 closed bug (fixed)

Opened May 28, 2010 06:09AM UTC

Closed June 29, 2010 03:39AM UTC

Regexp for TRIM can freeze browser

Reported by: poppyto Owned by:
Priority: Milestone: 1.4.3
Component: core Version: 1.4.2
Keywords: trim freeze catastrophic backtracking Cc:
Blocked by: Blocking:
Description

I have a problem with the trim function in jquery 1.4.2

try the replacing regexp(rtrim) /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g

with the string below, and the browser will freeze !

" x\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0x ".replace(/^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g,"")

Lastest Firefox & Chrome are freezing !


WHY ?


Because of the large number of \\u00A0, regexbuddy says it's a case of "catastrophic backtracking"


Patch :


change

rtrim = /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g,

by

rtrim = /(?:^[\\s\\u00A0]+)|(?:[\\s\\u00A0]+$)/g,
Attachments (0)
Change History (3)

Changed May 31, 2010 02:09PM UTC by benpbenp comment:1

Came across the same problem. The suggested patch does fix the problem in that it prevents freezing, however a warning to anyone who wants to try using it: lots of stuff probably relies on the original behavior of trim and will be broken by this. For example, and without digging into the why of it, the slider plugin for jquery ui will add classes to the slider handle unbroken by spaces: "ui-slider-handleui-state-defaultui-corner-all" instead of "ui-slider-handle ui-state-default ui-corner-all".

Changed June 28, 2010 01:43PM UTC by Pointy comment:2

Note: both the original value of "rtrim" and the replacement - as quoted in this bug report, that is - are incorrect. Both should have the initial "leading string" matcher anchored with and uparrow character. Unfortunately, Trac seems to be eating those, so they don't show up in the bug report text! Therefore, if you're reading this bug report and (as benpbenp apparently did) trying the fix, make sure you include an uparrow character at the start of the new regex; you'll see the uparrow in the 1.4.2 value of rtrim if you check the source.

Changed June 29, 2010 03:39AM UTC by dmethvin comment:3

description: I have a problem with the trim function in jquery 1.4.2 \ \ try the replacing regexp(rtrim) /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g \ with the string below, and the browser will freeze ! \ \ " x\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0x ".replace(/^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g,"") \ \ Lastest Firefox & Chrome are freezing ! \ ----------- \ WHY ? \ ----------- \ Because of the large number of \\u00A0, regexbuddy says it's a case of "catastrophic backtracking" \ \ ----------- \ Patch : \ ----------- \ change \ rtrim = /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g, \ by \ rtrim = /(?:^[\\s\\u00A0]+)|(?:[\\s\\u00A0]+$)/g,I have a problem with the trim function in jquery 1.4.2 \ \ try the replacing regexp(rtrim) /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g \ with the string below, and the browser will freeze ! \ {{{ \ " x\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0\\u00A0x ".replace(/^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g,"") \ }}} \ Lastest Firefox & Chrome are freezing ! \ ----------- \ WHY ? \ ----------- \ Because of the large number of \\u00A0, regexbuddy says it's a case of "catastrophic backtracking" \ \ ----------- \ Patch : \ ----------- \ change \ {{{ \ rtrim = /^(\\s|\\u00A0)+|(\\s|\\u00A0)+$/g, \ }}} \ by \ {{{ \ rtrim = /(?:^[\\s\\u00A0]+)|(?:[\\s\\u00A0]+$)/g, \ }}}
resolution: → fixed
status: newclosed

The trim function was changed in 1.4 to remove this regexp.