$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
utils.h
Go to the documentation of this file.
1 
6 #ifndef UTILS_H
7 #define UTILS_H
8 
9 #include <stdarg.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 
23 void* utils_buffer_offset(size_t offset);
24 
33 __attribute__((format(printf, 3, 4))) int safe_snprintf(char* buf,
34  size_t buf_size,
35  const char* format,
36  ...);
37 
41 static inline bool check_flag(int value, int flag)
42 {
43  return ((unsigned int)value & (unsigned int)flag) != 0;
44 }
45 
49 void* safe_calloc(size_t num, size_t size);
50 
54 bool safe_memcpy(void* dest, size_t dest_size, const void* src, size_t count);
55 
59 bool safe_memset(void* dest, size_t dest_size, int value, size_t count);
60 
68 void safe_strncpy(char* dest, size_t dest_size, const char* src,
69  size_t src_size);
70 
77 void safe_strncat(char* dest, size_t dest_size, const char* src);
78 
88 bool is_safe_filename(const char* filename);
89 
99 bool is_safe_relative_path(const char* path);
100 
104 static inline void cleanup_file(FILE** file_ptr)
105 {
106  if (file_ptr && *file_ptr) {
107  (void)fclose(*file_ptr);
108  }
109 }
110 
112 #define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
113 
117 #ifdef __clang_analyzer__
118 void raii_satisfy_analyzer_file(FILE* file_ptr);
119 #define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
120 #else
121 #define RAII_SATISFY_FILE(f) (void)0
122 #endif
123 
127 static inline void cleanup_free(void* ptr_ptr)
128 {
129  void** ptr = (void**)ptr_ptr;
130  if (ptr && *ptr) {
131  free(*ptr);
132  }
133 }
134 
136 #define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
137 
141 #ifdef __clang_analyzer__
142 void raii_satisfy_analyzer_free(void* ptr);
143 #define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
144 #else
145 #define RAII_SATISFY_FREE(p) (void)0
146 #endif
147 
154 #define TRANSFER_OWNERSHIP(ptr) \
155  ({ \
156  __typeof__(ptr) _tmp_ptr = (ptr); \
157  (ptr) = 0; \
158  _tmp_ptr; \
159  })
160 
161 #endif /* UTILS_H */
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