Skip to content

Commit 539db50

Browse files
committed
Fixes for bugs revealed by _GLIBCXX_ASSERTIONS and _ITERATOR_DEBUG_LEVEL=2
1 parent a28b417 commit 539db50

File tree

6 files changed

+67
-27
lines changed

6 files changed

+67
-27
lines changed

CMakeLists.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,24 @@ if(NOT ENABLE_PROFILING)
132132
endif()
133133

134134
if(WIN32)
135-
add_definitions(-DUNICODE -D_UNICODE -D_SECURE_SCL=0 -D_ITERATOR_DEBUG_LEVEL=0 -D_CRT_SECURE_NO_DEPRECATE -DWIN32_LEAN_AND_MEAN -DVC_EXTRALEAN -DNOMINMAX -D_SILENCE_CXX20_CISO646_REMOVED_WARNING -D_USE_MATH_DEFINES)
135+
add_definitions(-DUNICODE -D_UNICODE -D_CRT_SECURE_NO_DEPRECATE -DWIN32_LEAN_AND_MEAN -DVC_EXTRALEAN -DNOMINMAX -D_SILENCE_CXX20_CISO646_REMOVED_WARNING -D_USE_MATH_DEFINES)
136+
if(CMAKE_BUILD_TYPE STREQUAL "Release")
137+
add_definitions(-D_SECURE_SCL=0 -D_ITERATOR_DEBUG_LEVEL=0)
138+
else()
139+
add_definitions(-D_SECURE_SCL=1 -D_ITERATOR_DEBUG_LEVEL=2)
140+
endif()
141+
136142
include_directories("include/getopt" "include/wordexp")
137143
else()
138144
add_definitions(-D_POSIX_C_SOURCE=200112 -DBOOST_NO_CXX98_FUNCTION_BASE=1)
145+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
146+
add_definitions(-D_GLIBCXX_ASSERTIONS)
147+
endif()
139148
set(POPEN_PLUS_CPP "../include/posix/popen_plus.cpp" "../include/posix/popen_plus.hpp")
149+
140150
include_directories("include/posix")
141151
endif()
152+
142153
if(EMSCRIPTEN)
143154
include_directories("include/wordexp")
144155
set(CMAKE_EXECUTABLE_SUFFIX ".js")

src/GrammarApplicator.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ class GrammarApplicator {
261261
Rule* current_rule = nullptr;
262262
std::vector<Rule_Context> context_stack;
263263
std::vector<CohortSet*> cohortsets;
264-
std::vector<CohortSet::const_iterator*> rocits;
264+
std::vector<size_t*> rocits;
265265

266266
ReadingSpec get_attach_to();
267267
Cohort* get_mark();

src/GrammarApplicator_runRules.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,23 +79,23 @@ bool GrammarApplicator::updateRuleToCohorts(Cohort& c, const uint32_t& rsit) {
7979
}
8080
if (!csi.empty()) {
8181
auto cap = cohortset.capacity();
82-
std::vector<CohortSet::const_iterator*> ends;
83-
std::vector<std::pair<CohortSet::const_iterator*,Cohort*>> chs;
82+
std::vector<size_t*> ends;
83+
std::vector<std::pair<size_t*,Cohort*>> chs;
8484
for (size_t i = 0; i < csi.size(); ++i) {
85-
if (*rocits[csi[i]] == cohortset.end()) {
85+
if (*rocits[csi[i]] >= cohortset.size()) {
8686
ends.push_back(rocits[csi[i]]);
8787
}
8888
else {
89-
chs.push_back(std::pair(rocits[csi[i]], **rocits[csi[i]]));
89+
chs.push_back(std::pair(rocits[csi[i]], cohortset.at(*rocits[csi[i]])));
9090
}
9191
}
9292
cohortset.insert(&c);
9393
for (auto it : ends) {
94-
*it = cohortset.end();
94+
*it = cohortset.size();
9595
}
9696
if (cap != cohortset.capacity()) {
9797
for (auto& it : chs) {
98-
*it.first = cohortset.find(it.second);
98+
*it.first = cohortset.find_n(it.second);
9999
}
100100
}
101101
}
@@ -375,9 +375,9 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
375375
if (debug_level > 1) {
376376
std::cerr << "DEBUG: " << cohortset->size() << "/" << current.cohorts.size() << " = " << double(cohortset->size()) / double(current.cohorts.size()) << std::endl;
377377
}
378-
for (auto rocit = cohortset->cbegin(); (!cohortset->empty()) && (rocit != cohortset->cend());) {
378+
for (size_t rocit = 0; rocit < cohortset->size();) {
379379
rocits.back() = &rocit;
380-
Cohort* cohort = *rocit;
380+
Cohort* cohort = cohortset->at(rocit);
381381
++rocit;
382382

383383
finish_reading_loop = true;
@@ -529,14 +529,10 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
529529
cohortset = &current.rule_to_cohorts[rule.number];
530530
override_cohortset();
531531
cohortsets.back() = cohortset;
532-
if (get_apply_to().cohort->type & (CT_REMOVED | CT_IGNORED)) {
533-
rocit = cohortset->lower_bound(current.cohorts[get_apply_to().cohort->local_number]);
534-
}
535-
else {
536-
rocit = cohortset->find(current.cohorts[get_apply_to().cohort->local_number]);
537-
if (rocit != cohortset->end()) {
538-
++rocit;
539-
}
532+
auto gac = get_apply_to().cohort;
533+
rocit = gac->local_number;
534+
if (!(gac->type & (CT_REMOVED | CT_IGNORED)) && rocit < cohortset->size()) {
535+
++rocit;
540536
}
541537
};
542538

@@ -790,7 +786,7 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
790786
if (num_active == 0 && (num_iff == 0 || rule.type != K_IFF)) {
791787
if (!matched_target) {
792788
--rocit; // We have already incremented rocit earlier, so take one step back...
793-
rocit = cohortset->erase(rocit); // ...and one step forward again
789+
cohortset->erase(rocit); // ...and one step forward again
794790
}
795791
context_stack.pop_back();
796792
continue;
@@ -1223,7 +1219,7 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
12231219
}
12241220
else if (current.next) {
12251221
current.next->text = current.text_post + current.next->text;
1226-
current.next->all_cohorts.insert(current.previous->all_cohorts.begin() + 1, current.all_cohorts.begin() + 1, current.all_cohorts.end());
1222+
current.next->all_cohorts.insert(current.next->all_cohorts.begin() + 1, current.all_cohorts.begin() + 1, current.all_cohorts.end());
12271223
}
12281224
current.all_cohorts.clear();
12291225

@@ -1755,9 +1751,9 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
17551751
cCohort->relations.swap(get_apply_to().cohort->relations);
17561752

17571753
std::pair<SingleWindow**, size_t> swss[3] = {
1758-
std::make_pair(&gWindow->previous[0], gWindow->previous.size()),
1754+
std::make_pair(gWindow->previous.data(), gWindow->previous.size()),
17591755
std::make_pair(&gWindow->current, static_cast<size_t>(1)),
1760-
std::make_pair(&gWindow->next[0], gWindow->next.size()),
1756+
std::make_pair(gWindow->next.data(), gWindow->next.size()),
17611757
};
17621758
for (auto sws : swss) {
17631759
for (size_t sw = 0; sw < sws.second; ++sw) {

src/MathParser.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ inline void MathParser::eval_add_sub(double& result) {
9595
UChar op = 0;
9696
double temp = 0;
9797
eval_mul_div(result);
98-
while ((op = token[0]) == '+' || op == '-') {
98+
while (!token.empty() && ((op = token[0]) == '+' || op == '-')) {
9999
get_token();
100100
eval_mul_div(temp);
101101
switch (op) {
@@ -113,7 +113,7 @@ inline void MathParser::eval_mul_div(double& result) {
113113
UChar op = 0;
114114
double temp = 0;
115115
eval_exp(result);
116-
while ((op = token[0]) == '*' || op == '/') {
116+
while (!token.empty() && ((op = token[0]) == '*' || op == '/')) {
117117
get_token();
118118
eval_exp(temp);
119119
switch (op) {
@@ -130,7 +130,7 @@ inline void MathParser::eval_mul_div(double& result) {
130130
inline void MathParser::eval_exp(double& result) {
131131
double temp = 0;
132132
eval_unary(result);
133-
while (token[0] == '^') {
133+
while (!token.empty() && token[0] == '^') {
134134
get_token();
135135
eval_unary(temp);
136136
result = pow(result, temp);

src/interval_vector.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,26 @@ class interval_vector {
172172
}
173173

174174
bool insert(T t) {
175+
if (elements.empty()) {
176+
elements.emplace_back(interval(t));
177+
return true;
178+
}
175179
auto it = std::lower_bound(elements.begin(), elements.end(), t);
176180
if (it != elements.end() && t >= it->lb && t <= it->ub) {
177181
return false;
178182
}
183+
if (it == elements.begin()) {
184+
if (it->ub + 1 == t) {
185+
++it->ub;
186+
}
187+
else if (it->lb - 1 == t) {
188+
--it->lb;
189+
}
190+
else {
191+
elements.insert(it, interval(t));
192+
}
193+
return true;
194+
}
179195
auto pr = it - 1;
180196
if (it != elements.begin() && pr->ub + 1 == t) {
181197
++pr->ub;

src/sorted_vector.hpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ class sorted_vector {
8181
//*/
8282
iterator it = std::lower_bound(elements.begin(), elements.end(), t, comp);
8383
size_t at = std::distance(elements.begin(), it);
84-
if (it == elements.end() || comp(*it, t) || comp(t, *it)) {
84+
if (it == elements.end()) {
85+
elements.emplace_back(t);
86+
return std::make_pair(elements.begin() + at, true);
87+
}
88+
if (comp(*it, t) || comp(t, *it)) {
8589
elements.insert(it, t);
8690
return std::make_pair(elements.begin() + at, true);
8791
}
@@ -142,6 +146,10 @@ class sorted_vector {
142146
return elements.erase(elements.begin() + o);
143147
}
144148

149+
void erase(size_type i) {
150+
elements.erase(elements.begin() + i);
151+
}
152+
145153
template<typename It>
146154
void erase(It b, It e) {
147155
for (; b != e; ++b) {
@@ -166,7 +174,12 @@ class sorted_vector {
166174
return it;
167175
}
168176

169-
size_t count(T t) const {
177+
size_type find_n(T t) const {
178+
auto it = find(t);
179+
return std::distance(elements.begin(), it);
180+
}
181+
182+
size_type count(T t) const {
170183
return (find(t) != end());
171184
}
172185

@@ -214,6 +227,10 @@ class sorted_vector {
214227
return elements.back();
215228
}
216229

230+
T& at(size_type i) {
231+
return elements[i];
232+
}
233+
217234
iterator lower_bound(T t) {
218235
return std::lower_bound(elements.begin(), elements.end(), t, comp);
219236
}

0 commit comments

Comments
 (0)