Skip to content

Commit

Permalink
Ensure string is non-empty before calling stoul or stoi
Browse files Browse the repository at this point in the history
Change-Id: I2c6314fb86d3bba8fd6aab932dbb989263fa8542
  • Loading branch information
Chris Freehill authored and Chris Freehill committed Jan 28, 2020
1 parent d00b9ac commit f748868
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/rocm_smi.cc
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,14 @@ static uint64_t get_multiplier_from_str(char units_char) {
* "<int index>: <int freq><freq. unit string> <|*>"
*/
static uint64_t freq_string_to_int(const std::vector<std::string> &freq_lines,
bool *is_curr, uint32_t lanes[], int i) {
bool *is_curr, uint32_t lanes[], uint32_t i) {

assert(i < freq_lines.size());
if (i >= freq_lines.size()) {
throw amd::smi::rsmi_exception(RSMI_STATUS_INPUT_OUT_OF_BOUNDS,
__FUNCTION__);
}

std::istringstream fs(freq_lines[i]);

uint32_t ind;
Expand Down Expand Up @@ -238,6 +245,10 @@ static uint64_t freq_string_to_int(const std::vector<std::string> &freq_lines,
if (star_str[0] == 'x') {
assert(lanes != nullptr && "Lanes are provided but null lanes pointer");
if (lanes) {
if (star_str.substr(1) == "") {
throw amd::smi::rsmi_exception(RSMI_STATUS_NO_DATA, __FUNCTION__);
}

lanes[i] = std::stoi(star_str.substr(1), nullptr);
}
}
Expand Down Expand Up @@ -417,6 +428,9 @@ static rsmi_status_t get_dev_mon_value(amd::smi::MonitorTypes type,
return errno_to_rsmi_status(ret);
}

if (val_str == "") {
return RSMI_STATUS_NO_DATA;
}
*val = std::stoi(val_str);

return RSMI_STATUS_SUCCESS;
Expand Down Expand Up @@ -445,6 +459,10 @@ static rsmi_status_t get_dev_mon_value(amd::smi::MonitorTypes type,
}
assert(amd::smi::IsInteger(val_str));

if (val_str == "") {
return RSMI_STATUS_NO_DATA;
}

*val = std::stoul(val_str);

return RSMI_STATUS_SUCCESS;
Expand Down Expand Up @@ -1400,6 +1418,11 @@ get_id_name_str_from_line(uint64_t id, std::string ln,
THROW_IF_NULLPTR_DEREF(ln_str)

*ln_str >> token1;

if (token1 == "") {
throw amd::smi::rsmi_exception(RSMI_STATUS_NO_DATA, __FUNCTION__);
}

if (std::stoul(token1, nullptr, 16) == id) {
int64_t pos = ln_str->tellg();

Expand Down
10 changes: 10 additions & 0 deletions src/rocm_smi_device.cc
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ int Device::readDevInfo(DevInfoTypes type, uint64_t *val) {
case kDevErrCntFeatures:
ret = readDevInfoStr(type, &tempStr);
RET_IF_NONZERO(ret);

if (tempStr == "") {
return EINVAL;
}
*val = std::stoi(tempStr, 0, 16);
break;

Expand All @@ -681,6 +685,9 @@ int Device::readDevInfo(DevInfoTypes type, uint64_t *val) {
case kDevXGMIError:
ret = readDevInfoStr(type, &tempStr);
RET_IF_NONZERO(ret);
if (tempStr == "") {
return EINVAL;
}
*val = std::stoul(tempStr, 0);
break;

Expand Down Expand Up @@ -708,6 +715,9 @@ int Device::readDevInfo(DevInfoTypes type, uint64_t *val) {
case kDevFwVersionVcn:
ret = readDevInfoStr(type, &tempStr);
RET_IF_NONZERO(ret);
if (tempStr == "") {
return EINVAL;
}
*val = std::stoul(tempStr, 0, 16);
break;

Expand Down
7 changes: 7 additions & 0 deletions src/rocm_smi_kfd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ int GetProcessInfo(rsmi_process_info_t *procs, uint32_t num_allocated,
return err;
}
assert(is_number(tmp) && "Unexpected value in pasid file");
if (!is_number(tmp)) {
return EINVAL;
}
procs[*num_procs_found].pasid = std::stoi(tmp);
}
++(*num_procs_found);
Expand Down Expand Up @@ -196,6 +199,10 @@ int GetProcessInfoForPID(uint32_t pid, rsmi_process_info_t *proc) {
return err;
}
assert(is_number(tmp) && "Unexpected value in pasid file");

if (!is_number(tmp)) {
return EINVAL;
}
proc->pasid = std::stoi(tmp);

return 0;
Expand Down

0 comments on commit f748868

Please sign in to comment.