summaryrefslogtreecommitdiff
path: root/main/strings.c
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2016-05-12 14:36:25 -0500
committerMark Michelson <mmichelson@digium.com>2016-05-12 15:24:33 -0500
commitfd3f70598d193d10bed997eaaea128a7bdbb2143 (patch)
tree24842418361aa738399bcbdb1a03bfa81c7274d3 /main/strings.c
parentdd354009d3c64499640a0297703f5619e2cd5007 (diff)
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
Diffstat (limited to 'main/strings.c')
-rw-r--r--main/strings.c6
1 files changed, 3 insertions, 3 deletions
diff --git a/main/strings.c b/main/strings.c
index f828727ad..b9c88dea5 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -234,8 +234,8 @@ int ast_strings_match(const char *left, const char *op, const char *right)
{
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 @@ regex:
}
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)) {