diff --git a/cthreads.c b/cthreads.c index dea3270..17f6c26 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) { +#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) { +#endif #ifdef CTHREADS_DEBUG puts("cthreads_error_string"); #endif @@ -474,7 +480,7 @@ size_t cthreads_error_string(int error_code, char *buf, size_t length) { /* 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 634691b..048c566 100644 --- a/cthreads.h +++ b/cthreads.h @@ -446,17 +446,28 @@ 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 +#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); +#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); +#endif #ifdef CTHREADS_SEMAPHORE /**