Skip to content

Electronics_Shop.cpp:89:12 #6845

@18781875724

Description

@18781875724

Issue: Potential Runtime Errors in Budget Shopping Calculator

Description

The budget shopping calculator has several potential runtime errors including possible segmentation faults, invalid memory access, and unhandled edge cases when processing input data.

Root Cause

  • No input validation for empty input strings
  • Potential out-of-bounds access in vector indexing
  • No error handling for malformed input data
  • Assumption that input will always be properly formatted

Affected Code

vector<string> split_string(string input_string) {
    // Potential issues with empty strings:
    while (input_string[input_string.length() - 1] == ' ') { // CRASH: access [-1] if empty
        input_string.pop_back();
    }
    // ... rest of splitting logic
}

int main() {
    // No validation before processing:
    vector<string> bnm = split_string(bnm_temp); // Could crash if bnm_temp empty
    int b = stoi(bnm[0]); // Could crash if bnm empty
    int n = stoi(bnm[1]); // Could crash if bnm size < 2
    int m = stoi(bnm[2]); // Could crash if bnm size < 3
    
    // Similar issues with keyboards_temp_temp and drives_temp_temp
    for (int keyboards_itr = 0; keyboards_itr < n; keyboards_itr++) {
        int keyboards_item = stoi(keyboards_temp[keyboards_itr]); // Could crash
        keyboards[keyboards_itr] = keyboards_item;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions