[Asterisk-code-review] Use doubles instead of floats for conversions when comparing... (asterisk[13.9])

Mark Michelson asteriskteam at digium.com
Thu May 12 15:24:46 CDT 2016


Mark Michelson has uploaded a new change for review.

  https://gerrit.asterisk.org/2823

Change subject: Use doubles instead of floats for conversions when comparing strings.
......................................................................

Use doubles instead of floats for conversions when comparing strings.

In 13.9.0, there was an issue where PJSIP contacts added to an AOR would
be deleted at seemingly random times.

One reason this was happening was because of an operation to retrieve
the contacts whose expiration time was less than or equal to the current
time. When retrieving existing contacts, the contact's expiration time
and the current time were converted from a string to a float, and those
two floats were compared.

On some systems, including mine, this conversion was horribly off. For
instance, I could regularly see the string "1463079214" get converted
into 1463079168.000000. When switching from using a float to using a
double, the conversion was as expected.

Why was the conversion to float off? My best guess is that the
conversion to float was attempting to store the entire value in the 23
bit significand of the IEEE-754 floating point number. In particular, if
you take only the 23 most significant bits of 1463079214, you get the
messed up 1463079168 that we were seeing in the conversion. It likely
was possible to get a more precise value by composing the number using
an exponent, but the conversion did not work that way. With a double,
you have a 52 bit significand, allowing the entire value to fit there,
and thereby allowing an accurate conversion.

ASTERISK-26007 #close
Reported by Greg Siemon

Change-Id: I83ca7944aae8b7cd994b254c78ec02411d321070
---
M main/strings.c
1 file changed, 3 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/23/2823/1

diff --git a/main/strings.c b/main/strings.c
index 9e885eb..db78a6c 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -234,8 +234,8 @@
 {
 	char *internal_op = (char *)op;
 	char *internal_right = (char *)right;
-	float left_num;
-	float right_num;
+	double left_num;
+	double right_num;
 	int scan_numeric = 0;
 
 	if (!(left && right)) {
@@ -297,7 +297,7 @@
 	}
 
 equals:
-	scan_numeric = (sscanf(left, "%f", &left_num) && sscanf(internal_right, "%f", &right_num));
+	scan_numeric = (sscanf(left, "%lf", &left_num) && sscanf(internal_right, "%lf", &right_num));
 
 	if (internal_op[0] == '=') {
 		if (ast_strlen_zero(left) && ast_strlen_zero(internal_right)) {

-- 
To view, visit https://gerrit.asterisk.org/2823
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I83ca7944aae8b7cd994b254c78ec02411d321070
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13.9
Gerrit-Owner: Mark Michelson <mmichelson at digium.com>



More information about the asterisk-code-review mailing list