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

SetRoundMode should be used with try/finally #41

Closed
tweibert opened this issue Dec 14, 2023 · 0 comments
Closed

SetRoundMode should be used with try/finally #41

tweibert opened this issue Dec 14, 2023 · 0 comments

Comments

@tweibert
Copy link

Hello,

the Rasterize procedure in Img32.Draw alters the FPU rounding mode by calling SetRoundMode(rmDown):

savedRoundMode := SetRoundMode(rmDown);

At the end of the procedure, the rounding mode is set back to the original value. However, there may be conditions where the procedure exits prematurely, in particular when a zero-width rect was passed (for whatever reason), but also in case of an exception.

In this case, code execution never reaches the call which resets the rounding mode, and it is left in its rmDown state, which may lead to unexpected behavior in other parts of the application which use Delphi's Round() functions etc.

I think this issue can be solved easily by wrapping the entire code between the two SetRoundMode() calls into a try..finally..end block:

savedRoundMode := SetRoundMode(rmDown);
try
  // rest of the code
finally
  SetRoundMode(savedRoundMode);
end;

Let me know if you need a PR for this.

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

No branches or pull requests

2 participants