fix(style_configurator): fix unsafe int to nk_bool pointer casts#883
Conversation
|
In fact, fixing this bug is not a trivial task. There are two possible strategies:
|
That wasn't really my intent. I just wanted to fix this thing and move on. I only did it because I was investigating other issue related to nk_bool (#849) and found out this is the safest approach. This thing was working fine in the past and #185 broke it only because our CI can't really catch this (but maybe it should...) I tried to find other possible places affected by this and found none (explained in #812 (comment) )
These days, if you find yourself writing a code with similar defect (aka passing int* into bool*), a compiler with half-decent warnings enabled should catch this. In fact, GCC 15 does this by default and reports as error. Proof here: https://godbolt.org/z/4Pjncdjqn And if your compiler cannot do this, well... you're screwed anyway, because those kinds of issues can happen all over the C code.
I would rather not do this. Not only there are way more int fields with bool semantics like these two, there is simply no other use of nk_bool type anywhere in the style structs (EDIT: I think I found one somewhere). If you want these two changed, you should probably also change other similar fields elsewhere, and then go through each line that uses them. This in return will create even bigger mess and more bugs. |
Btw, I think I remember some people talking about serializing those structs. So there is some benefit of keeping them the same, and adding nk_bool would definitely break this use case. You can't really serialize bool safely, although serializing raw signed integer is not a good idea neither, it will still probably work on Win32 platforms. That might be why they haven't changed it back in #185 and why those were never changed from |
|
Overall, I think this makes sense. Making |
Nuklear changed a lot of stuff from integer types to nk_bool back in: Immediate-Mode-UI#185 This introduced an issue in demo/common/style_configurator.c where the int pointer would be passed into nk_checkbox_label() which expects nk_bool pointer instead, thus causing an unsafe cast. This change fixes this issue by using a reference of additional variable. Fixes: Immediate-Mode-UI#812
3eea8d6 to
f416ca9
Compare
|
@RobLoach latest force-push makes those casts explicit, just like you requested. |
|
@RobLoach As you can see, sleeptightAnsiC has already provided several arguments. At the moment, Nuklear contains several structures that use Line 5228 in 25f33c8 But in Line 5038 in 25f33c8 Line 5134 in 25f33c8 Regarding API/ABI breakage, I believe the impact would be limited, since Nuklear defines |
I believe, there are more issues like this one, not only in styling structs. I personally don't want to touch this (for all the reasons mentioned before). This can be fixed as another Issue/PR, but not this one. |
I may missing something, but I’m not entirely sure why this issue is being generalized to such a broad scope in the discussion. |
Those were the only ones that we had the code coverage to catch. If someone can write a new faulty code that casts int* to bool* , then this can be caused by literally anything. The fact that it happened here wasn't caused by someone being confused by the API, but because #185 forgot to change this code path. That's all.
Yes, you do: Line 5359 in 25f33c8 Line 4642 in 25f33c8 Line 5741 in 25f33c8 (and probably way more) @PavelSharp This PR is only about fixing those two specific code paths. Either take it or leave it. If you want the struct methods changed, go open a new PR/Issue for it. I'm not going to do this myself (you already know my opinion on this). |
|
I just entered a new issue about all those int fields #891 The conversation above is irrelevant here. This PR only fixes the two existing code paths. Nothing else. (also, if we would ever want to change those to nk_bool, the code in this PR will still be compatible with it) |
|
@RobLoach Fell free to either merge or reject this one. I will not be messing with those structs here. |
Fixes: #812
For more detailed explanation see the commit message or the linked issue. Also, if you're wondering if those are the only places affected by this issue, I really think there is nothing else left :)