Skip to content

Commit c328003

Browse files
Fix scoping. Use vectors for buffers.
Writing data to a buffer obtained by data() is undefined behavior. Fix that.
1 parent a7ddd92 commit c328003

File tree

1 file changed

+41
-40
lines changed
  • libcxx/test/std/input.output/file.streams/fstreams/ifstream.members

1 file changed

+41
-40
lines changed

libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/xsgetn.pass.cpp

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,58 @@
1515

1616
// streamsize xsgetn(char_type*, streamsize) override;
1717

18-
// This isn't a required override by the standard, but most implementations override it, since it allows for
19-
// significantly improved performance in some cases. All of this code is required to work, so this isn't a libc++
20-
// extension
18+
// This isn't a required override by the standard, but most implementations
19+
// override it, since it allows for significantly improved performance in some
20+
// cases. All of this code is required to work, so this isn't a libc++ extension
2121

2222
#include <cassert>
2323
#include <fstream>
24+
#include <vector>
2425

2526
#include "test_macros.h"
2627

2728
int main(int, char**) {
28-
{
29-
char buffer[10];
30-
std::ifstream fs("xsgetn.test.dat");
31-
std::filebuf* fb = fs.rdbuf();
32-
fb->pubsetbuf(buffer, 10);
29+
std::vector<char> stream_buffer(10);
30+
std::ifstream fs("xsgetn.test.dat");
31+
std::filebuf* fb = fs.rdbuf();
32+
fb->pubsetbuf(stream_buffer.data(), 10);
3333

34-
// Ensure that the buffer is set up
35-
assert(fb->sgetc() == 't');
34+
// Ensure that the buffer is set up
35+
assert(fb->sgetc() == 't');
3636

37-
std::string str(5, '\0');
37+
std::vector<char> test_buffer(5);
38+
test_buffer[0] = '\0';
3839

39-
{ // Check that a read smaller than the buffer works fine
40-
assert(fb->sgetn(str.data(), 5) == 5);
41-
assert(str == "this ");
42-
}
43-
{ // Check that reading up to the buffer end works fine
44-
assert(fb->sgetn(str.data(), 5) == 5);
45-
assert(str == "is so");
46-
}
47-
{ // Check that reading from an empty buffer, but more than the buffer can hold works fine
48-
str.resize(12);
49-
assert(fb->sgetn(str.data(), 12) == 12);
50-
assert(str == "me random da");
51-
}
52-
{ // Check that reading from a non-empty buffer, and more than the buffer can hold works fine
53-
// Fill the buffer up
54-
str.resize(2);
55-
assert(fb->sgetn(str.data(), 2) == 2);
56-
assert(str == "ta");
40+
{ // Check that a read smaller than the buffer works fine
41+
assert(fb->sgetn(test_buffer.data(), 5) == 5);
42+
assert(std::string(test_buffer.data()) == "this ");
43+
}
44+
{ // Check that reading up to the buffer end works fine
45+
assert(fb->sgetn(test_buffer.data(), 5) == 5);
46+
assert(std::string(test_buffer.data(), 5) == "is so");
47+
}
48+
{ // Check that reading from an empty buffer, but more than the buffer can
49+
// hold works fine
50+
test_buffer.resize(12);
51+
assert(fb->sgetn(test_buffer.data(), 12) == 12);
52+
assert(std::string(test_buffer.data(), 12) == "me random da");
53+
}
54+
{ // Check that reading from a non-empty buffer, and more than the buffer can
55+
// hold works fine Fill the buffer up
56+
test_buffer.resize(2);
57+
assert(fb->sgetn(test_buffer.data(), 2) == 2);
58+
assert(std::string(test_buffer.data(), 2) == "ta");
5759

58-
// Do the actual check
59-
str.resize(12);
60-
assert(fb->sgetn(str.data(), 12) == 12);
61-
assert(str == " to be able ");
62-
}
63-
{ // Check that trying to read more than the file size works fine
64-
str.resize(30);
65-
assert(fb->sgetn(str.data(), 30) == 24);
66-
str.resize(24);
67-
assert(str == "to test buffer behaviour");
68-
}
60+
// Do the actual check
61+
test_buffer.resize(12);
62+
assert(fb->sgetn(test_buffer.data(), 12) == 12);
63+
assert(std::string(test_buffer.data(), 12) == " to be able ");
64+
}
65+
{ // Check that trying to read more than the file size works fine
66+
test_buffer.resize(30);
67+
assert(fb->sgetn(test_buffer.data(), 30) == 24);
68+
test_buffer.resize(24);
69+
assert(std::string(test_buffer.data(), 24) == "to test buffer behaviour");
6970
}
7071

7172
return 0;

0 commit comments

Comments
 (0)