Skip to content

Commit

Permalink
Catch and handle regex exception
Browse files Browse the repository at this point in the history
When pattern matching file names to determine API support, in
some environments std::regex will throw. This change is meant
to handle this more gracefully.

Change-Id: If1ccfe5bdd71ec4d08663c80692024488072e11b
  • Loading branch information
Chris Freehill authored and Chris Freehill committed May 14, 2020
1 parent b7ff71c commit 27148a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
15 changes: 11 additions & 4 deletions src/rocm_smi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,17 @@ static rsmi_status_t handleException() {
// This macro assumes dev already available
#define CHK_API_SUPPORT_ONLY(RT_PTR, VR, SUB_VR) \
if ((RT_PTR) == nullptr) { \
if (!dev->DeviceAPISupported(__FUNCTION__, (VR), (SUB_VR))) { \
return RSMI_STATUS_NOT_SUPPORTED; \
} \
return RSMI_STATUS_INVALID_ARGS; \
try { \
if (!dev->DeviceAPISupported(__FUNCTION__, (VR), (SUB_VR))) { \
return RSMI_STATUS_NOT_SUPPORTED; \
} \
return RSMI_STATUS_INVALID_ARGS; \
} catch (const amd::smi::rsmi_exception& e) { \
debug_print( \
"Exception caught when checking if API is supported %s.\n", \
e.what()); \
return RSMI_STATUS_INVALID_ARGS; \
} \
}

#define CHK_SUPPORT(RT_PTR, VR, SUB_VR) \
Expand Down
52 changes: 31 additions & 21 deletions src/rocm_smi_monitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -323,27 +323,34 @@ static int get_supported_sensors(std::string dir_path, std::string fn_reg_ex,
int64_t mon_val;

char *endptr;
std::regex re(fn_reg_ex);
std::string fn;

while (dentry != nullptr) {
fn = dentry->d_name;
if (std::regex_search(fn, match, re)) {
assert(match.size() == 2); // 1 for whole match + 1 for sub-match
errno = 0;
mon_val = strtol(match.str(1).c_str(), &endptr, 10);
assert(errno == 0);
assert(*endptr == '\0');
if (errno) {
closedir(hwmon_dir);
return -2;
try {
std::regex re(fn_reg_ex);
std::string fn;

while (dentry != nullptr) {
fn = dentry->d_name;
if (std::regex_search(fn, match, re)) {
assert(match.size() == 2); // 1 for whole match + 1 for sub-match
errno = 0;
mon_val = strtol(match.str(1).c_str(), &endptr, 10);
assert(errno == 0);
assert(*endptr == '\0');
if (errno) {
closedir(hwmon_dir);
return -2;
}
sensors->push_back(mon_val);
}
sensors->push_back(mon_val);
dentry = readdir(hwmon_dir);
}
dentry = readdir(hwmon_dir);
}
if (closedir(hwmon_dir)) {
return errno;
if (closedir(hwmon_dir)) {
return errno;
}
} catch (std::regex_error e) {
std::cout << "Regular expression error:" << std::endl;
std::cout << e.what() << std::endl;
std::cout << "Regex error code: " << e.code() << std::endl;
return -3;
}
return 0;
}
Expand Down Expand Up @@ -431,7 +438,7 @@ void Monitor::fillSupportedFuncs(SupportedFuncMap *supported_funcs) {
if (!FileExists(dep_path.c_str())) {
mand_depends_met = false;
}
} else if (ret == -2) {
} else if (ret <= -2) {
throw amd::smi::rsmi_exception(RSMI_STATUS_INTERNAL_EXCEPTION,
"Failed to parse monitor file name: " + dep_path);
}
Expand All @@ -450,7 +457,7 @@ void Monitor::fillSupportedFuncs(SupportedFuncMap *supported_funcs) {
mand_depends_met = false;
break;
}
} else if (ret == -2) {
} else if (ret <= -2) {
throw amd::smi::rsmi_exception(RSMI_STATUS_INTERNAL_EXCEPTION,
"Failed to parse monitor file name: " + dep_path);
}
Expand Down Expand Up @@ -479,6 +486,9 @@ void Monitor::fillSupportedFuncs(SupportedFuncMap *supported_funcs) {

if (ret == 0) {
supported_monitors = get_intersection(&sensors_i, &intersect);
} else if (ret <= -2) {
throw amd::smi::rsmi_exception(RSMI_STATUS_INTERNAL_EXCEPTION,
"Failed to parse monitor file name: " + dep_path);
}
} else {
supported_monitors = intersect;
Expand Down

0 comments on commit 27148a0

Please sign in to comment.