Skip to content

Commit

Permalink
Fix corner case
Browse files Browse the repository at this point in the history
In contains_substring_ignoring_case method, if the string that we are
looking for is longer than the source string, return with string not
found.  Earlier the char array index went out of bounds due to invalid
arthimetic operation on the index.

Tested and found the method working as expected.

Signed-off-by: deepakala-k <deepakala.karthikeyan@ibm.com>
  • Loading branch information
deepakala-k committed Jul 23, 2024
1 parent f429a97 commit 2e5ead4
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions libpdbg/dtb.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ static const uint16_t ODYSSEY_CHIP_ID = 0x60C0;
static const uint8_t ATTR_TYPE_OCMB_CHIP = 75;
static const char* RAINIER = "rainier";
static const char* EVEREST = "everest";
static const char* FUJI = "fuji";
static const char* BLUERIDGE = "blueridge";

static struct pdbg_dtb pdbg_dtb = {
.backend = {
Expand Down Expand Up @@ -116,6 +118,13 @@ bool contains_substring_ignoring_case(const char* buffer, const char* substring)
size_t bufferLen = strlen(buffer);
size_t substringLen = strlen(substring);

//If the string that we are looking for is longer than the
//given string, return as not found
if(substringLen > bufferLen)
{
return false;
}

for (size_t i = 0; i <= bufferLen - substringLen; ++i) {
size_t j;
for (j = 0; j < substringLen; ++j) {
Expand Down Expand Up @@ -319,10 +328,12 @@ static void bmc_target(struct pdbg_dtb *dtb)
pdbg_proc = PDBG_PROC_P10;
if (!dtb->backend.fdt) {
char *system_type = get_p10_system_type();
if (contains_substring_ignoring_case(system_type, EVEREST)) {
if (contains_substring_ignoring_case(system_type, EVEREST) ||
contains_substring_ignoring_case(system_type, FUJI)) {
pdbg_log(PDBG_INFO, "bmc_target - loading bmc kernel everest target");
dtb->backend.fdt = &_binary_bmc_kernel_everest_dtb_o_start;
} else if (contains_substring_ignoring_case(system_type, RAINIER)) {
} else if (contains_substring_ignoring_case(system_type, RAINIER)||
contains_substring_ignoring_case(system_type, BLUERIDGE)) {
pdbg_log(PDBG_INFO, "bmc_target - loading bmc kernel rainier target");
dtb->backend.fdt = &_binary_bmc_kernel_rainier_dtb_o_start;
} else {
Expand Down Expand Up @@ -350,10 +361,12 @@ static void bmc_target(struct pdbg_dtb *dtb)
pdbg_proc = PDBG_PROC_P10;
if (!dtb->backend.fdt) {
char *system_type = get_p10_system_type();
if (contains_substring_ignoring_case(system_type, EVEREST)) {
if (contains_substring_ignoring_case(system_type, EVEREST) ||
contains_substring_ignoring_case(system_type, FUJI)) {
pdbg_log(PDBG_INFO, "bmc_target - loading bmc kernel everest target");
dtb->backend.fdt = &_binary_bmc_kernel_everest_dtb_o_start;
} else if (contains_substring_ignoring_case(system_type, RAINIER)) {
} else if (contains_substring_ignoring_case(system_type, RAINIER) ||
contains_substring_ignoring_case(system_type, BLUERIDGE)) {
pdbg_log(PDBG_INFO, "bmc_target - loading bmc kernel rainier target");
dtb->backend.fdt = &_binary_bmc_kernel_rainier_dtb_o_start;
} else {
Expand Down Expand Up @@ -406,11 +419,13 @@ static void sbefifo_target(struct pdbg_dtb *dtb)
pdbg_proc = PDBG_PROC_P10;
if (!dtb->backend.fdt) {
char *system_type = get_p10_system_type();
if (contains_substring_ignoring_case(system_type, EVEREST)) {
if (contains_substring_ignoring_case(system_type, EVEREST) ||
contains_substring_ignoring_case(system_type, FUJI)) {
pdbg_log(PDBG_INFO,
"sbefifo_target - loading bmc sbefifo everest target");
dtb->backend.fdt = &_binary_bmc_sbefifo_everest_dtb_o_start;
} else if (contains_substring_ignoring_case(system_type, RAINIER)) {
} else if (contains_substring_ignoring_case(system_type, RAINIER) ||
contains_substring_ignoring_case(system_type, BLUERIDGE)) {
pdbg_log(PDBG_INFO,
"sbefifo_target - loading bmc sbefifo rainier target");
dtb->backend.fdt = &_binary_bmc_sbefifo_rainier_dtb_o_start;
Expand Down Expand Up @@ -439,11 +454,13 @@ static void sbefifo_target(struct pdbg_dtb *dtb)
pdbg_proc = PDBG_PROC_P10;
if (!dtb->backend.fdt) {
char *system_type = get_p10_system_type();
if (contains_substring_ignoring_case(system_type, EVEREST)) {
if (contains_substring_ignoring_case(system_type, EVEREST) ||
contains_substring_ignoring_case(system_type, FUJI)) {
pdbg_log(PDBG_INFO,
"sbefifo_target - loading bmc sbefifo everest target");
dtb->backend.fdt = &_binary_bmc_sbefifo_everest_dtb_o_start;
} else if (contains_substring_ignoring_case(system_type, RAINIER)) {
} else if (contains_substring_ignoring_case(system_type, RAINIER) ||
contains_substring_ignoring_case(system_type, BLUERIDGE)) {
pdbg_log(PDBG_INFO,
"sbefifo_target - loading bmc sbefifo rainier target");
dtb->backend.fdt = &_binary_bmc_sbefifo_rainier_dtb_o_start;
Expand Down

0 comments on commit 2e5ead4

Please sign in to comment.