From 947daa02b0b64131626c2ecedb74ca6893aab6c6 Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Mon, 26 Feb 2024 13:37:33 +0100 Subject: [PATCH 1/4] 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 --- 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 1c85e9a04..7d58e62ec 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 a0b3ff400..737c01c8b 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]); -- 2.43.0