Changeset 18182 in webkit for trunk/JavaScriptCore/kjs/regexp.cpp


Ignore:
Timestamp:
Dec 12, 2006, 12:09:58 PM (18 years ago)
Author:
ap
Message:

Reviewed by Geoff. Based on a patch by Maks Orlovich.

http://bugs.webkit.org/show_bug.cgi?id=6257
Throw errors on invalid expressions (KJS merge)

JavaScriptCore:

  • kjs/regexp.cpp: (KJS::RegExp::RegExp): (KJS::RegExp::~RegExp): (KJS::RegExp::match):
  • kjs/regexp.h: (KJS::RegExp::flags): (KJS::RegExp::isValid): (KJS::RegExp::errorMessage): (KJS::RegExp::subPatterns): Remember and report RegExp construction failures. Renamed data members not to start with underscores.
  • kjs/regexp_object.cpp: (RegExpObjectImp::construct): Raise an exception if RegExp construction fails. (RegExpObjectImp::callAsFunction): Removed an obsolete comment.
  • tests/mozilla/ecma_3/RegExp/regress-119909.js: Reduced the number of nested parentheses to a value supported by PCRE.

LayoutTests:

  • fast/js/kde/RegExp-expected.txt: One more test passes.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/regexp.cpp

    r17862 r18182  
    22/*
    33 *  This file is part of the KDE libraries
    4  *  Copyright (C) 1999-2001 Harri Porten ([email protected])
     4 *  Copyright (C) 1999-2001,2004 Harri Porten ([email protected])
    55 *
    66 *  This library is free software; you can redistribute it and/or
     
    2323#include "regexp.h"
    2424
     25#include "lexer.h"
     26
    2527#include <assert.h>
    2628#include <stdio.h>
     
    3133
    3234RegExp::RegExp(const UString &p, int flags)
    33   : _flags(flags), _numSubPatterns(0)
     35  : m_flags(flags), m_constructionError(0), m_numSubPatterns(0)
    3436{
    3537#if HAVE(PCREPOSIX)
     
    4951 
    5052  pattern.append('\0');
    51   _regex = pcre_compile(reinterpret_cast<const uint16_t*>(pattern.data()),
     53  m_regex = pcre_compile(reinterpret_cast<const uint16_t*>(pattern.data()),
    5254                        options, &errorMessage, &errorOffset, NULL);
    53   if (!_regex) {
     55  if (!m_regex) {
    5456    // Try again, this time handle any \u we might find.
    5557    UString uPattern = sanitizePattern(pattern);
    56     _regex = pcre_compile(reinterpret_cast<const uint16_t*>(uPattern.data()),
     58    m_regex = pcre_compile(reinterpret_cast<const uint16_t*>(uPattern.data()),
    5759                          options, &errorMessage, &errorOffset, NULL);
    58     if (!_regex)
     60    if (!m_regex) {
     61      m_constructionError = strdup(errorMessage);
    5962      return;
     63    }
    6064  }
    6165
    6266#ifdef PCRE_INFO_CAPTURECOUNT
    6367  // Get number of subpatterns that will be returned.
    64   pcre_fullinfo(_regex, NULL, PCRE_INFO_CAPTURECOUNT, &_numSubPatterns);
     68  pcre_fullinfo(m_regex, NULL, PCRE_INFO_CAPTURECOUNT, &m_numSubPatterns);
    6569#endif
    6670
     
    8185  // Note: the Global flag is already handled by RegExpProtoFunc::execute
    8286
    83   regcomp(&_regex, p.ascii(), regflags);
    84   /* TODO check for errors */
     87  // FIXME: support \u Unicode escapes.
     88
     89  int errorCode = regcomp(&m_regex, intern.ascii(), regflags);
     90  if (errorCode != 0) {
     91    char errorMessage[80];
     92    regerror(errorCode, &m_regex, errorMessage, sizeof errorMessage);
     93    m_constructionError = strdup(errorMessage);
     94  }
    8595
    8696#endif
     
    90100{
    91101#if HAVE(PCREPOSIX)
    92   pcre_free(_regex);
     102  pcre_free(m_regex);
    93103#else
    94104  /* TODO: is this really okay after an error ? */
    95   regfree(&_regex);
    96 #endif
     105  regfree(&m_regex);
     106#endif
     107  free(m_constructionError);
    97108}
    98109
     
    113124#if HAVE(PCREPOSIX)
    114125
    115   if (!_regex)
     126  if (!m_regex)
    116127    return UString::null();
    117128
     
    125136    offsetVector = fixedSizeOffsetVector;
    126137  } else {
    127     offsetVectorSize = (_numSubPatterns + 1) * 3;
     138    offsetVectorSize = (m_numSubPatterns + 1) * 3;
    128139    offsetVector = new int [offsetVectorSize];
    129140  }
    130141
    131   const int numMatches = pcre_exec(_regex, NULL, reinterpret_cast<const uint16_t *>(s.data()), s.size(), i, 0, offsetVector, offsetVectorSize);
     142  const int numMatches = pcre_exec(m_regex, NULL, reinterpret_cast<const uint16_t *>(s.data()), s.size(), i, 0, offsetVector, offsetVectorSize);
    132143
    133144  if (numMatches < 0) {
     
    152163
    153164  char *str = strdup(s.ascii()); // TODO: why ???
    154   if (regexec(&_regex, str + i, maxMatch, rmatch, 0)) {
     165  if (regexec(&m_regex, str + i, maxMatch, rmatch, 0)) {
    155166    free(str);
    156167    return UString::null();
     
    164175
    165176  // map rmatch array to ovector used in PCRE case
    166   _numSubPatterns = 0;
     177  m_numSubPatterns = 0;
    167178  for(unsigned j = 1; j < maxMatch && rmatch[j].rm_so >= 0 ; j++)
    168       _numSubPatterns++;
    169   int ovecsize = (_numSubPatterns+1)*3; // see above
     179      m_numSubPatterns++;
     180  int ovecsize = (m_numSubPatterns+1)*3; // see above
    170181  *ovector = new int[ovecsize];
    171   for (unsigned j = 0; j < _numSubPatterns + 1; j++) {
     182  for (unsigned j = 0; j < m_numSubPatterns + 1; j++) {
    172183    if (j>maxMatch)
    173184      break;
Note: See TracChangeset for help on using the changeset viewer.