public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/main] Cygwin: introduce constexpr errmap_size and errmap[] consistency checks
Date: Mon, 26 Feb 2024 15:47:02 +0000 (GMT)	[thread overview]
Message-ID: <20240226154702.E1637385840D@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b55eed943b85a72b76a0a2f5738dfb9aad1982ea

commit b55eed943b85a72b76a0a2f5738dfb9aad1982ea
Author:     Christian Franke <christian.franke@t-online.de>
AuthorDate: Mon Feb 26 13:37:33 2024 +0100
Commit:     Corinna Vinschen <corinna@vinschen.de>
CommitDate: Mon Feb 26 15:56:10 2024 +0100

    Cygwin: introduce constexpr errmap_size and errmap[] consistency checks
    
    Use constexpr instead of const for errmap[] to allow static_assert
    checks on its values.
    
    Signed-off-by: Christian Franke <christian.franke@t-online.de>

Diff:
---
 winsup/cygwin/errno.cc                |  2 +-
 winsup/cygwin/local_includes/errmap.h | 11 ++++++++++-
 2 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/errno.cc b/winsup/cygwin/errno.cc
index 1c85e9a04367..7d58e62ec142 100644
--- a/winsup/cygwin/errno.cc
+++ b/winsup/cygwin/errno.cc
@@ -185,7 +185,7 @@ geterrno_from_win_error (DWORD code, int deferrno)
 {
   /* A 0-value in errmap means, we don't handle this windows error
      explicitely.  Fall back to deferrno in these cases. */
-  if (code < sizeof errmap / sizeof errmap[0] && errmap[code])
+  if (code < errmap_size && errmap[code])
     {
       syscall_printf ("windows error %u == errno %d", code, errmap[code]);
       return errmap[code];
diff --git a/winsup/cygwin/local_includes/errmap.h b/winsup/cygwin/local_includes/errmap.h
index a0b3ff400644..737c01c8b869 100644
--- a/winsup/cygwin/local_includes/errmap.h
+++ b/winsup/cygwin/local_includes/errmap.h
@@ -3,7 +3,7 @@
    to this new array manually on demand. */
 
 /* FIXME: Some of these choices are arbitrary! */
-static const int errmap[] =
+constexpr int errmap[] =
 {
   0,			/* ERROR_SUCCESS */
   EBADRQC,		/* ERROR_INVALID_FUNCTION */
@@ -9006,3 +9006,12 @@ static const int errmap[] =
   0,			/* 8998 */
   0,			/* 8999 */
 };
+
+constexpr unsigned errmap_size = sizeof (errmap) / sizeof (errmap[0]);
+
+/* Some consistency checks. */
+static_assert (errmap_size == 8999 + 1);
+static_assert (EINTR == errmap[/* 104 */ ERROR_INVALID_AT_INTERRUPT_TIME]);
+static_assert (ENXIO == errmap[/* 1006 */ ERROR_FILE_INVALID]);
+static_assert (EAGAIN == errmap[/* 2404 */ ERROR_DEVICE_IN_USE]);
+static_assert (EIO == errmap[/* 8341 */ ERROR_DS_GENERIC_ERROR]);

                 reply	other threads:[~2024-02-26 15:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20240226154702.E1637385840D@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /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).