Skip to content

Commit a446b30

Browse files
committed
Make auto-discovery error message handling more failsafe
Currently it is too easy to fill the stringstream err_ss with an error message but forget to assign the contents to the string err_str. Reduce scope of stringstream err_ss to where it is used to make such mistakes more obvious. In C++20, this may be replaced with std::format(). Signed-off-by: Peter Colberg <[email protected]>
1 parent c8d7f3c commit a446b30

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/acl_auto_configure.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,15 @@ bool acl_load_device_def_from_str(const std::string &config_str,
198198
std::string &err_str) noexcept {
199199
acl_assert_locked();
200200

201-
auto result = !config_str.empty();
201+
bool result = !config_str.empty();
202+
err_str.clear();
203+
202204
std::string::size_type curr_pos = 0;
203-
std::stringstream err_ss;
204205
int total_fields = 0;
205206
std::vector<int> counters;
206207

207208
if (!result) {
209+
std::stringstream err_ss;
208210
err_ss << "FAILED to read auto-discovery string at byte " << curr_pos
209211
<< ": Expected non-zero value. Full string value is " << config_str
210212
<< "\n";
@@ -222,6 +224,7 @@ bool acl_load_device_def_from_str(const std::string &config_str,
222224
static_cast<unsigned>(
223225
ACL_AUTO_CONFIGURE_BACKWARDS_COMPATIBLE_WITH_VERSIONID)) {
224226
result = false;
227+
std::stringstream err_ss;
225228
err_ss << "Error: The accelerator hardware currently programmed is "
226229
"incompatible with this\nversion of the runtime (" ACL_VERSION
227230
" Commit " ACL_GIT_COMMIT ")."
@@ -557,6 +560,7 @@ bool acl_load_device_def_from_str(const std::string &config_str,
557560
if (!ok) {
558561
// Device global name already exist in map, but it should have been
559562
// unique.
563+
std::stringstream err_ss;
560564
err_ss << "Device global name should be unique. " << device_global_name
561565
<< " is repeated.\n";
562566
err_str = err_ss.str();
@@ -652,6 +656,7 @@ bool acl_load_device_def_from_str(const std::string &config_str,
652656
devdef.accel[i].is_workitem_invariant, counters);
653657
if (!devdef.accel[i].is_workgroup_invariant &&
654658
devdef.accel[i].is_workitem_invariant) {
659+
std::stringstream err_ss;
655660
err_ss << "FAILED to read auto-discovery string at byte " << curr_pos
656661
<< ": kernel cannot be workitem-invariant while it is "
657662
"workgroup-variant. "
@@ -975,7 +980,8 @@ bool acl_load_device_def_from_str(const std::string &config_str,
975980
devdef.accel.clear();
976981
devdef.hal_info.clear();
977982
}
978-
if (!result && err_ss.str().empty()) {
983+
if (!result && err_str.empty()) {
984+
std::stringstream err_ss;
979985
err_ss << "FAILED to read auto-discovery string at byte " << curr_pos
980986
<< ". Full auto-discovery string value is " << config_str << "\n";
981987
err_str = err_ss.str();

0 commit comments

Comments
 (0)