22__attribute__((format(printf, 3, 4))) int
safe_snprintf(
char* buf,
32 return ((
unsigned int)value & (
unsigned int)flag) != 0;
43bool safe_memcpy(
void* dest,
size_t dest_size,
const void* src,
size_t count);
48bool safe_memset(
void* dest,
size_t dest_size,
int value,
size_t count);
57void safe_strncpy(
char* dest,
size_t dest_size,
const char* src,
66void safe_strncat(
char* dest,
size_t dest_size,
const char* src);
95 if (file_ptr && *file_ptr) {
96 (void)fclose(*file_ptr);
101#define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
106#ifdef __clang_analyzer__
107void raii_satisfy_analyzer_file(FILE* file_ptr);
108#define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
110#define RAII_SATISFY_FILE(f) (void)0
118 void** ptr = (
void**)ptr_ptr;
125#define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
130#ifdef __clang_analyzer__
131void raii_satisfy_analyzer_free(
void* ptr);
132#define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
134#define RAII_SATISFY_FREE(p) (void)0
143#define TRANSFER_OWNERSHIP(ptr) \
145 __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:129
void * safe_calloc(size_t num, size_t size)
calloc wrapper with zero-size check.
Definition utils.c:46
bool is_safe_relative_path(const char *path)
Validates a relative path to prevent arbitrary file access.
Definition utils.c:148
static void cleanup_file(FILE **file_ptr)
RAII callback for FILE*.
Definition utils.h:93
static void cleanup_free(void *ptr_ptr)
RAII callback for free().
Definition utils.h:116
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:29
void safe_strncat(char *dest, size_t dest_size, const char *src)
Safe wrapper around strncat to ensure bounds safety.
Definition utils.c:107
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:87
bool safe_memset(void *dest, size_t dest_size, int value, size_t count)
memset wrapper with bounds checking.
Definition utils.c:72
static bool check_flag(int value, int flag)
Bitwise flag check helper.
Definition utils.h:30
bool safe_memcpy(void *dest, size_t dest_size, const void *src, size_t count)
memcpy wrapper with bounds checking.
Definition utils.c:54