-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert-STR50.cpp
49 lines (42 loc) · 1010 Bytes
/
cert-STR50.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// STR50-CPP. Guarantee that storage for strings has sufficient space for character data and the null
// terminator
//
#include <iostream>
#include <sstream>
#include <string>
namespace {
void foo(std::istream& in)
{
char buf[12];
in >> buf; // diagnositc required
}
void bar(std::istream& in)
{
constexpr size_t SIZE = 12;
char bufOne[SIZE];
char bufTwo[SIZE];
in.width(SIZE); // NOTE: should be (SIZE-1)! CK
in >> bufOne; // diagnositc required
in >> bufTwo; // diagnositc required
}
void bad(std::istream& in)
{
char buffer[32];
try {
in.read(buffer, sizeof(buffer)); // NOTE: should be (SIZE-1)! CK
} catch (std::ios_base::failure& e) {
// Handle error
}
std::string str(buffer); // diagnositc required
// ...
std::cout << str << std::endl;
}
} // namespace
int main()
{
std::istringstream ss("1111111111111 2222222222222 3333333333333 4444444444444444444444444444444");
foo(ss);
bar(ss);
bad(ss);
}