33 __attribute__((format(printf, 3, 4))) int
safe_snprintf(
char* buf,
43 return ((
unsigned int)value & (
unsigned int)flag) != 0;
54 bool safe_memcpy(
void* dest,
size_t dest_size,
const void* src,
size_t count);
59 bool safe_memset(
void* dest,
size_t dest_size,
int value,
size_t count);
68 void safe_strncpy(
char* dest,
size_t dest_size,
const char* src,
77 void safe_strncat(
char* dest,
size_t dest_size,
const char* src);
106 if (file_ptr && *file_ptr) {
107 (void)fclose(*file_ptr);
112 #define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
117 #ifdef __clang_analyzer__
118 void raii_satisfy_analyzer_file(FILE* file_ptr);
119 #define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
121 #define RAII_SATISFY_FILE(f) (void)0
129 void** ptr = (
void**)ptr_ptr;
136 #define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
141 #ifdef __clang_analyzer__
142 void raii_satisfy_analyzer_free(
void* ptr);
143 #define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
145 #define RAII_SATISFY_FREE(p) (void)0
154 #define TRANSFER_OWNERSHIP(ptr) \
156 __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:110
bool is_safe_relative_path(const char *path)
Validates a relative path to prevent arbitrary file access.
Definition: utils.c:129
static void cleanup_file(FILE **file_ptr)
RAII callback for FILE*.
Definition: utils.h:104
static void cleanup_free(void *ptr_ptr)
RAII callback for free().
Definition: utils.h:127
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:26
void safe_strncat(char *dest, size_t dest_size, const char *src)
Safe wrapper around strncat to ensure bounds safety.
Definition: utils.c:88
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:71
bool safe_memset(void *dest, size_t dest_size, int value, size_t count)
memset wrapper with bounds checking.
Definition: utils.c:61
void * safe_calloc(size_t num, size_t size)
calloc wrapper with zero-size check.
Definition: utils.c:43
static bool check_flag(int value, int flag)
Bitwise flag check helper.
Definition: utils.h:41
bool safe_memcpy(void *dest, size_t dest_size, const void *src, size_t count)
memcpy wrapper with bounds checking.
Definition: utils.c:51
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:153