|
| 1 | +<!DOCTYPE qhelp PUBLIC |
| 2 | +"-//Semmle//qhelp//EN" |
| 3 | +"qhelp.dtd"> |
| 4 | + |
| 5 | +<qhelp> |
| 6 | + |
| 7 | + <include src="ReDoSIntroduction.inc.qhelp" /> |
| 8 | + |
| 9 | + <example> |
| 10 | + <p> |
| 11 | + |
| 12 | + Consider this use of a regular expression, which removes |
| 13 | + all leading and trailing whitespace in a string: |
| 14 | + |
| 15 | + </p> |
| 16 | + |
| 17 | + <sample language="python"> |
| 18 | + re.sub(r"^\s+|\s+$", "", text) # BAD |
| 19 | + </sample> |
| 20 | + |
| 21 | + <p> |
| 22 | + |
| 23 | + The sub-expression <code>"\s+$"</code> will match the |
| 24 | + whitespace characters in <code>text</code> from left to right, but it |
| 25 | + can start matching anywhere within a whitespace sequence. This is |
| 26 | + problematic for strings that do <strong>not</strong> end with a whitespace |
| 27 | + character. Such a string will force the regular expression engine to |
| 28 | + process each whitespace sequence once per whitespace character in the |
| 29 | + sequence. |
| 30 | + |
| 31 | + </p> |
| 32 | + |
| 33 | + <p> |
| 34 | + |
| 35 | + This ultimately means that the time cost of trimming a |
| 36 | + string is quadratic in the length of the string. So a string like |
| 37 | + <code>"a b"</code> will take milliseconds to process, but a similar |
| 38 | + string with a million spaces instead of just one will take several |
| 39 | + minutes. |
| 40 | + |
| 41 | + </p> |
| 42 | + |
| 43 | + <p> |
| 44 | + |
| 45 | + Avoid this problem by rewriting the regular expression to |
| 46 | + not contain the ambiguity about when to start matching whitespace |
| 47 | + sequences. For instance, by using a negative look-behind |
| 48 | + (<code>^\s+|(?<!\s)\s+$</code>), or just by using the built-in strip |
| 49 | + method (<code>text.strip()</code>). |
| 50 | + |
| 51 | + </p> |
| 52 | + |
| 53 | + <p> |
| 54 | + |
| 55 | + Note that the sub-expression <code>"^\s+"</code> is |
| 56 | + <strong>not</strong> problematic as the <code>^</code> anchor restricts |
| 57 | + when that sub-expression can start matching, and as the regular |
| 58 | + expression engine matches from left to right. |
| 59 | + |
| 60 | + </p> |
| 61 | + |
| 62 | + </example> |
| 63 | + |
| 64 | + <example> |
| 65 | + |
| 66 | + <p> |
| 67 | + |
| 68 | + As a similar, but slightly subtler problem, consider the |
| 69 | + regular expression that matches lines with numbers, possibly written |
| 70 | + using scientific notation: |
| 71 | + </p> |
| 72 | + |
| 73 | + <sample language="python"> |
| 74 | + ^0\.\d+E?\d+$ # BAD |
| 75 | + </sample> |
| 76 | + |
| 77 | + <p> |
| 78 | + |
| 79 | + The problem with this regular expression is in the |
| 80 | + sub-expression <code>\d+E?\d+</code> because the second |
| 81 | + <code>\d+</code> can start matching digits anywhere after the first |
| 82 | + match of the first <code>\d+</code> if there is no <code>E</code> in |
| 83 | + the input string. |
| 84 | + |
| 85 | + </p> |
| 86 | + |
| 87 | + <p> |
| 88 | + |
| 89 | + This is problematic for strings that do <strong>not</strong> |
| 90 | + end with a digit. Such a string will force the regular expression |
| 91 | + engine to process each digit sequence once per digit in the sequence, |
| 92 | + again leading to a quadratic time complexity. |
| 93 | + |
| 94 | + </p> |
| 95 | + |
| 96 | + <p> |
| 97 | + |
| 98 | + To make the processing faster, the regular expression |
| 99 | + should be rewritten such that the two <code>\d+</code> sub-expressions |
| 100 | + do not have overlapping matches: <code>^0\.\d+(E\d+)?$</code>. |
| 101 | + |
| 102 | + </p> |
| 103 | + |
| 104 | + </example> |
| 105 | + |
| 106 | + <include src="ReDoSReferences.inc.qhelp"/> |
| 107 | + |
| 108 | +</qhelp> |
0 commit comments