Skip to content
10 changes: 8 additions & 2 deletions cthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string.h> /* strerror(), strlen() */
#endif

#include <time.h>

#include "cthreads.h"

#ifdef _WIN32
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 14 additions & 3 deletions cthreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
/**
Expand Down