Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.

Commit e9d1187

Browse files
authored
Merge pull request #1147 from KFilipek/fix-coverity_examples
Small fixes for coverity issues
2 parents 3c5f479 + fd22c3e commit e9d1187

File tree

6 files changed

+97
-40
lines changed

6 files changed

+97
-40
lines changed

examples/concurrent_hash_map/concurrent_hash_map.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,23 @@ main(int argc, char *argv[])
3737
pool<root> pop;
3838
bool remove_hashmap = false;
3939

40-
try {
41-
if (argc < 2)
42-
std::cerr << "usage: " << argv[0]
43-
<< " file-name [remove_hashmap]" << std::endl;
40+
if (argc < 2)
41+
std::cerr << "usage: " << argv[0]
42+
<< " file-name [remove_hashmap]" << std::endl;
4443

45-
auto path = argv[1];
44+
auto path = argv[1];
4645

47-
if (argc == 3)
48-
remove_hashmap = std::string(argv[2]) == "1";
46+
if (argc == 3)
47+
remove_hashmap = std::string(argv[2]) == "1";
4948

50-
try {
51-
pop = pool<root>::open(path, "concurrent_hash_map");
52-
} catch (pmem::pool_error &e) {
53-
std::cerr << e.what() << std::endl;
54-
return -1;
55-
}
49+
try {
50+
pop = pool<root>::open(path, "concurrent_hash_map");
51+
} catch (pmem::pool_error &e) {
52+
std::cerr << e.what() << std::endl;
53+
return -1;
54+
}
5655

56+
try {
5757
auto &r = pop.root()->pptr;
5858

5959
if (r == nullptr) {
@@ -184,16 +184,19 @@ main(int argc, char *argv[])
184184
r = nullptr;
185185
});
186186
}
187-
pop.close();
187+
} catch (const pmem::transaction_out_of_memory &e) {
188+
std::cerr << "Exception occurred: " << e.what() << std::endl;
188189
} catch (std::exception &e) {
189190
std::cerr << "Exception occurred: " << e.what() << std::endl;
190-
try {
191-
pop.close();
192-
} catch (const std::logic_error &e) {
193-
std::cerr << "Exception: " << e.what() << std::endl;
194-
}
191+
}
192+
193+
try {
194+
pop.close();
195+
} catch (const std::logic_error &e) {
196+
std::cerr << "Exception: " << e.what() << std::endl;
195197
return -1;
196198
}
199+
197200
return 0;
198201
}
199202
//! [concurrent_hash_map_ex]

examples/concurrent_hash_map/concurrent_hash_map_string.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,17 @@ main(int argc, char *argv[])
202202
r = nullptr;
203203
});
204204
}
205-
} catch (std::exception &e) {
205+
} catch (const pmem::transaction_out_of_memory &e) {
206+
std::cerr << "Exception occurred: " << e.what() << std::endl;
207+
retval = -127;
208+
} catch (const std::exception &e) {
206209
std::cerr << "Exception occurred: " << e.what() << std::endl;
207210
retval = -1;
208211
}
209212
try {
210213
pop.close();
211214
} catch (const std::logic_error &e) {
212-
std::cerr << "Exception: " << e.what() << std::endl;
215+
std::cerr << "Exception occurred: " << e.what() << std::endl;
213216
retval = -2;
214217
}
215218
return retval;

examples/pool/pool_as_class_member.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ class Foo {
4646
~Foo()
4747
{
4848
/* Close a pmemobj pool */
49-
pop.close();
49+
try {
50+
pop.close();
51+
} catch (...) {
52+
/* Let's ignore all exceptions during close */
53+
}
5054
}
5155

5256
void

examples/transaction/transaction.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Identifier: BSD-3-Clause
2-
/* Copyright 2016-2020, Intel Corporation */
2+
/* Copyright 2016-2021, Intel Corporation */
33

44
/*
55
* transaction.cpp -- C++ documentation snippets.
@@ -302,6 +302,9 @@ struct simple_ptr {
302302
} catch (pmem::transaction_free_error &e) {
303303
std::cerr << e.what() << std::endl;
304304
std::terminate();
305+
} catch (pmem::transaction_scope_error &e) {
306+
std::cerr << e.what() << std::endl;
307+
std::terminate();
305308
}
306309
}
307310

include/libpmemobj++/container/concurrent_hash_map.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2828,7 +2828,7 @@ class concurrent_hash_map
28282828
* [start_percent, start_percent + amount_percent]
28292829
* is incorrect.
28302830
*
2831-
* @throw rethrows pmem::defrag_error when a failure during
2831+
* @throw pmem::defrag_error rethrows defrag_error when a failure during
28322832
* defragmentation occurs. Even if this error is thrown,
28332833
* some of objects could have been relocated,
28342834
* see in such case defrag_error.result for summary stats.

include/libpmemobj++/experimental/atomic_self_relative_ptr.hpp

Lines changed: 60 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,14 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
8585

8686
bool result = accessor::get_offset(ptr).compare_exchange_weak(
8787
expected_offset, desired_offset, success, failure);
88-
if (!result)
89-
expected = accessor::offset_to_pointer<T>(
90-
expected_offset, ptr);
88+
if (!result) {
89+
try {
90+
expected = accessor::offset_to_pointer<T>(
91+
expected_offset, ptr);
92+
} catch (...) {
93+
std::terminate();
94+
}
95+
}
9196
return result;
9297
}
9398

@@ -103,9 +108,14 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
103108

104109
bool result = accessor::get_offset(ptr).compare_exchange_weak(
105110
expected_offset, desired_offset, order);
106-
if (!result)
107-
expected = accessor::offset_to_pointer<T>(
108-
expected_offset, ptr);
111+
if (!result) {
112+
try {
113+
expected = accessor::offset_to_pointer<T>(
114+
expected_offset, ptr);
115+
} catch (...) {
116+
std::terminate();
117+
}
118+
}
109119
return result;
110120
}
111121

@@ -121,9 +131,14 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
121131

122132
bool result = accessor::get_offset(ptr).compare_exchange_strong(
123133
expected_offset, desired_offset, success, failure);
124-
if (!result)
125-
expected = accessor::offset_to_pointer<T>(
126-
expected_offset, ptr);
134+
if (!result) {
135+
try {
136+
expected = accessor::offset_to_pointer<T>(
137+
expected_offset, ptr);
138+
} catch (...) {
139+
std::terminate();
140+
}
141+
}
127142
return result;
128143
}
129144

@@ -139,9 +154,14 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
139154

140155
bool result = accessor::get_offset(ptr).compare_exchange_strong(
141156
expected_offset, desired_offset, order);
142-
if (!result)
143-
expected = accessor::offset_to_pointer<T>(
144-
expected_offset, ptr);
157+
if (!result) {
158+
try {
159+
expected = accessor::offset_to_pointer<T>(
160+
expected_offset, ptr);
161+
} catch (...) {
162+
std::terminate();
163+
}
164+
}
145165
return result;
146166
}
147167

@@ -191,7 +211,13 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
191211
value_type
192212
operator++() noexcept
193213
{
194-
return this->fetch_add(1) + 1;
214+
try {
215+
return this->fetch_add(1) + 1;
216+
} catch (...) {
217+
/* This should never happen during normal program
218+
* execution */
219+
std::terminate();
220+
}
195221
}
196222

197223
value_type
@@ -203,7 +229,13 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
203229
value_type
204230
operator--() noexcept
205231
{
206-
return this->fetch_sub(1) - 1;
232+
try {
233+
return this->fetch_sub(1) - 1;
234+
} catch (...) {
235+
/* This should never happen during normal program
236+
* execution */
237+
std::terminate();
238+
}
207239
}
208240

209241
value_type
@@ -215,13 +247,25 @@ struct atomic<pmem::obj::experimental::self_relative_ptr<T>> {
215247
value_type
216248
operator+=(difference_type diff) noexcept
217249
{
218-
return this->fetch_add(diff) + diff;
250+
try {
251+
return this->fetch_add(diff) + diff;
252+
} catch (...) {
253+
/* This should never happen during normal program
254+
* execution */
255+
std::terminate();
256+
}
219257
}
220258

221259
value_type
222260
operator-=(difference_type diff) noexcept
223261
{
224-
return this->fetch_sub(diff) - diff;
262+
try {
263+
return this->fetch_sub(diff) - diff;
264+
} catch (...) {
265+
/* This should never happen during normal program
266+
* execution */
267+
std::terminate();
268+
}
225269
}
226270

227271
private:

0 commit comments

Comments
 (0)