summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/sstream5
-rw-r--r--test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp24
2 files changed, 27 insertions, 2 deletions
diff --git a/include/sstream b/include/sstream
index 34b0014c1..b01f47b68 100644
--- a/include/sstream
+++ b/include/sstream
@@ -577,6 +577,7 @@ basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
if ((__wch & (ios_base::in | ios_base::out)) == (ios_base::in | ios_base::out)
&& __way == ios_base::cur)
return pos_type(-1);
+ const ptrdiff_t __hm = __hm_ == nullptr ? 0 : __hm_ - __str_.data();
off_type __noff;
switch (__way)
{
@@ -590,13 +591,13 @@ basic_stringbuf<_CharT, _Traits, _Allocator>::seekoff(off_type __off,
__noff = this->pptr() - this->pbase();
break;
case ios_base::end:
- __noff = __hm_ - __str_.data();
+ __noff = __hm;
break;
default:
return pos_type(-1);
}
__noff += __off;
- if (__noff < 0 || __hm_ - __str_.data() < __noff)
+ if (__noff < 0 || __hm < __noff)
return pos_type(-1);
if (__noff != 0)
{
diff --git a/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp b/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp
index 6d20db131..8ec69cb24 100644
--- a/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp
+++ b/test/std/input.output/string.streams/stringbuf/stringbuf.virtuals/seekoff.pass.cpp
@@ -21,6 +21,30 @@
int main()
{
{
+ std::stringbuf sb(std::ios_base::in);
+ assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);
+ assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::out) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(0, std::ios_base::beg, std::ios_base::in) == 0);
+ assert(sb.pubseekoff(0, std::ios_base::cur, std::ios_base::in) == 0);
+ assert(sb.pubseekoff(0, std::ios_base::end, std::ios_base::in) == 0);
+ }
+ {
+ std::stringbuf sb(std::ios_base::out);
+ assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in) == -1);
+ assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(-3, std::ios_base::end, std::ios_base::in | std::ios_base::out) == -1);
+ assert(sb.pubseekoff(0, std::ios_base::beg, std::ios_base::out) == 0);
+ assert(sb.pubseekoff(0, std::ios_base::cur, std::ios_base::out) == 0);
+ assert(sb.pubseekoff(0, std::ios_base::end, std::ios_base::out) == 0);
+ }
+ {
std::stringbuf sb("0123456789", std::ios_base::in);
assert(sb.pubseekoff(3, std::ios_base::beg, std::ios_base::out) == -1);
assert(sb.pubseekoff(3, std::ios_base::cur, std::ios_base::out) == -1);