Fix bad uses of sizeof(char[])/sizeof(char*)#906
Conversation
|
Tagged @robertbartel and @christophertubbs as reviewers, as they introduced the offending code. |
|
Good find. I have a few thoughts about it (criticisms of my original code, really) that I want to run by you that might improve it more.
I'm obviously not the expert on this, so I assume you'll know the nuts and bolts of it better. |
|
Good find @PhilMiller. I tend to think "yes" regarding @christophertubbs's questions 1 and 2 above. Regarding question 3, eh, maybe. I do think we should (more fully) abstract this logic out to a single reusable utility function somewhere. I feel like I was probably starting to do this by adding the Also, even if the above isn't done, clearly some additional tests are needed, at minimum to ensure |
…xt `sizeof(pattern.c_str()) - 2` where `pattern == "{{id}}"`
a8c3dfa to
c3855d0
Compare
|
I've addressed Chris's points 1 and 2. I think 3 is out of scope of this change, whose goal is to replace code that happens to work, but isn't really 'correct'. |
0b11fa3 to
620d4c4
Compare
|
This'll be rebased on #955, after finding that the impacted functionality was not covered by tests, and was in fact broken. |
In both cases, we're ultimately getting the length
6of the literal string"{{id}}", without'\0'terminatorIn the case where the literal string is passed to
sizeof, it's obtusely correct, because that evaluates the literalchar[], which takes serious language-lawyering to recognize. It's also very fragile to array-to-pointer-decay if someone attempts to refactor the code in some way, which would leave it over by one.In the case where
std::string patternis a function parameter and the code sayssizeof(pattern.c_string()) - 2, it's only managed to operate correctly because all of the current call sites pass an argument of"{{id}}", and it's only tested on 64-bit systems.Changes
sizeof(<string literal>)withstd::string::size()to be more durably correctTodos
Checklist