Bug 272723 - <sys/cdefs.h> header breaks restricted pointers in C++
Summary: <sys/cdefs.h> header breaks restricted pointers in C++
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-07-25 17:45 UTC by sebastian.huber
Modified: 2023-10-02 16:26 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description sebastian.huber 2023-07-25 17:45:43 UTC
Newlib shares large parts of <sys/cdefs.h> with FreeBSD and received this bug report:

https://sourceware.org/pipermail/newlib/2023/020400.html

As an extension, GCC and clang offer C99-style restricted pointers in C++ mode:
https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html

We notice that this extension is broken when including newlib headers: restricted pointers are treated as ordinary pointers.

We traced this to the following section of newlib/libc/include/sys/cdefs.h:

  /*
   * GCC 2.95 provides `__restrict' as an extension to C90 to support the
   * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
   * a way to define the `restrict' type qualifier without disturbing older
   * software that is unaware of C99 keywords.
   */
  #if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
  #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
  #define	__restrict
  #else
  #define	__restrict	restrict
  #endif
  #endif

While the GCC __restrict extension was indeed introduced in GCC 2.95, it is not limited to this version; the extension is also not limited to C90:
https://gcc.gnu.org/gcc-2.95/c++features.html

This is a suggestion how the logic in the header could be rewritten:

  /*
   * We use `__restrict' as a way to define the `restrict' type qualifier
   * without disturbing older software that is unaware of C99 keywords.
   * GCC also provides `__restrict' as an extension to support C99-style
   * restricted pointers in other language modes.
   */
  #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901
  #define	__restrict	restrict
  #elif !__GNUC_PREREQ__(2, 95)
  #define	__restrict
  #endif
Comment 1 commit-hook freebsd_committer freebsd_triage 2023-08-06 21:28:06 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=15876d9fd83fdfa7d541ea747eb40faeade975d2

commit 15876d9fd83fdfa7d541ea747eb40faeade975d2
Author:     Sebastian Huber <sebastian.huber@embedded-brains.de>
AuthorDate: 2023-08-06 13:27:27 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-06 21:09:52 +0000

    sys/cdefs.h: fix for use __restrict in C++

    Newlib shares large parts of <sys/cdefs.h> with FreeBSD and received
    this bug report:

    https://sourceware.org/pipermail/newlib/2023/020400.html

    As an extension, GCC and clang offer C99-style restricted pointers in
    C++ mode:
    https://gcc.gnu.org/onlinedocs/gcc/Restricted-Pointers.html

    We notice that this extension is broken when including newlib headers:
    restricted pointers are treated as ordinary pointers.

    We traced this to the following section of
    newlib/libc/include/sys/cdefs.h:

      /*
       * GCC 2.95 provides `__restrict' as an extension to C90 to support the
       * C99-specific `restrict' type qualifier.  We happen to use `__restrict' as
       * a way to define the `restrict' type qualifier without disturbing older
       * software that is unaware of C99 keywords.
       */
      #if !(__GNUC__ == 2 && __GNUC_MINOR__ == 95)
      #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901
      #define       __restrict
      #else
      #define       __restrict      restrict
      #endif
      #endif

    While the GCC __restrict extension was indeed introduced in GCC 2.95, it
    is not limited to this version; the extension is also not limited to
    C90:
    https://gcc.gnu.org/gcc-2.95/c++features.html

    Rewrite the logic in the header so that __restrict is kept alone when
    available.

    PR:     272723
    MFC after:      1 week

 sys/sys/cdefs.h | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
Comment 2 commit-hook freebsd_committer freebsd_triage 2023-08-13 04:38:48 UTC
A commit in branch stable/13 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=4e94219d55824b19693287052edd8a1da76e3541

commit 4e94219d55824b19693287052edd8a1da76e3541
Author:     Sebastian Huber <sebastian.huber@embedded-brains.de>
AuthorDate: 2023-08-06 13:27:27 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-08-13 04:35:41 +0000

    sys/cdefs.h: fix for use __restrict in C++

    PR:     272723

    (cherry picked from commit 15876d9fd83fdfa7d541ea747eb40faeade975d2)

 sys/sys/cdefs.h | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)