-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA17-1-1.cpp
More file actions
95 lines (64 loc) · 1.24 KB
/
A17-1-1.cpp
File metadata and controls
95 lines (64 loc) · 1.24 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Rule: A17-1-1
// Source line: 28027
// Original file: A17-1-1.cpp
// $Id: A17-1-1.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stdexcept>
void Fn1(const char* filename)
{
FILE* handle = fopen(filename, "rb");
if (handle == NULL)
{
throw std::system_error(errno, std::system_category());
}
// ...
fclose(handle);
// Compliant - C code is isolated; fn1()
// function is a wrapper.
}
void Fn2() noexcept
{
try
{
Fn1("filename.txt");
// Compliant - fn1() allows you to use C code like
// C++ code
// ...
}
catch (std::system_error& e)
{
std::cerr << "Error: " << e.code() << " - " << e.what() << ’\n’;
}
}
std::int32_t Fn3(const char* filename) noexcept // Non-compliant - placing C
// functions calls along with C++
// code forces a developer to be
// responsible for C-specific error
// handling, explicit resource
// cleanup, etc.
{
FILE* handle = fopen(filename, "rb");
if (handle == NULL)
{
std::cerr << "An error occured: " << errno << " - " << strerror(errno)
<< ’\n’;
return errno;
}
try
{
// ...
fclose(handle);
}
catch (std::system_error& e)
{
fclose(handle);
}
catch (std::exception& e)
{
fclose(handle);
}
return errno;
}