bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] timespec: long-to-int truncation could make timespec_cmp malfunc


From: Jim Meyering
Subject: [PATCH] timespec: long-to-int truncation could make timespec_cmp malfunction
Date: Thu, 29 Oct 2009 11:55:28 +0100

I noticed a compile-time warning when trying to build faketime:

  ./lib/timespec.h: In function 'timespec_cmp':
  ./lib/timespec.h:30: error: conversion to 'int' from 'long int' may alter its 
value

While a tv_nsec value of 10^9 or larger may be unusual,
it is certainly possible, and must be accommodated.

>From 02a5d9ca15dc8e71fe8af9e76e732e01c90fc703 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Thu, 29 Oct 2009 11:52:34 +0100
Subject: [PATCH] timespec: long-to-int truncation could make timespec_cmp 
malfunction

* lib/timespec.h (timespec_cmp): Do not interpret a difference of
a multiple of 2^32 nanoseconds as no difference.
---
 ChangeLog      |    6 ++++++
 lib/timespec.h |    6 ++++--
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7982453..b2073ee 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2009-10-29  Jim Meyering  <address@hidden>
+
+       timespec: long-to-int truncation could make timespec_cmp malfunction
+       * lib/timespec.h (timespec_cmp): Do not interpret a difference of
+       a multiple of 2^32 nanoseconds as no difference.
+
 2009-10-28  Jim Meyering  <address@hidden>

        fprintftime: wrap macro code argument in "do {...} while(0)"
diff --git a/lib/timespec.h b/lib/timespec.h
index 3f51db8..db3453a 100644
--- a/lib/timespec.h
+++ b/lib/timespec.h
@@ -1,6 +1,6 @@
 /* timespec -- System time interface

-   Copyright (C) 2000, 2002, 2004, 2005, 2007 Free Software Foundation, Inc.
+   Copyright (C) 2000, 2002, 2004, 2005, 2007, 2009 Free Software Foundation, 
Inc.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,7 +27,9 @@ timespec_cmp (struct timespec a, struct timespec b)
 {
   return (a.tv_sec < b.tv_sec ? -1
          : a.tv_sec > b.tv_sec ? 1
-         : a.tv_nsec - b.tv_nsec);
+         : a.tv_nsec < b.tv_nsec ? -1
+         : a.tv_nsec > b.tv_nsec ? 1
+         : 0);
 }

 void gettime (struct timespec *);
--
1.6.5.2.375.g164f1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]