Bug 231425 - Fix C11 and POSIX 1003.1b-1993 compliance in time.h
Summary: Fix C11 and POSIX 1003.1b-1993 compliance in time.h
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Brooks Davis
URL:
Keywords: patch
Depends on:
Blocks:
 
Reported: 2018-09-17 11:11 UTC by Tamas Szakaly
Modified: 2018-09-18 15:44 UTC (History)
3 users (show)

See Also:


Attachments
patch to make time.h C11 and POSIX 1003.1b-1993 compliant (953 bytes, patch)
2018-09-17 11:11 UTC, Tamas Szakaly
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tamas Szakaly 2018-09-17 11:11:32 UTC
Created attachment 197167 [details]
patch to make time.h C11 and POSIX 1003.1b-1993 compliant

The problems:
 - struct timespec is only visible if _POSIX_C_SOURCE >= 199309 even though it is in the C11 standard (ISO/IEC 9899:201x 7.27.1)
 - timespec_get is not behind any feature test macros, but it is only defined in C11 (ISO/IEC 9899:201x 7.27.2.5)

Related headers:
 - time.h: includes timespec.h if _POSIX_C_SOURCE >= 19930, defines timespec_get and TIME_UTC unconditionally
 - timespec.h: defines some BSD-specific macros, itimerspec, and includes _timespec.h
 - _timespec.h: defines struct timespec

A summary of which symbols are defined in which standard:
 - itimerspec: POSIX 1003.1b-1993
 - timespec: POSIX 1003.1b-1993, C11
 - timespec_get: C11
 - TIME_UTC: C11

The patch:
I did not really know what would be the proper approach to fix this, so I went with a solution that does not require touching _timespec.h and timespec.h. The attached patch puts timespec_get and TIME_UTC behind __STDC_VERSION__ >= 201112L guard. It also includes _timespec.h inside that guard, so we get struct timespec.
Comment 1 Konstantin Belousov freebsd_committer freebsd_triage 2018-09-17 11:20:30 UTC
The .c part of the patch is certainly bogus.

For the header, see https://reviews.freebsd.org/D17174 .  I think that your suggestion to make timespec visible to C11 consumers is correct, but still this is not a final patch.
Comment 2 Tamas Szakaly 2018-09-17 11:44:26 UTC
(In reply to Konstantin Belousov from comment #1)
Ah, sorry, I've tried searching for this, but somehow missed the Phabricator issue. Thanks for the link!
Comment 3 Brooks Davis freebsd_committer freebsd_triage 2018-09-17 20:59:01 UTC
I've updated D17174 to use __ISO_C_VISIBLE, include sys/_timespec.h, and use __BSD_VISABLE.  I agree with kib that the change to libc is wrong and we should always provide the symbol.
Comment 4 Brooks Davis freebsd_committer freebsd_triage 2018-09-17 21:00:24 UTC
I'll handle this since I've got a review open.
Comment 5 commit-hook freebsd_committer freebsd_triage 2018-09-18 15:32:00 UTC
A commit references this bug:

Author: brooks
Date: Tue Sep 18 15:31:24 UTC 2018
New revision: 338751
URL: https://svnweb.freebsd.org/changeset/base/338751

Log:
  Fix C11 and POSIX 1003.1b-1993 compliance in time.h

  Only expose timespec_get in C11, C++17, or BSD code.  Always define
  struct timespect if defining timespec_get.

  PR:		231425
  Reviewed by:	kib
  Approved by:	re (gjb)
  Differential Revision:	https://reviews.freebsd.org/D17174

Changes:
  head/include/time.h