Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
6#ifndef UTILS_H
7#define UTILS_H
8
9#include <stdbool.h>
10#include <stddef.h>
11#include <stdio.h>
12#include <stdlib.h>
13
22__attribute__((format(printf, 3, 4))) int safe_snprintf(char* buf,
23 size_t buf_size,
24 const char* format,
25 ...);
26
30static inline bool check_flag(int value, int flag)
31{
32 return ((unsigned int)value & (unsigned int)flag) != 0;
33}
34
38void* safe_calloc(size_t num, size_t size);
39
43bool safe_memcpy(void* dest, size_t dest_size, const void* src, size_t count);
44
48bool safe_memset(void* dest, size_t dest_size, int value, size_t count);
49
57void safe_strncpy(char* dest, size_t dest_size, const char* src,
58 size_t src_size);
59
66void safe_strncat(char* dest, size_t dest_size, const char* src);
67
77bool is_safe_filename(const char* filename);
78
88bool is_safe_relative_path(const char* path);
89
93static inline void cleanup_file(FILE** file_ptr)
94{
95 if (file_ptr && *file_ptr) {
96 (void)fclose(*file_ptr);
97 }
98}
99
101#define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
102
106#ifdef __clang_analyzer__
107void raii_satisfy_analyzer_file(FILE* file_ptr);
108#define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
109#else
110#define RAII_SATISFY_FILE(f) (void)0
111#endif
112
116static inline void cleanup_free(void* ptr_ptr)
117{
118 void** ptr = (void**)ptr_ptr;
119 if (ptr && *ptr) {
120 free(*ptr);
121 }
122}
123
125#define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
126
130#ifdef __clang_analyzer__
131void raii_satisfy_analyzer_free(void* ptr);
132#define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
133#else
134#define RAII_SATISFY_FREE(p) (void)0
135#endif
136
143#define TRANSFER_OWNERSHIP(ptr) \
144 ({ \
145 __typeof__(ptr) _tmp_ptr = (ptr); \
146 (ptr) = 0; \
147 _tmp_ptr; \
148 })
149
150#endif /* UTILS_H */
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