Commit 77f23a5b authored by Kai Vehmanen's avatar Kai Vehmanen

Fixed bug in su_time_cmp() and SU_TIME_CMP(). If the difference of sec or usec...

Fixed bug in su_time_cmp() and SU_TIME_CMP(). If the difference of sec or usec fields was more than LONG_MAX, the routines returned incorrect results due to the ulong-to-long conversion.

darcs-hash:20061020204338-7659e-15fe07c7f55b37fb4ce76222e49f79480ce75fc2.gz
parent dd015d94
......@@ -76,10 +76,7 @@ typedef uint64_t su_ntp_t;
/** Represent NTP consttant */
#define SU_NTP_C(x) SU_U64_C(x)
#define SU_TIME_CMP(t1, t2) \
(long)((t1.tv_sec - t2.tv_sec != 0) ? \
(t1.tv_sec - t2.tv_sec) : \
(t1.tv_usec - t2.tv_usec))
#define SU_TIME_CMP(t1, t2) su_time_cmp(t1, t2)
SOFIAPUBFUN su_time_t su_now(void);
SOFIAPUBFUN void su_time(su_time_t *tv);
......
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
* Copyright (C) 2005,2006 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
......@@ -29,6 +29,7 @@
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
* @author Jari Selin <Jari.Selin@nokia.com>
* @author Kai Vehmanen <first.surname@nokia.com>
*
* @date Created: Thu Mar 18 19:40:51 1999 pessi
*/
......@@ -77,11 +78,19 @@
*/
long su_time_cmp(su_time_t const t1, su_time_t const t2)
{
long retval = t1.tv_sec - t2.tv_sec;
long retval = 0;
if (t1.tv_sec > t2.tv_sec)
retval = 1;
else if (t1.tv_sec < t2.tv_sec)
retval = -1;
else {
if (t1.tv_usec > t2.tv_usec)
retval = 1;
else if (t1.tv_usec < t2.tv_usec)
retval = -1;
}
if (retval == 0)
retval = t1.tv_usec - t2.tv_usec;
return retval;
}
......
/*
* This file is part of the Sofia-SIP package
*
* Copyright (C) 2005 Nokia Corporation.
* Copyright (C) 2005,2006 Nokia Corporation.
*
* Contact: Pekka Pessi <pekka.pessi@nokia.com>
*
......@@ -29,6 +29,7 @@
* Tests for su_time functions.
*
* @author Pekka Pessi <Pekka.Pessi@nokia.com>
* @author Kai Vehmanen <first.surname@nokia.com>
*
* @date Created: Fri May 10 16:08:18 2002 ppessi
*/
......@@ -38,6 +39,7 @@
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>
#include <sofia-sip/su_time.h>
......@@ -191,6 +193,25 @@ int test2(int flags)
int test3(int flags)
{
su_time_t a = { ULONG_MAX , ULONG_MAX };
su_time_t b = { 0 , 0 };
su_time_t c = { ULONG_MAX , ULONG_MAX - 1 };
BEGIN();
TEST_1(su_time_cmp(a, b) > 0);
TEST_1(su_time_cmp(a, a) == 0);
TEST_1(su_time_cmp(b, a) < 0);
TEST_1(su_time_cmp(a, c) > 0);
TEST_1(su_time_cmp(c, c) == 0);
TEST_1(su_time_cmp(c, a) < 0);
TEST_1(SU_TIME_CMP(a, b) > 0);
TEST_1(SU_TIME_CMP(a, a) == 0);
TEST_1(SU_TIME_CMP(b, a) < 0);
TEST_1(SU_TIME_CMP(a, c) > 0);
TEST_1(SU_TIME_CMP(c, c) == 0);
TEST_1(SU_TIME_CMP(c, a) < 0);
END();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment