From e5d4f25d0536f1d5a11b8a3f01d9ed826b92b623 Mon Sep 17 00:00:00 2001 From: 0xfadead Date: Mon, 20 Jan 2025 13:24:47 -0500 Subject: [PATCH 1/7] Made `buf` a static array for `cthreads_error_string()` if the compiler supports it Making something a static array gives compiler warnings when the length of the array is smaller than the provided length. --- cthreads.c | 8 +++++++- cthreads.h | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cthreads.c b/cthreads.c index 8119612..3edfa9e 100644 --- a/cthreads.c +++ b/cthreads.c @@ -7,6 +7,8 @@ #include /* strerror(), strlen() */ #endif +#include + #include "cthreads.h" #ifdef _WIN32 @@ -463,7 +465,11 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m } #endif -size_t cthreads_error_string(int error_code, char *buf, size_t length) { +#if CTHREADS_STATIC_VLA == 1 +size_t cthreads_error_string(int error_code, size_t length, char buf[static length]) { +#else +size_t cthreads_error_string(int error_code, size_t length, char *buf) { +#endif #ifdef CTHREADS_DEBUG puts("cthreads_error_string"); #endif diff --git a/cthreads.h b/cthreads.h index 97bc29d..62bd26f 100644 --- a/cthreads.h +++ b/cthreads.h @@ -433,16 +433,27 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m int cthreads_error_code(void); #endif +#if ((defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)) && __STDC__ == 1 && __STDC_VERSION__ >= 199901L) \ + || (__STDC__ == 0 && __STDC_VERSION__ >= 201112L) +#define CTHREADS_STATIC_VLA 1 +#else +#define CTHREADS_STATIC_VLA 0 +#endif + /** * Obtains the error code and writes at most `length` * bytes of the associated message to `buf`. * - * @param error_code Platform-specific error code. (See: `cthreads_error_code()`) - * @param buf Buffer of `length` bytes and target of the error message + * @param error_code Platform-specific error code. @see \ref cthreads_error_code() * @param length Length of the provided buffer + * @param buf Buffer of at least `length` bytes and target of the error message * * @return Number of bytes required to print the message + NULL-terminator */ -size_t cthreads_error_string(int error_code, char *buf, size_t length); +#if CTHREADS_STATIC_VLA == 1 +size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); +#else +size_t cthreads_error_string(int error_code, size_t length, char* buf); +#endif #endif /* CTHREADS_H */ From d3d6f5ded81bd870e978bc6974bea265b83c6f00 Mon Sep 17 00:00:00 2001 From: Awildidiot <69577313+0xfadead@users.noreply.github.com> Date: Mon, 3 Feb 2025 22:02:23 -0500 Subject: [PATCH 2/7] Simplified macro logic for static VLA detection (1/2) Co-authored-by: Pedro.js Signed-off-by: Awildidiot <69577313+0xfadead@users.noreply.github.com> --- cthreads.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cthreads.h b/cthreads.h index a2c1ae7..084131d 100644 --- a/cthreads.h +++ b/cthreads.h @@ -448,7 +448,7 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m #if ((defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)) && __STDC__ == 1 && __STDC_VERSION__ >= 199901L) \ || (__STDC__ == 0 && __STDC_VERSION__ >= 201112L) -#define CTHREADS_STATIC_VLA 1 +#define CTHREADS_SUPPORTS_VLA #else #define CTHREADS_STATIC_VLA 0 #endif From 753e3a583b77de71dc4bf810dc70b712401d7250 Mon Sep 17 00:00:00 2001 From: 0xfadead Date: Mon, 3 Feb 2025 22:11:15 -0500 Subject: [PATCH 3/7] Simplified macro logic for static VLA detection (2/2) --- cthreads.c | 4 ++-- cthreads.h | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cthreads.c b/cthreads.c index 56dbe4c..7a9907f 100644 --- a/cthreads.c +++ b/cthreads.c @@ -465,7 +465,7 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m } #endif -#if CTHREADS_STATIC_VLA == 1 +#ifdef CTHREADS_SUPPORTS_VLA size_t cthreads_error_string(int error_code, size_t length, char buf[static length]) { #else size_t cthreads_error_string(int error_code, size_t length, char *buf) { @@ -480,7 +480,7 @@ size_t cthreads_error_string(int error_code, size_t length, char *buf) { /* INFO: The string that is written also contains a \n, which we must ignore, besides the NULL terminator. - */ + */Update cthreads.c const size_t error_str_len = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&error_str, 0, NULL) - 1 - 1; #else diff --git a/cthreads.h b/cthreads.h index 084131d..af7acf2 100644 --- a/cthreads.h +++ b/cthreads.h @@ -446,11 +446,11 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m int cthreads_error_code(void); #endif +/* Check for static vla support ( (is_compiler(gcc or clang or tcc) and complies_with(C99)) + * or complies_with(C11) Update cthreads.c) */ #if ((defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)) && __STDC__ == 1 && __STDC_VERSION__ >= 199901L) \ || (__STDC__ == 0 && __STDC_VERSION__ >= 201112L) #define CTHREADS_SUPPORTS_VLA -#else -#define CTHREADS_STATIC_VLA 0 #endif /** @@ -463,7 +463,7 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m * * @return Number of bytes required to print the message + NULL-terminator */ -#if CTHREADS_STATIC_VLA == 1 +#ifdef CTHREADS_SUPPORTS_VLA size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); #else size_t cthreads_error_string(int error_code, size_t length, char* buf); From 604e16662b4d6cb33478af64f13b034223a669ab Mon Sep 17 00:00:00 2001 From: Awildidiot <69577313+0xfadead@users.noreply.github.com> Date: Mon, 3 Feb 2025 22:13:22 -0500 Subject: [PATCH 4/7] Indent the `cthreads_error_string()` declaration Co-authored-by: Pedro.js Signed-off-by: Awildidiot <69577313+0xfadead@users.noreply.github.com> --- cthreads.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cthreads.c b/cthreads.c index 7a9907f..17f6c26 100644 --- a/cthreads.c +++ b/cthreads.c @@ -466,9 +466,9 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m #endif #ifdef CTHREADS_SUPPORTS_VLA -size_t cthreads_error_string(int error_code, size_t length, char buf[static length]) { + size_t cthreads_error_string(int error_code, size_t length, char buf[static length]) { #else -size_t cthreads_error_string(int error_code, size_t length, char *buf) { + size_t cthreads_error_string(int error_code, size_t length, char *buf) { #endif #ifdef CTHREADS_DEBUG puts("cthreads_error_string"); From e48110d51b2c75c010312187720184ed1382234c Mon Sep 17 00:00:00 2001 From: Awildidiot <69577313+0xfadead@users.noreply.github.com> Date: Mon, 3 Feb 2025 22:14:33 -0500 Subject: [PATCH 5/7] Indent the `cthreads_error_string()` definition Co-authored-by: Pedro.js Signed-off-by: Awildidiot <69577313+0xfadead@users.noreply.github.com> --- cthreads.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cthreads.h b/cthreads.h index af7acf2..8d1de24 100644 --- a/cthreads.h +++ b/cthreads.h @@ -464,9 +464,9 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m * @return Number of bytes required to print the message + NULL-terminator */ #ifdef CTHREADS_SUPPORTS_VLA -size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); + size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); #else -size_t cthreads_error_string(int error_code, size_t length, char* buf); + size_t cthreads_error_string(int error_code, size_t length, char *buf); #endif #ifdef CTHREADS_SEMAPHORE From 3c92819f28d067f8170e378adf3838339816186b Mon Sep 17 00:00:00 2001 From: Awildidiot <69577313+0xfadead@users.noreply.github.com> Date: Mon, 3 Feb 2025 22:14:33 -0500 Subject: [PATCH 6/7] Indent the `cthreads_error_string()` definition Co-authored-by: Pedro.js Signed-off-by: Awildidiot <69577313+0xfadead@users.noreply.github.com> --- cthreads.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cthreads.h b/cthreads.h index af7acf2..8d1de24 100644 --- a/cthreads.h +++ b/cthreads.h @@ -464,9 +464,9 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m * @return Number of bytes required to print the message + NULL-terminator */ #ifdef CTHREADS_SUPPORTS_VLA -size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); + size_t cthreads_error_string(int error_code, size_t length, char buf[static length]); #else -size_t cthreads_error_string(int error_code, size_t length, char* buf); + size_t cthreads_error_string(int error_code, size_t length, char *buf); #endif #ifdef CTHREADS_SEMAPHORE From f1051476506e962182c4a302e68a07ca39ccfec1 Mon Sep 17 00:00:00 2001 From: 0xfadead Date: Mon, 3 Feb 2025 22:27:45 -0500 Subject: [PATCH 7/7] Indented definition of `CTHREADS_SUPPORTS_VLA` --- cthreads.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cthreads.h b/cthreads.h index 8d1de24..048c566 100644 --- a/cthreads.h +++ b/cthreads.h @@ -450,7 +450,7 @@ int cthreads_cond_timedwait(struct cthreads_cond *cond, struct cthreads_mutex *m * or complies_with(C11) Update cthreads.c) */ #if ((defined(__GNUC__) || defined(__clang__) || defined(__TINYC__)) && __STDC__ == 1 && __STDC_VERSION__ >= 199901L) \ || (__STDC__ == 0 && __STDC_VERSION__ >= 201112L) -#define CTHREADS_SUPPORTS_VLA + #define CTHREADS_SUPPORTS_VLA #endif /**