From patchwork Fri Oct 18 14:57:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 35145 Received: (qmail 105784 invoked by alias); 18 Oct 2019 14:57:46 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Delivered-To: mailing list libc-alpha@sourceware.org Received: (qmail 105776 invoked by uid 89); 18 Oct 2019 14:57:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-19.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 spammy=Moreover X-HELO: mail-out.m-online.net From: Lukasz Majewski To: Joseph Myers , Paul Eggert Cc: Alistair Francis , Arnd Bergmann , Alistair Francis , GNU C Library , Adhemerval Zanella , Florian Weimer , Carlos O'Donell , Stepan Golosunov , Florian Weimer , Zack Weinberg , Lukasz Majewski Subject: [PATCH 1/2] y2038: Helper macro to convert struct __timespec64 to struct timespec Date: Fri, 18 Oct 2019 16:57:19 +0200 Message-Id: <20191018145720.11706-1-lukma@denx.de> MIME-Version: 1.0 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