Skip to content

Commit

Permalink
fix(calculus, recognizer): memory leak due to unchecked regex error
Browse files Browse the repository at this point in the history
Closes #171
  • Loading branch information
lotem committed Jan 16, 2018
1 parent 97cd661 commit 19ddc1e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
39 changes: 21 additions & 18 deletions src/rime/algo/calculus.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
#include <boost/algorithm/string.hpp>
#include <utf8.h>
#include <rime/algo/calculus.h>
#include <rime/common.h>

namespace rime {

const double kAbbreviationPenalty = 0.5;
const double kFuzzySpellingPenalty = 0.5;

Calculus::Calculus() {
Register("xlit", &Transliteration::Parse);
Register("xform", &Transformation::Parse);
Expand All @@ -33,8 +37,7 @@ Calculation* Calculus::Parse(const string& definition) {
boost::is_from_range(definition[sep], definition[sep]));
if (args.empty())
return NULL;
map<string,
Calculation::Factory*>::iterator it = factories_.find(args[0]);
auto it = factories_.find(args[0]);
if (it == factories_.end())
return NULL;
Calculation* result = (*it->second)(args);
Expand All @@ -58,9 +61,9 @@ Calculation* Transliteration::Parse(const vector<string>& args) {
char_map[cl] = cr;
}
if (cl == 0 && cr == 0) {
Transliteration* x = new Transliteration;
the<Transliteration> x(new Transliteration);
x->char_map_.swap(char_map);
return x;
return x.release();
}
return NULL;
}
Expand Down Expand Up @@ -101,17 +104,17 @@ Calculation* Transformation::Parse(const vector<string>& args) {
const string& right(args[2]);
if (left.empty())
return NULL;
Transformation* x = new Transformation;
the<Transformation> x(new Transformation);
x->pattern_.assign(left);
x->replacement_.assign(right);
return x;
return x.release();
}

bool Transformation::Apply(Spelling* spelling) {
if (!spelling || spelling->str.empty())
return false;
string result(boost::regex_replace(spelling->str,
pattern_, replacement_));
string result = boost::regex_replace(spelling->str,
pattern_, replacement_);
if (result == spelling->str)
return false;
spelling->str.swap(result);
Expand All @@ -126,9 +129,9 @@ Calculation* Erasion::Parse(const vector<string>& args) {
const string& pattern(args[1]);
if (pattern.empty())
return NULL;
Erasion* x = new Erasion;
the<Erasion> x(new Erasion);
x->pattern_.assign(pattern);
return x;
return x.release();
}

bool Erasion::Apply(Spelling* spelling) {
Expand All @@ -149,10 +152,10 @@ Calculation* Derivation::Parse(const vector<string>& args) {
const string& right(args[2]);
if (left.empty())
return NULL;
Derivation* x = new Derivation;
the<Derivation> x(new Derivation);
x->pattern_.assign(left);
x->replacement_.assign(right);
return x;
return x.release();
}

// Fuzzing
Expand All @@ -164,17 +167,17 @@ Calculation* Fuzzing::Parse(const vector<string>& args) {
const string& right(args[2]);
if (left.empty())
return NULL;
Fuzzing* x = new Fuzzing;
the<Fuzzing> x(new Fuzzing);
x->pattern_.assign(left);
x->replacement_.assign(right);
return x;
return x.release();
}

bool Fuzzing::Apply(Spelling* spelling) {
bool result = Transformation::Apply(spelling);
if (result) {
spelling->properties.type = kFuzzySpelling;
spelling->properties.credibility *= 0.5;
spelling->properties.credibility *= kFuzzySpellingPenalty;
}
return result;
}
Expand All @@ -188,17 +191,17 @@ Calculation* Abbreviation::Parse(const vector<string>& args) {
const string& right(args[2]);
if (left.empty())
return NULL;
Abbreviation* x = new Abbreviation;
the<Abbreviation> x(new Abbreviation);
x->pattern_.assign(left);
x->replacement_.assign(right);
return x;
return x.release();
}

bool Abbreviation::Apply(Spelling* spelling) {
bool result = Transformation::Apply(spelling);
if (result) {
spelling->properties.type = kAbbreviation;
spelling->properties.credibility *= 0.5;
spelling->properties.credibility *= kAbbreviationPenalty;
}
return result;
}
Expand Down
8 changes: 7 additions & 1 deletion src/rime/gear/recognizer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ static void load_patterns(RecognizerPatterns* patterns, an<ConfigMap> map) {
auto value = As<ConfigValue>(it->second);
if (!value)
continue;
(*patterns)[it->first] = value->str();
try {
boost::regex pattern(value->str());
(*patterns)[it->first] = pattern;
} catch (boost::regex_error& e) {
LOG(ERROR) << "error parsing pattern /" << value->str() << "/: "
<< e.what();
}
}
}

Expand Down

0 comments on commit 19ddc1e

Please sign in to comment.