fix: improve code consistency and warning settings across multiple files

This commit is contained in:
MechSlayer 2025-09-11 07:01:02 +02:00
parent 4c4eb7e620
commit 2ed55205d3
14 changed files with 131 additions and 162 deletions

View file

@ -70,6 +70,19 @@ if (UNIX)
endif ()
endif ()
target_compile_options(${MODULE_NAME} PRIVATE
-Wall -Wextra -Werror
-Wunused-value
-Wno-reserved-macro-identifier
-Wno-reserved-identifier
-Wno-unused-macros
-Wno-unused-variable
-Wno-undef
-Wno-gnu-statement-expression-from-macro-expansion
-Wno-unsafe-buffer-usage
-Wno-declaration-after-statement
-Wno-switch-default
)
set_target_properties(${MODULE_NAME} PROPERTIES LINKER_LANGUAGE C)
target_include_directories(${MODULE_NAME}

View file

@ -1,7 +1,7 @@
#pragma once
#include "platform.h"
#include <stdint.h>
#include <stddef.h>
#include <stdint.h>
DRANG_BEGIN_DECLS
@ -11,10 +11,8 @@ typedef struct drang_uuid
uint64_t low;
} drang_uuid_t;
DRANG_PLATFORM_API int drang_uuid_generate(drang_uuid_t* out_uuid);
DRANG_PLATFORM_API int drang_uuid_to_string(const drang_uuid_t* uuid, char* out_str, size_t str_size);
DRANG_PLATFORM_API int drang_uuid_from_string(const char* str, size_t str_size, drang_uuid_t* out_uuid);
DRANG_PLATFORM_API int drang_uuid_generate(drang_uuid_t *out_uuid);
DRANG_PLATFORM_API int drang_uuid_to_string(const drang_uuid_t *uuid, char *out_str, size_t str_size);
DRANG_PLATFORM_API int drang_uuid_from_string(const char *str, size_t str_size, drang_uuid_t *out_uuid);
DRANG_END_DECLS

View file

@ -5,9 +5,9 @@
#include <drang/fs.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
int drang_fs_mkdir(const char *path, drang_fs_permissions_t permissions)
{
@ -130,7 +130,7 @@ int drang_fs_get_cwd(char *buffer, size_t size, size_t *out_size)
{
DRANG_BEGIN_TRY()
DRANG_CHECK((buffer != NULL && size > 0) || (buffer == NULL && size == 0), DRANG_EINVAL);
char* dir = get_current_dir_name();
char *dir = get_current_dir_name();
if (dir != NULL) {
const size_t dir_length = strlen(dir);
if (buffer != NULL && size < dir_length + 1) {

View file

@ -7,14 +7,14 @@ int drang_uuid_generate(drang_uuid_t *out_uuid)
uuid_generate_random(linux_uuid);
// Convert to our format (UUID is stored as big-endian bytes)
out_uuid->high = ((uint64_t)linux_uuid[0] << 56) | ((uint64_t)linux_uuid[1] << 48) |
((uint64_t)linux_uuid[2] << 40) | ((uint64_t)linux_uuid[3] << 32) |
((uint64_t)linux_uuid[4] << 24) | ((uint64_t)linux_uuid[5] << 16) |
((uint64_t)linux_uuid[6] << 8) | ((uint64_t)linux_uuid[7]);
out_uuid->high = ((uint64_t) linux_uuid[0] << 56) | ((uint64_t) linux_uuid[1] << 48)
| ((uint64_t) linux_uuid[2] << 40) | ((uint64_t) linux_uuid[3] << 32)
| ((uint64_t) linux_uuid[4] << 24) | ((uint64_t) linux_uuid[5] << 16)
| ((uint64_t) linux_uuid[6] << 8) | ((uint64_t) linux_uuid[7]);
out_uuid->low = ((uint64_t)linux_uuid[8] << 56) | ((uint64_t)linux_uuid[9] << 48) |
((uint64_t)linux_uuid[10] << 40) | ((uint64_t)linux_uuid[11] << 32) |
((uint64_t)linux_uuid[12] << 24) | ((uint64_t)linux_uuid[13] << 16) |
((uint64_t)linux_uuid[14] << 8) | ((uint64_t)linux_uuid[15]);
out_uuid->low = ((uint64_t) linux_uuid[8] << 56) | ((uint64_t) linux_uuid[9] << 48)
| ((uint64_t) linux_uuid[10] << 40) | ((uint64_t) linux_uuid[11] << 32)
| ((uint64_t) linux_uuid[12] << 24) | ((uint64_t) linux_uuid[13] << 16)
| ((uint64_t) linux_uuid[14] << 8) | ((uint64_t) linux_uuid[15]);
return DRANG_EOK;
}
}

View file

@ -5,7 +5,7 @@
int drang_cond_new(struct drang_cond **cond)
{
struct drang_cond *c = NULL;
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
@ -21,7 +21,7 @@ int drang_cond_new(struct drang_cond **cond)
drang_free(c);
}
}
DRANG_END_TRY();
DRANG_END_TRY()
}
void drang_cond_free(struct drang_cond *cond)

View file

@ -5,7 +5,7 @@
int drang_mutex_new(struct drang_mutex **mutex)
{
struct drang_mutex *m = NULL;
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(mutex != NULL, DRANG_EINVAL);

View file

@ -5,7 +5,7 @@
int drang_rwlock_new(struct drang_rwlock **rwlock)
{
struct drang_rwlock *rw = NULL;
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
@ -21,7 +21,7 @@ int drang_rwlock_new(struct drang_rwlock **rwlock)
drang_free(rw);
}
}
DRANG_END_TRY();
DRANG_END_TRY()
}
void drang_rwlock_free(struct drang_rwlock *rwlock)

View file

@ -1,5 +1,5 @@
#include "drang/error.h"
#include <ctype.h>
#include <drang/uuid.h>
#include <stdio.h>
int drang_uuid_to_string(const drang_uuid_t *uuid, char *out_str, size_t str_size)
@ -7,136 +7,97 @@ int drang_uuid_to_string(const drang_uuid_t *uuid, char *out_str, size_t str_siz
uint8_t bytes[16];
// Convert back to bytes for string formatting
for (int i = 0; i < 8; i++) {
bytes[i] = (uint8_t)(uuid->high >> (56 - i * 8));
bytes[i + 8] = (uint8_t)(uuid->low >> (56 - i * 8));
bytes[i] = (uint8_t) (uuid->high >> (56 - i * 8));
bytes[i + 8] = (uint8_t) (uuid->low >> (56 - i * 8));
}
return snprintf(out_str, str_size, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15]);
return snprintf(out_str,
str_size,
"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
bytes[0],
bytes[1],
bytes[2],
bytes[3],
bytes[4],
bytes[5],
bytes[6],
bytes[7],
bytes[8],
bytes[9],
bytes[10],
bytes[11],
bytes[12],
bytes[13],
bytes[14],
bytes[15]);
}
static int hex_char_to_value(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'a' && c <= 'f') return c - 'a' + 10;
if (c >= 'A' && c <= 'F') return c - 'A' + 10;
return -1;
}
static int parse_hex_byte(const char *str, uint8_t *byte) {
int high = hex_char_to_value(str[0]);
int low = hex_char_to_value(str[1]);
if (high < 0 || low < 0) {
return -1;
static int hex_char_to_value(char c, int *out)
{
DRANG_BEGIN_TRY()
if (c >= '0' && c <= '9') {
DRANG_RETURN_IN(out, c - '0');
}
if (c >= 'a' && c <= 'f') {
DRANG_RETURN_IN(out, c - 'a' + 10);
}
if (c >= 'A' && c <= 'F') {
DRANG_RETURN_IN(out, c - 'A' + 10);
}
DRANG_FAIL(DRANG_EINVAL);
DRANG_END_TRY_IGNORE()
}
*byte = (uint8_t)((high << 4) | low);
return 0;
static int parse_hex_byte(const char *str, uint8_t *byte)
{
DRANG_BEGIN_TRY()
int high = 0;
int low = 0;
DRANG_TRY(hex_char_to_value(str[0], &high));
DRANG_TRY(hex_char_to_value(str[1], &low));
DRANG_RETURN_IN(byte, (uint8_t) ((high << 4) | low));
DRANG_END_TRY_IGNORE()
}
int drang_uuid_from_string(const char *str, size_t str_size, drang_uuid_t *out_uuid)
{
DRANG_BEGIN_TRY()
DRANG_CHECK(str, DRANG_EINVAL);
DRANG_CHECK(out_uuid, DRANG_EINVAL);
DRANG_CHECK(str != NULL, DRANG_EINVAL);
DRANG_CHECK(out_uuid != NULL, DRANG_EINVAL);
// Work with a copy to handle different formats
char normalized[37] = {0}; // 36 chars + null terminator
const char *src = str;
char *dst = normalized;
int hex_count = 0;
// Expected format: "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (36 characters)
DRANG_CHECK(str_size >= 36, DRANG_ENOMEM);
// Skip leading brace if present
if (*src == '{') {
src++;
}
// Check for proper hyphen positions
DRANG_CHECK(str[8] == '-' && str[13] == '-' && str[18] == '-' && str[23] == '-', DRANG_EINVAL);
// Copy only hex digits and hyphens, convert to lowercase
while (*src && dst < normalized + 36) {
if (isxdigit(*src)) {
*dst++ = (char)tolower(*src);
hex_count++;
} else if (*src == '-') {
*dst++ = '-';
} else if (*src == '}' && *(src + 1) == '\0') {
// Allow trailing brace
break;
} else if (!isspace(*src)) {
// Invalid character (ignoring whitespace)
DRANG_FAIL(DRANG_EINVAL);
}
src++;
}
// Must have exactly 32 hex digits
DRANG_CHECK(hex_count == 32, DRANG_EINVAL);
// Determine format and validate
const int len = (int)(dst - normalized);
int has_hyphens = 0;
if (len == 36) {
// Standard format with hyphens: 8-4-4-4-12
if (normalized[8] != '-' || normalized[13] != '-' ||
normalized[18] != '-' || normalized[23] != '-') {
DRANG_FAIL(DRANG_EINVAL);
}
has_hyphens = 1;
} else if (len == 32) {
// No hyphens format
has_hyphens = 0;
} else {
DRANG_FAIL(DRANG_EINVAL);
}
// Parse the hex digits into bytes
uint8_t bytes[16];
const char *hex_positions[16];
int byte_idx = 0;
if (has_hyphens) {
// Standard format positions
hex_positions[0] = &normalized[0]; // 8 chars: time_low
hex_positions[1] = &normalized[2];
hex_positions[2] = &normalized[4];
hex_positions[3] = &normalized[6];
hex_positions[4] = &normalized[9]; // 4 chars: time_mid
hex_positions[5] = &normalized[11];
hex_positions[6] = &normalized[14]; // 4 chars: time_hi_and_version
hex_positions[7] = &normalized[16];
hex_positions[8] = &normalized[19]; // 4 chars: clock_seq_and_reserved + clock_seq_low
hex_positions[9] = &normalized[21];
hex_positions[10] = &normalized[24]; // 12 chars: node
hex_positions[11] = &normalized[26];
hex_positions[12] = &normalized[28];
hex_positions[13] = &normalized[30];
hex_positions[14] = &normalized[32];
hex_positions[15] = &normalized[34];
} else {
// No hyphens format
for (int i = 0; i < 16; i++) {
hex_positions[i] = &normalized[i * 2];
// Parse each hex pair, skipping hyphens
for (int i = 0; i < 36 && byte_idx < 16; i += 2) {
// Skip hyphens
if (str[i] == '-') {
i -= 1; // Compensate for the loop increment
continue;
}
DRANG_TRY(parse_hex_byte(&str[i], &bytes[byte_idx]));
byte_idx++;
}
// Convert hex pairs to bytes
for (int i = 0; i < 16; i++) {
DRANG_CHECK(parse_hex_byte(hex_positions[i], &bytes[i]) == 0, DRANG_EINVAL);
// Ensure we parsed exactly 16 bytes
DRANG_CHECK(byte_idx == 16, DRANG_EINVAL);
// Convert bytes to high and low uint64_t values
out_uuid->high = 0;
out_uuid->low = 0;
for (int i = 0; i < 8; i++) {
out_uuid->high |= ((uint64_t) bytes[i]) << (56 - i * 8);
out_uuid->low |= ((uint64_t) bytes[i + 8]) << (56 - i * 8);
}
// Convert bytes to uint64_t values
out_uuid->high = ((uint64_t)bytes[0] << 56) | ((uint64_t)bytes[1] << 48) |
((uint64_t)bytes[2] << 40) | ((uint64_t)bytes[3] << 32) |
((uint64_t)bytes[4] << 24) | ((uint64_t)bytes[5] << 16) |
((uint64_t)bytes[6] << 8) | ((uint64_t)bytes[7]);
out_uuid->low = ((uint64_t)bytes[8] << 56) | ((uint64_t)bytes[9] << 48) |
((uint64_t)bytes[10] << 40) | ((uint64_t)bytes[11] << 32) |
((uint64_t)bytes[12] << 24) | ((uint64_t)bytes[13] << 16) |
((uint64_t)bytes[14] << 8) | ((uint64_t)bytes[15]);
DRANG_END_TRY_IGNORE()
}

View file

@ -357,11 +357,8 @@ bool drang_fs_exists(const char *path)
if (path == NULL)
return false;
const DWORD attrs = GetFileAttributesA(path);
if (attrs == INVALID_FILE_ATTRIBUTES) {
false;
}
return true;
return attrs != INVALID_FILE_ATTRIBUTES;
}
int drang_fs_stat(const char *path, struct drang_fs_stat *out_stat)

View file

@ -15,7 +15,7 @@ size_t drang_cond_alignment(void)
int drang_cond_init(struct drang_cond *cond)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
DRANG_CHECK(!cond->initialized, DRANG_EBUSY);
@ -23,7 +23,7 @@ int drang_cond_init(struct drang_cond *cond)
InitializeConditionVariable(&cond->cv);
cond->initialized = true;
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
void drang_cond_fini(struct drang_cond *cond)
@ -38,26 +38,26 @@ void drang_cond_fini(struct drang_cond *cond)
int drang_cond_signal(struct drang_cond *cond)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
WakeConditionVariable(&cond->cv);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_cond_broadcast(struct drang_cond *cond)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
WakeAllConditionVariable(&cond->cv);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_cond_wait(struct drang_cond *cond, struct drang_mutex *mutex)
@ -70,7 +70,7 @@ int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uin
DWORD timeout = (timeout_ms == 0) ? INFINITE : (DWORD) timeout_ms;
BOOL result;
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(cond != NULL, DRANG_EINVAL);
DRANG_CHECK(cond->initialized, DRANG_EINVAL);
@ -85,5 +85,5 @@ int drang_cond_timedwait(struct drang_cond *cond, struct drang_mutex *mutex, uin
DRANG_FAIL(DRANG_EINVAL); // Generic error for other failures
}
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}

View file

@ -1,8 +1,8 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdbool.h>
#include <windows.h>
struct drang_mutex
{

View file

@ -14,7 +14,7 @@ size_t drang_mutex_alignment(void)
int drang_mutex_init(struct drang_mutex *mutex)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(mutex != NULL, DRANG_EINVAL);
DRANG_CHECK(!mutex->initialized, DRANG_EBUSY);
@ -22,7 +22,7 @@ int drang_mutex_init(struct drang_mutex *mutex)
InitializeCriticalSection(&mutex->cs);
mutex->initialized = true;
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
void drang_mutex_fini(struct drang_mutex *mutex)
@ -37,19 +37,19 @@ void drang_mutex_fini(struct drang_mutex *mutex)
int drang_mutex_lock(struct drang_mutex *mutex)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(mutex != NULL, DRANG_EINVAL);
DRANG_CHECK(mutex->initialized, DRANG_EINVAL);
EnterCriticalSection(&mutex->cs);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_mutex_unlock(struct drang_mutex *mutex)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(mutex != NULL, DRANG_EINVAL);
DRANG_CHECK(mutex->initialized, DRANG_EINVAL);
@ -58,5 +58,5 @@ int drang_mutex_unlock(struct drang_mutex *mutex)
DRANG_RETURN();
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}

View file

@ -14,7 +14,7 @@ size_t drang_rwlock_alignment(void)
int drang_rwlock_init(struct drang_rwlock *rwlock)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
DRANG_CHECK(!rwlock->initialized, DRANG_EBUSY);
@ -22,7 +22,7 @@ int drang_rwlock_init(struct drang_rwlock *rwlock)
InitializeSRWLock(&rwlock->srw);
rwlock->initialized = true;
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
void drang_rwlock_fini(struct drang_rwlock *rwlock)
@ -37,48 +37,48 @@ void drang_rwlock_fini(struct drang_rwlock *rwlock)
int drang_rwlock_rdlock(struct drang_rwlock *rwlock)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
DRANG_CHECK(rwlock->initialized, DRANG_EINVAL);
AcquireSRWLockShared(&rwlock->srw);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_rwlock_wrlock(struct drang_rwlock *rwlock)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
DRANG_CHECK(rwlock->initialized, DRANG_EINVAL);
AcquireSRWLockExclusive(&rwlock->srw);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_rwlock_rdunlock(struct drang_rwlock *rwlock)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
DRANG_CHECK(rwlock->initialized, DRANG_EINVAL);
ReleaseSRWLockShared(&rwlock->srw);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}
int drang_rwlock_wrunlock(struct drang_rwlock *rwlock)
{
DRANG_BEGIN_TRY();
DRANG_BEGIN_TRY()
DRANG_CHECK(rwlock != NULL, DRANG_EINVAL);
DRANG_CHECK(rwlock->initialized, DRANG_EINVAL);
ReleaseSRWLockExclusive(&rwlock->srw);
DRANG_END_TRY_IGNORE();
DRANG_END_TRY_IGNORE()
}

View file

@ -13,7 +13,7 @@ int drang_uuid_generate(drang_uuid_t *out_uuid)
}
// Convert Windows UUID to our format
// Windows UUID is stored in mixed-endian format
out_uuid->high = ((uint64_t)win_uuid.Data1 << 32) | ((uint64_t)win_uuid.Data2 << 16) | win_uuid.Data3;
out_uuid->high = ((uint64_t) win_uuid.Data1 << 32) | ((uint64_t) win_uuid.Data2 << 16) | win_uuid.Data3;
out_uuid->low = 0;
for (int i = 0; i < 8; ++i) {
out_uuid->low = (out_uuid->low << 8) | win_uuid.Data4[i];