Opened 7 years ago

Closed 6 years ago

#11536 closed Bugs (fixed)

string_ref::substr length overflow

Reported by: bibmaster@… Owned by: Marshall Clow
Milestone: To Be Determined Component: utility
Version: Severity: Problem
Keywords: Cc:

Description

basic_string_ref::substr returns invalid object in some cases:

string_ref s1("hello");
string_ref s2 = s1.substr(0, string_ref::npos - 1);
// EXPECT s2.size() <= s1.size()

version with overflow check:

basic_string_ref substr(size_type pos, size_type n=npos) const {
            ...
            // add overflow check: pos + n < n
            if ( n == npos || pos + n > size() || pos + n < n )
                n = size () - pos;
            ...

Change History (2)

comment:1 by Marshall Clow, 7 years ago

Owner: changed from No-Maintainer to Marshall Clow

Ok, that's obscure. :-) thanks for the bug report.

Beman has done a bunch of work on string_ref, and after the 1.59.0 release, I will be integrating his changes. I'll make sure that this gets fixed then.

comment:2 by Marshall Clow, 6 years ago

Resolution: fixed
Status: newclosed

Better change:

return basic_string_ref(data() + pos, (std::min)(size() - pos, n));

No worries about over/underflow on n, because we never do arithmetic on it.

No worries about over/underflow on size() - pos, because we know that size() >= pos.

Committed as: 0876da4

Note: See TracTickets for help on using tickets.