Fixed a buffer overrun

This commit is contained in:
John Wiegley 2009-02-27 01:46:16 -04:00
parent 0ed57916cf
commit 316b854676
3 changed files with 24 additions and 9 deletions

View file

@ -335,8 +335,13 @@ string format_t::truncate(const unistring& ustr, std::size_t width,
if (newlen > width) { if (newlen > width) {
unistring temp(*i); unistring temp(*i);
if (temp.length() > static_cast<std::size_t>(account_abbrev_length)) {
result << temp.extract(0, account_abbrev_length) << ":"; result << temp.extract(0, account_abbrev_length) << ":";
newlen -= temp.length() - account_abbrev_length; newlen -= temp.length() - account_abbrev_length;
} else {
result << temp.extract() << ":";
newlen -= temp.length();
}
} else { } else {
result << *i << ":"; result << *i << ":";
} }
@ -346,7 +351,8 @@ string format_t::truncate(const unistring& ustr, std::size_t width,
// Even abbreviated its too big to show the last account, so // Even abbreviated its too big to show the last account, so
// abbreviate all but the last and truncate at the beginning. // abbreviate all but the last and truncate at the beginning.
unistring temp(result.str()); unistring temp(result.str());
buf << ".." << temp.extract(temp.length() - width - 2, width - 2); assert(temp.length() > width - 2);
buf << ".." << temp.extract(temp.length() - (width - 2), width - 2);
} else { } else {
buf << result.str(); buf << result.str();
} }

View file

@ -48,6 +48,8 @@
#include "utils.h" #include "utils.h"
namespace ledger {
/** /**
* @class unistring * @class unistring
* *
@ -68,7 +70,7 @@ public:
const char * p = input.c_str(); const char * p = input.c_str();
std::size_t len = input.length(); std::size_t len = input.length();
//assert(utf8::is_valid(p, p + len)); assert(utf8::is_valid(p, p + len));
utf8::utf8to32(p, p + len, std::back_inserter(utf32chars)); utf8::utf8to32(p, p + len, std::back_inserter(utf32chars));
} }
~unistring() { ~unistring() {
@ -83,8 +85,13 @@ public:
const std::size_t len = 0) const const std::size_t len = 0) const
{ {
std::string utf8result; std::string utf8result;
std::size_t this_len = length();
assert(begin <= this_len);
assert(begin + len <= this_len);
if (this_len)
utf8::utf32to8(utf32chars.begin() + begin, utf8::utf32to8(utf32chars.begin() + begin,
utf32chars.begin() + begin + (len ? len : length()), utf32chars.begin() + begin +
(len ? (len > this_len ? this_len : len) : this_len),
std::back_inserter(utf8result)); std::back_inserter(utf8result));
return utf8result; return utf8result;
} }
@ -108,4 +115,6 @@ inline void justify(std::ostream& out,
out << str; out << str;
} }
} // namespace ledger
#endif // _UNISTRING_H #endif // _UNISTRING_H

View file

@ -4,8 +4,8 @@ reg --anon
Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00 Assets:Investments:Vanguard:VMMXX 0.350 VMMXX @ $1.00
Income:Dividends:Vanguard:VMMXX $-0.35 Income:Dividends:Vanguard:VMMXX $-0.35
>>>1 >>>1
07-Feb-02 6a93dcb30aa7722e967.. ..8000cfb2807d6a978b43 0.350 VMMXX 0.350 VMMXX 07-Feb-02 6a93dcb30aa7722e967.. ..cfb2807d6a978b431fc7 0.350 VMMXX 0.350 VMMXX
..b52e4fbac0065bfcc300 $-0.35 $-0.35 ..4fbac0065bfcc300a24e $-0.35 $-0.35
0.350 VMMXX 0.350 VMMXX
>>>2 >>>2
=== 0 === 0