Bug Tracker

Opened 13 years ago

Closed 13 years ago

#6605 closed bug (fixed)

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 (last modified by dmethvin)

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 !


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

Patch :


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


rtrim = /(?:^[\s\u00A0]+)|(?:[\s\u00A0]+$)/g,

Change History (3)

comment:1 Changed 13 years ago by benpbenp

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".

comment:2 Changed 13 years ago by Pointy

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.

comment:3 Changed 13 years ago by dmethvin

Description: modified (diff)
Resolution: fixed
Status: newclosed

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

Note: See TracTickets for help on using tickets.