fix(crop): prevent division by zero in margin calc - #66
Conversation
| #include "koptcrop.h" | ||
|
|
||
| float min(float a, float b) { | ||
| static float k2_min(float a, float b) { |
There was a problem hiding this comment.
Is there a reason we shouldn't we just use fminf() (from math.h) instead? The output seems to be identical at a quick glance.
Without the -ffast-math it just says branch to fminf which annoyingly doesn't tell me much about the internals. (I suppose I could check glibc etc.) According to the manpage NaN behavior may be different but in a way that sounds better.
These functions return the minimum of x and y. If one argument is a NaN, the other argument is returned. If both arguments are NaN, a NaN is returned.
There was a problem hiding this comment.
Personally, I don't see any reason for it, but since this function is there, I don't want to delete it. The only difference between the results of the two functions is that k2_min returns 0.0 and fminf returns -0.0 when the boundary is -0.0 < 0.0, but I’ve looked through the code snippets and the parameters a + b are always greater than or equal to 0, so I don’t think there’s any difference.
There was a problem hiding this comment.
I don't think that argument quite works if you're touching all the lines.
Btw @benoit-pierre I noticed something funny if it bothers you or something:
Lines 26 to 27 in 1fcffeb
|
Would you mind squashing the last two commits together? |
1eed128 to
dfd4082
Compare
yeah done |
Change 1: In
lib/koptcrop.c:92, the margin calculation computes:kctx->dev_width <= margin(e.g. very small device width or very large margin setting), the denominator becomes zero or negative, causing undefined behavior (floating-point division by zero).Change 2: The file defines a global
float min(float, float)atlib/koptcrop.c:32. This name collides with commonminmacros defined in system headers and other libraries, causing potential compilation issues or silent behavior changes depending on include order.This change is