Skip to content

Commit c59e18c

Browse files
dumbbellpmjdebruijn
authored andcommitted
Fix handling of integer preference parameters
Before this commit, the code did: int max = G_MAXINT; /* INT_MAX, 2147483647 */ float factor = 1.0f; max *= factor; Dimitry Andric helped me understand the problem; here is the explanation: Here, "max * factor" is 2147483648.0, not 2147483647.0: the value is rounded up because the float type has a mantissa of 23 bits only. However, converting 2147483648.0 to an integer is an undefined behaviour. The resulting value depends on the compiler and the level of optimization: GCC (all versions) with -O0: max = -2147483648 GCC (all versions) with -O2: max = 2147483647 Clang up-to 3.5 with -O0: max = -2147483648 Clang up-to 3.5 with -O2: max = 2147483647 (ie. same behaviour as GCC) Clang 3.6+ with -O0: max = -2147483648 Clang 3.6+ with -O2: max = 0 In the context of the preferences dialog, this means that all integers must be between min=0 and max=0. The fix, suggested by Dimitry, is to use a double as an intermediate variable: it is wide enough to store "max * factor" without rounding up the value. Then, 2147483647.0 can be converted to 2147483647. (cherry picked from commit 9d77a28)
1 parent 6bc644e commit c59e18c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

tools/generate_prefs.xsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@
231231
<xsl:text> gint min = 0;&#xA; gint max = G_MAXINT;&#xA;</xsl:text>
232232
<xsl:apply-templates select="type" mode="range"/>
233233
<xsl:text> </xsl:text><xsl:apply-templates select="type" mode="factor"/>
234-
<xsl:text> min *= factor; max *= factor;
234+
<xsl:text> double tmp;
235+
tmp = min * (double)factor; min = tmp;
236+
tmp = max * (double)factor; max = tmp;
235237
widget = gtk_spin_button_new_with_range(min, max, 1);
236238
gtk_spin_button_set_digits(GTK_SPIN_BUTTON(widget), 0);
237239
gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), dt_conf_get_int("</xsl:text><xsl:value-of select="name"/><xsl:text>") * factor);

0 commit comments

Comments
 (0)