AngusJohnson/Image32

SetRoundMode should be used with try/finally

Closed this issue · 0 comments

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.