Assigning the value of a float to an unsigned int is undefined
behavior unless the value is guaranteed to fit. We run afoul of this
in compute_grad_points(), where the number of steps is computed using
a floating point calculation.
On a RISC-V / musl system, the end result is that we wind up with
steps = the maximum value of an unsigned int, when really this should
be an error case resulting in steps = 1. The test suite catches this.
*/
#include <assert.h>
+#include <limits.h>
#include <math.h>
#include <string.h>
#include <stdio.h>
if(isnan(r0) || isnan(r1)) {
steps = 1;
} else {
- steps = ceilf(fabsf(r1 - r0) / 0.05);
+ /* float -> int assignment is undefined unless
+ * the value is guaranteed to fit */
+ float stepsf = ceilf(fabsf(r1 - r0) / 0.05);
+ if (isnan(stepsf)) {
+ steps = 1;
+ }
+ else if (stepsf > (float)UINT_MAX) {
+ steps = 1;
+ }
+ else {
+ steps = stepsf;
+ }
}
if (steps == 0)