30 __attribute__((format(printf, 3, 4))) int
safe_snprintf(
char* buf,
40 return ((
unsigned int)value & (
unsigned int)flag) != 0;
51 bool safe_memcpy(
void* dest,
size_t dest_size,
const void* src,
size_t count);
56 bool safe_memset(
void* dest,
size_t dest_size,
int value,
size_t count);
65 void safe_strncpy(
char* dest,
size_t dest_size,
const char* src,
74 void safe_strncat(
char* dest,
size_t dest_size,
const char* src);
103 if (file_ptr && *file_ptr) {
104 (void)fclose(*file_ptr);
109 #define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
114 #ifdef __clang_analyzer__
115 void raii_satisfy_analyzer_file(FILE* file_ptr);
116 #define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
118 #define RAII_SATISFY_FILE(f) (void)0
126 void** ptr = (
void**)ptr_ptr;
133 #define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
138 #ifdef __clang_analyzer__
139 void raii_satisfy_analyzer_free(
void* ptr);
140 #define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
142 #define RAII_SATISFY_FREE(p) (void)0
151 #define TRANSFER_OWNERSHIP(ptr) \
153 __typeof__(ptr) _tmp_ptr = (ptr); \
bool is_safe_filename(const char *filename)
Validates a filename to prevent path traversal and shell injection.
Definition: utils.c:112
bool is_safe_relative_path(const char *path)
Validates a relative path to prevent arbitrary file access.
Definition: utils.c:131
static void cleanup_file(FILE **file_ptr)
RAII callback for FILE*.
Definition: utils.h:101
static void cleanup_free(void *ptr_ptr)
RAII callback for free().
Definition: utils.h:124
int safe_snprintf(char *buf, size_t buf_size, const char *format,...)
Safe wrapper around vsnprintf to format strings with bounds checking.
Definition: utils.c:28
void safe_strncat(char *dest, size_t dest_size, const char *src)
Safe wrapper around strncat to ensure bounds safety.
Definition: utils.c:90
void safe_strncpy(char *dest, size_t dest_size, const char *src, size_t src_size)
Safe wrapper around strncpy to ensure null-termination.
Definition: utils.c:73
bool safe_memset(void *dest, size_t dest_size, int value, size_t count)
memset wrapper with bounds checking.
Definition: utils.c:63
void * safe_calloc(size_t num, size_t size)
calloc wrapper with zero-size check.
Definition: utils.c:45
static bool check_flag(int value, int flag)
Bitwise flag check helper.
Definition: utils.h:38
bool safe_memcpy(void *dest, size_t dest_size, const void *src, size_t count)
memcpy wrapper with bounds checking.
Definition: utils.c:53
void * utils_buffer_offset(size_t offset)
Helper to securely cast an integer offset to a pointer, often used for VBO/EBO byte offsets.
Definition: utils.c:155