Issue: Segmentation Fault on Empty Input in Expression Tree Implementation
Description
The expression tree implementation crashes with segmentation fault when an empty string is provided as input. The code attempts to access exp[len-1] without checking if the input string is empty.
Root Cause
- No input validation for empty strings in constructor
- Direct access to
exp[len-1] where len = 0 causes out-of-bounds access
- Stack operations proceed with invalid nodes when input is empty
Affected Code
exp_tree(string postfix_exp)
{
this->postfix_exp = postfix_exp;
root = NULL;
create_tree(postfix_exp); // Called without validation
}
void create_tree(string exp)
{
int len = exp.length();
root = new node(exp[len-1]); // CRASH: len-1 = -1 when empty
s.push(root);
// ... continues processing without checks
}
Issue: Segmentation Fault on Empty Input in Expression Tree Implementation
Description
The expression tree implementation crashes with segmentation fault when an empty string is provided as input. The code attempts to access
exp[len-1]without checking if the input string is empty.Root Cause
exp[len-1]wherelen = 0causes out-of-bounds accessAffected Code