aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2023-01-17 17:01:05 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2023-01-17 17:01:05 +0100
commitc344ca6c7f5d8468624ae518d2912d1d9612cb91 (patch)
treebb8e3940c8914fc3ddacc7b7b571545fe7cfc233
parent85acf71d2579ebe4eec05c6f31901adffa700adc (diff)
downloadbusybox-w32-c344ca6c7f5d8468624ae518d2912d1d9612cb91.tar.gz
busybox-w32-c344ca6c7f5d8468624ae518d2912d1d9612cb91.tar.bz2
busybox-w32-c344ca6c7f5d8468624ae518d2912d1d9612cb91.zip
ntpd: correct fixed->float conversions of fractions
Need to divide by (1<<32), not by (1<<32)-1. Fraction of 0xffffffff is not 1 whole second. function old new delta .rodata 105264 105268 +4 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--networking/ntpd.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/networking/ntpd.c b/networking/ntpd.c
index 4365166ff..ff49550e9 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -564,7 +564,7 @@ lfp_to_d(l_fixedpt_t lfp)
564 double ret; 564 double ret;
565 lfp.int_partl = ntohl(lfp.int_partl); 565 lfp.int_partl = ntohl(lfp.int_partl);
566 lfp.fractionl = ntohl(lfp.fractionl); 566 lfp.fractionl = ntohl(lfp.fractionl);
567 ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX); 567 ret = (double)lfp.int_partl + ((double)lfp.fractionl / (1ULL << 32));
568 /* 568 /*
569 * Shift timestamps before 1970 to the second NTP era (2036-2106): 569 * Shift timestamps before 1970 to the second NTP era (2036-2106):
570 * int_partl value of OFFSET_1900_1970 (2208988800) is interpreted as 570 * int_partl value of OFFSET_1900_1970 (2208988800) is interpreted as
@@ -581,7 +581,7 @@ sfp_to_d(s_fixedpt_t sfp)
581 double ret; 581 double ret;
582 sfp.int_parts = ntohs(sfp.int_parts); 582 sfp.int_parts = ntohs(sfp.int_parts);
583 sfp.fractions = ntohs(sfp.fractions); 583 sfp.fractions = ntohs(sfp.fractions);
584 ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX); 584 ret = (double)sfp.int_parts + ((double)sfp.fractions / (1 << 16));
585 return ret; 585 return ret;
586} 586}
587#if ENABLE_FEATURE_NTPD_SERVER 587#if ENABLE_FEATURE_NTPD_SERVER
@@ -591,7 +591,7 @@ d_to_lfp(l_fixedpt_t *lfp, double d)
591 uint32_t intl; 591 uint32_t intl;
592 uint32_t frac; 592 uint32_t frac;
593 intl = (uint32_t)(time_t)d; 593 intl = (uint32_t)(time_t)d;
594 frac = (uint32_t)((d - (time_t)d) * UINT_MAX); 594 frac = (uint32_t)((d - (time_t)d) * 0xffffffff);
595 lfp->int_partl = htonl(intl); 595 lfp->int_partl = htonl(intl);
596 lfp->fractionl = htonl(frac); 596 lfp->fractionl = htonl(frac);
597} 597}
@@ -601,7 +601,7 @@ d_to_sfp(s_fixedpt_t *sfp, double d)
601 uint16_t ints; 601 uint16_t ints;
602 uint16_t frac; 602 uint16_t frac;
603 ints = (uint16_t)d; 603 ints = (uint16_t)d;
604 frac = (uint16_t)((d - ints) * USHRT_MAX); 604 frac = (uint16_t)((d - ints) * 0xffff);
605 sfp->int_parts = htons(ints); 605 sfp->int_parts = htons(ints);
606 sfp->fractions = htons(frac); 606 sfp->fractions = htons(frac);
607} 607}