Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Fix a memory corruption #6

Merged
merged 9 commits into from
Mar 12, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/node_modules
/build
/lib/*.js
*.swp
*.o
*.lo
*.la
Expand Down
4 changes: 0 additions & 4 deletions Gruntfile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ module.exports = (grunt) ->
include: false
legal:
copyright: false
readability:
braces: false
runtime:
sizeof: false
whitespace:
line_length: false

Expand Down
14 changes: 11 additions & 3 deletions src/onig-reg-exp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using ::v8::Exception;
using ::v8::String;

OnigRegExp::OnigRegExp(const string& source) : source_(source) {
OnigRegExp::OnigRegExp(const string& source)
: source_(source),
regex_(NULL) {
OnigErrorInfo error;
const UChar* sourceData = (const UChar*)source.data();
int status = onig_new(&regex_, sourceData, sourceData + source.length(),
Expand All @@ -28,7 +30,13 @@ bool OnigRegExp::Contains(const string& value) {
return source_.find(value) != string::npos;
}

OnigResult* OnigRegExp::Search(const string& searchString, size_t position) {
shared_ptr<OnigResult> OnigRegExp::Search(const string& searchString,
size_t position) {
if (!regex_) {
ThrowException(Exception::Error(String::New("RegExp is not valid")));
return NULL;
}

int end = searchString.size();
OnigRegion* region = onig_region_new();
const UChar* searchData = (const UChar*)searchString.data();
Expand All @@ -37,7 +45,7 @@ OnigResult* OnigRegExp::Search(const string& searchString, size_t position) {
ONIG_OPTION_NONE);

if (status != ONIG_MISMATCH) {
return new OnigResult(region);
return shared_ptr<OnigResult>(new OnigResult(region));
} else {
onig_region_free(region, 1);
return NULL;
Expand Down
4 changes: 3 additions & 1 deletion src/onig-reg-exp.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#ifndef SRC_ONIG_REG_EXP_H_
#define SRC_ONIG_REG_EXP_H_

#include <memory>
#include <string>

#include "oniguruma.h"

using ::std::shared_ptr;
using ::std::string;

class OnigResult;
Expand All @@ -16,7 +18,7 @@ class OnigRegExp {

bool Contains(const string& value);
int LocationAt(int index);
OnigResult *Search(const string &searchString, size_t position);
shared_ptr<OnigResult> Search(const string &searchString, size_t position);

private:
OnigRegExp(const OnigRegExp&); // Disallow copying
Expand Down
4 changes: 2 additions & 2 deletions src/onig-result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "onig-result.h"
#include "unicode-utils.h"

OnigResult::OnigResult(OnigRegion* region) {
region_ = region;
OnigResult::OnigResult(OnigRegion* region)
: region_(region) {
}

OnigResult::~OnigResult() {
Expand Down
10 changes: 5 additions & 5 deletions src/onig-scanner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ Handle<Value> OnigScanner::FindNextMatch(Handle<String> v8String, Handle<Number>
OnigRegExp *regExp = (*iter).get();

bool useCachedResult = false;
OnigResult *result = NULL;
shared_ptr<OnigResult> result;

if (useCachedResults && index <= maxCachedIndex) {
result = cachedResults[index].get();
if (result != NULL) {
result = cachedResults[index];
if (result) {
int location = result->LocationAt(0);
if (hasMultibyteCharacters) {
location = UnicodeUtils::characters_in_bytes(string.data(), location);
Expand All @@ -98,7 +98,7 @@ Handle<Value> OnigScanner::FindNextMatch(Handle<String> v8String, Handle<Number>

if (!useCachedResult) {
result = regExp->Search(string, byteOffset);
cachedResults[index] = shared_ptr<OnigResult>(result);
cachedResults[index] = result;
maxCachedIndex = index;
}

Expand All @@ -110,7 +110,7 @@ Handle<Value> OnigScanner::FindNextMatch(Handle<String> v8String, Handle<Number>

if (bestIndex == -1 || location < bestLocation) {
bestLocation = location;
bestResult = result;
bestResult = result.get();
bestIndex = index;
}

Expand Down