[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master 75129ca 4/5: Avoid undefined behavior; modern
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master 75129ca 4/5: Avoid undefined behavior; modernize a "while" statement |
Date: |
Wed, 25 Jan 2017 01:45:54 +0000 (UTC) |
branch: master
commit 75129caf03f5a5e0dd1850c6be4f06a5f231eaf9
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>
Avoid undefined behavior; modernize a "while" statement
Advancing an iterator past one-past-the-end violates the postcondition
that the result must be either dereferenceable or one-past-the-end,
even if it is never actually dereferenced.
---
ledger.cpp | 13 ++++---------
1 file changed, 4 insertions(+), 9 deletions(-)
diff --git a/ledger.cpp b/ledger.cpp
index 5262ecb..191de92 100644
--- a/ledger.cpp
+++ b/ledger.cpp
@@ -162,7 +162,6 @@ void Ledger::SetRunBases(int length)
void Ledger::ZeroInforceAfterLapse()
{
ledger_map_t const& l_map_rep = ledger_map_->held();
- ledger_map_t::const_iterator this_i = l_map_rep.begin();
// Pick the highest lapse year of any basis (i.e. any LedgerVariant).
// Set inforce lives to zero at the end of that year and thereafter.
@@ -172,20 +171,16 @@ void Ledger::ZeroInforceAfterLapse()
// that the vector of inforce lives does not correspond to the
// current values.
int lapse_year = 0;
- while(this_i != l_map_rep.end())
+ for(auto const& i : l_map_rep)
{
lapse_year = std::max
(lapse_year
- ,static_cast<int>((*this_i).second.LapseYear)
+ ,static_cast<int>(i.second.LapseYear)
);
- ++this_i;
}
- std::vector<double>::iterator b =
- ledger_invariant_->InforceLives.begin()
- + 1
- + lapse_year
- ;
+ std::vector<double>::iterator b = ledger_invariant_->InforceLives.begin();
std::vector<double>::iterator e = ledger_invariant_->InforceLives.end();
+ b += std::min(e - b, 1 + lapse_year);
if(b < e)
{
std::fill(b, e, 0.0);
- [lmi-commits] [lmi] master updated (2c94c87 -> 036a6b0), Greg Chicares, 2017/01/24
- [lmi-commits] [lmi] master de0060c 1/5: Last element is spelled "back()", Greg Chicares, 2017/01/24
- [lmi-commits] [lmi] master 800cf4b 2/5: Clarify parsing of documents with a single child element, Greg Chicares, 2017/01/24
- [lmi-commits] [lmi] master 75129ca 4/5: Avoid undefined behavior; modernize a "while" statement,
Greg Chicares <=
- [lmi-commits] [lmi] master 036a6b0 5/5: Modernize for statements, Greg Chicares, 2017/01/24
- [lmi-commits] [lmi] master cdfcd2d 3/5: Refactor, trivially, Greg Chicares, 2017/01/24