Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix division by 0 in ImGui::PlotLines when values_count = 1 #2387

Closed
wants to merge 1 commit into from

Conversation

Lectem
Copy link

@Lectem Lectem commented Feb 27, 2019

If values_count == 1, then there is a division by zero in const float t_step = 1.0f / (float)res_w; as res_w can be 0 ( ImMin((int)graph_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0) == 1 - 1 == 0 )

This can be reproduced with the following snippet:

float values[1] = {0.f};
ImGui::PlotLines("Boom", values, 1);

If `values_count == 1`, then there is a division by zero in `const float t_step = 1.0f / (float)res_w;` as res_w can be 0 ( `ImMin((int)graph_size.x, values_count) + ((plot_type == ImGuiPlotType_Lines) ? -1 : 0) == 1 - 1 == 0` );

This can be reproduced with the following snippet:
```cpp
float values[1] = {0.f};
ImGui::PlotLines("Boom", values, 1);
```
@Lectem Lectem changed the title Fix division by 0 in ImGui::PlotEx when values_count = 1 Fix division by 0 in ImGui::PlotLines when values_count = 1 Feb 27, 2019
@Lectem
Copy link
Author

Lectem commented Feb 27, 2019

Fixed by 5e3a1de

@Lectem Lectem closed this Feb 27, 2019
@ocornut
Copy link
Owner

ocornut commented Feb 27, 2019

Thank you, fixed!
(I have fixed this with another patch now, since yours doesn't follow the coding guidelines of imgui)

I don't think the division by zero here was harmful in this context, does your compile/cpu setting actually break on division by zero?

@Lectem
Copy link
Author

Lectem commented Feb 27, 2019

I don't think the division by zero here was harmful in this context, does your compile/cpu setting actually break on division by zero?

Yes we have float exceptions enabled in our application!

ocornut added a commit that referenced this pull request Feb 27, 2019
@Lectem
Copy link
Author

Lectem commented Feb 27, 2019

since yours doesn't follow the coding guidelines of imgui

Just to know for the future, was the issue constructing the int from bool ("cast") ?
Thanks for the quick response by the way.

@ocornut
Copy link
Owner

ocornut commented Feb 27, 2019

  • We use C-style cast only.
  • Extra space between the two parenthesis
  • In this particular case, I feel that casting the bool resulting from a == into an integer and using it in an integer compare (which is not != 0) is a little obtuse, and best done via a temporary variable.

Thanks for asking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants