Repository navigation
Fix #13167 FP invalidLifetime when constructing string from c_str() - #7231
Conversation
| if (it == scope->varlist.cend()) | ||
| return; | ||
| const Variable& var = *it; | ||
| if (var.valueType() && var.valueType()->container && var.valueType()->container->stdStringLike && !var.valueType()->container->view) |
There was a problem hiding this comment.
I don't think you need to check stdLikeString. A non-view container should always have ownership.
Looking at this closer it seems like this check should happen in the isLifetimeBorrowed function, but it probably doesn't get the parentValueType correctly.
There was a problem hiding this comment.
A string object will never own anything external*. As you noticed, isLifetimeBorrowed() doesn't even take a var argument.
*Unless it is a reference. But we already have a FN for that, because it doesn't involve a member function call and wouldn't hit the code path above.
https://trac.cppcheck.net/ticket/12407
There was a problem hiding this comment.
This looks very closely related to this FN:
https://trac.cppcheck.net/ticket/10885
I dont know if this fixes that issue or not.
Ideally we should check this in isLifetimeBorrowed function since this is a central place to check all the ownership/borrowing rules, but that looks to be a more complicated refactor.
This change is probably fine for now but probably should add a TODO comment(perhaps with a reference to this PR or discussion), so we don't end up duplicating a lot of logic here in the future.
No description provided.