source: webkit/trunk/JavaScriptCore/kjs/regexp.cpp@ 13861

Last change on this file since 13861 was 13203, checked in by darin, 19 years ago

JavaScriptCore:

Reviewed by Anders.

  • kjs/lexer.cpp: (Lexer::lex): Turn off the "yylex: ERROR" message.
  • kjs/regexp.cpp: (KJS::RegExp::RegExp): Remove the code to log errors from PCRE to standard output. I think we should arrange for the error text to be in JavaScript exceptions instead at some point.
  • kxmlcore/Vector.h: Add a check for overflow so that we'll abort if we pass a too-large size rather than allocating a buffer smaller than requested.

WebCore:

Reviewed by Anders.

  • khtml/xsl/xsl_stylesheetimpl.cpp: (WebCore::XSLStyleSheetImpl::parseString): Pass XML_PARSE_NOERROR and XML_PARSE_NOWARNING. We don't want errors and warnings to be logged to stdout or stderr. If we later decide we want the error messages, then we should do the additional work to put them into the web page or the console (along with the JavaScript errors).
  • platform/ArrayImpl.cpp: (WebCore::ArrayImpl::resize): Add a preflight to protect against integer overflow due to large array size. Noticed this while looking into the malloc error message.

WebKit:

Reviewed by Anders.

  • WebView/WebDataSourcePrivate.h:
  • WebView/WebDataSource.m: (-[WebDataSource _setRepresentation:]): Clear the flag that records whether we've sent all the data to the representation or not; need this to prevent telling the same representation both that we've succeeded and then later that we've failed. (-[WebDataSource _setMainDocumentError:]): Don't send an error if representationFinishedLoading is already YES. Set representationFinishedLoading. (-[WebDataSource _finishedLoading]): Set representationFinishedLoading. (-[WebDataSource _setupForReplaceByMIMEType:]): Ditto.

WebKitTools:

Reviewed by Anders.

  • DumpRenderTree/DumpRenderTree.m: (checkedMalloc): Added. (checkedRealloc): Added. (makeLargeMallocFailSilently): Added. (main): Call makeLargeMallocFailSilently.
  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22#include "config.h"
23#include "regexp.h"
24
25#include <assert.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29
30namespace KJS {
31
32RegExp::RegExp(const UString &p, int flags)
33 : _flags(flags), _numSubPatterns(0)
34{
35#if HAVE(PCREPOSIX)
36
37 int options = PCRE_UTF8;
38 // Note: the Global flag is already handled by RegExpProtoFunc::execute.
39 // FIXME: That last comment is dubious. Not all RegExps get run through RegExpProtoFunc::execute.
40 if (flags & IgnoreCase)
41 options |= PCRE_CASELESS;
42 if (flags & Multiline)
43 options |= PCRE_MULTILINE;
44
45 const char *errorMessage;
46 int errorOffset;
47 UString nullTerminated(p);
48 char null(0);
49 nullTerminated.append(null);
50 _regex = pcre_compile(reinterpret_cast<const uint16_t *>(nullTerminated.data()), options, &errorMessage, &errorOffset, NULL);
51 if (!_regex)
52 return;
53
54#ifdef PCRE_INFO_CAPTURECOUNT
55 // Get number of subpatterns that will be returned.
56 pcre_fullinfo(_regex, NULL, PCRE_INFO_CAPTURECOUNT, &_numSubPatterns);
57#endif
58
59#else /* HAVE(PCREPOSIX) */
60
61 int regflags = 0;
62#ifdef REG_EXTENDED
63 regflags |= REG_EXTENDED;
64#endif
65#ifdef REG_ICASE
66 if ( f & IgnoreCase )
67 regflags |= REG_ICASE;
68#endif
69
70 //NOTE: Multiline is not feasible with POSIX regex.
71 //if ( f & Multiline )
72 // ;
73 // Note: the Global flag is already handled by RegExpProtoFunc::execute
74
75 regcomp(&_regex, p.ascii(), regflags);
76 /* TODO check for errors */
77
78#endif
79}
80
81RegExp::~RegExp()
82{
83#if HAVE(PCREPOSIX)
84 pcre_free(_regex);
85#else
86 /* TODO: is this really okay after an error ? */
87 regfree(&_regex);
88#endif
89}
90
91UString RegExp::match(const UString &s, int i, int *pos, int **ovector)
92{
93 if (i < 0)
94 i = 0;
95 int dummyPos;
96 if (!pos)
97 pos = &dummyPos;
98 *pos = -1;
99 if (ovector)
100 *ovector = 0;
101
102 if (i > s.size() || s.isNull())
103 return UString::null();
104
105#if HAVE(PCREPOSIX)
106
107 if (!_regex)
108 return UString::null();
109
110 // Set up the offset vector for the result.
111 // First 2/3 used for result, the last third used by PCRE.
112 int *offsetVector;
113 int offsetVectorSize;
114 int fixedSizeOffsetVector[3];
115 if (!ovector) {
116 offsetVectorSize = 3;
117 offsetVector = fixedSizeOffsetVector;
118 } else {
119 offsetVectorSize = (_numSubPatterns + 1) * 3;
120 offsetVector = new int [offsetVectorSize];
121 }
122
123 const int numMatches = pcre_exec(_regex, NULL, reinterpret_cast<const uint16_t *>(s.data()), s.size(), i, 0, offsetVector, offsetVectorSize);
124
125 if (numMatches < 0) {
126#ifndef NDEBUG
127 if (numMatches != PCRE_ERROR_NOMATCH)
128 fprintf(stderr, "KJS: pcre_exec() failed with result %d\n", numMatches);
129#endif
130 if (offsetVector != fixedSizeOffsetVector)
131 delete [] offsetVector;
132 return UString::null();
133 }
134
135 *pos = offsetVector[0];
136 if (ovector)
137 *ovector = offsetVector;
138 return s.substr(offsetVector[0], offsetVector[1] - offsetVector[0]);
139
140#else
141
142 const unsigned maxMatch = 10;
143 regmatch_t rmatch[maxMatch];
144
145 char *str = strdup(s.ascii()); // TODO: why ???
146 if (regexec(&_regex, str + i, maxMatch, rmatch, 0)) {
147 free(str);
148 return UString::null();
149 }
150 free(str);
151
152 if (!ovector) {
153 *pos = rmatch[0].rm_so + i;
154 return s.substr(rmatch[0].rm_so + i, rmatch[0].rm_eo - rmatch[0].rm_so);
155 }
156
157 // map rmatch array to ovector used in PCRE case
158 _numSubPatterns = 0;
159 for(unsigned j = 1; j < maxMatch && rmatch[j].rm_so >= 0 ; j++)
160 _numSubPatterns++;
161 int ovecsize = (_numSubPatterns+1)*3; // see above
162 *ovector = new int[ovecsize];
163 for (unsigned j = 0; j < _numSubPatterns + 1; j++) {
164 if (j>maxMatch)
165 break;
166 (*ovector)[2*j] = rmatch[j].rm_so + i;
167 (*ovector)[2*j+1] = rmatch[j].rm_eo + i;
168 }
169
170 *pos = (*ovector)[0];
171 return s.substr((*ovector)[0], (*ovector)[1] - (*ovector)[0]);
172
173#endif
174}
175
176} // namespace KJS
Note: See TracBrowser for help on using the repository browser.