public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Lukasz Majewski <lukma@denx.de>
To: Joseph Myers <joseph@codesourcery.com>, Paul Eggert <eggert@cs.ucla.edu>
Cc: Alistair Francis <alistair23@gmail.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Alistair Francis <alistair.francis@wdc.com>,
	GNU C Library <libc-alpha@sourceware.org>,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	Florian Weimer <fweimer@redhat.com>,
	Carlos O'Donell <carlos@redhat.com>,
	Stepan Golosunov <stepan@golosunov.pp.ru>,
	Florian Weimer <fw@deneb.enyo.de>,
	Zack Weinberg <zackw@panix.com>, Lukasz Majewski <lukma@denx.de>
Subject: [PATCH 1/2] y2038: Helper macro to convert struct __timespec64 to struct timespec
Date: Fri, 18 Oct 2019 14:57:00 -0000	[thread overview]
Message-ID: <20191018145720.11706-1-lukma@denx.de> (raw)

This macro allows conversion between Y2038 safe struct __timespec64 and
struct timespec.
The struct __timespec64's tv_nsec field is checked if it is in the correct
range.
Moreover, the tv_sec is asserted if it fits into the time_t range.
When error is detected the errno is set accordingly and the function, which
uses this macro returns -1.

Tested with scripts/build-many-glibcs.py script.
---
 include/time.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/include/time.h b/include/time.h
index d93b16a781..c2b6c9b842 100644
--- a/include/time.h
+++ b/include/time.h
@@ -236,5 +236,28 @@ valid_timespec64_to_timeval (const struct __timespec64 ts64)
 
   return tv;
 }
+
+/* Check if a value lies with the valid nanoseconds range.  */
+#define IS_VALID_NANOSECONDS(ns) ((ns) >= 0 && (ns) <= 999999999)
+
+/* Check and convert a struct __timespec64 into a struct timespec.
+   This macro checks if the valid number of nanoseconds has been provided
+   by ts64 and if not the errno is set to EINVAL and -1 is returned.
+   Moreover, the number of seconds is check as well, if it is in the time_t
+   range. If not the errno is set to EOVERFLOW and -1 is returned.  */
+#define timespec64_to_timespec(ts64)                                           \
+  ({                                                                           \
+    if (! IS_VALID_NANOSECONDS (ts64.tv_nsec))                                 \
+      {                                                                        \
+        __set_errno (EINVAL);                                                  \
+        return -1;                                                             \
+      }                                                                        \
+    if (! in_time_t_range (ts64.tv_sec))                                       \
+      {                                                                        \
+        __set_errno (EOVERFLOW);                                               \
+        return -1;                                                             \
+      }                                                                        \
+    valid_timespec64_to_timespec (ts64); })
+
 #endif
 #endif
-- 
2.20.1

             reply	other threads:[~2019-10-18 14:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-18 14:57 Lukasz Majewski [this message]
2019-10-18 14:57 ` [PATCH 2/2] y2038: linux: Provide __clock_getres64 implementation Lukasz Majewski
2019-10-18 15:38   ` Joseph Myers
2019-10-19  6:16     ` Lukasz Majewski
2019-10-18 17:36 ` [PATCH 1/2] y2038: Helper macro to convert struct __timespec64 to struct timespec Paul Eggert
2019-10-18 20:44   ` Lukasz Majewski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191018145720.11706-1-lukma@denx.de \
    --to=lukma@denx.de \
    --cc=adhemerval.zanella@linaro.org \
    --cc=alistair.francis@wdc.com \
    --cc=alistair23@gmail.com \
    --cc=arnd@arndb.de \
    --cc=carlos@redhat.com \
    --cc=eggert@cs.ucla.edu \
    --cc=fw@deneb.enyo.de \
    --cc=fweimer@redhat.com \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=stepan@golosunov.pp.ru \
    --cc=zackw@panix.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).