$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 <stdbool.h>
10 #include <stddef.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 
20 void* utils_buffer_offset(size_t offset);
21 
30 __attribute__((format(printf, 3, 4))) int safe_snprintf(char* buf,
31  size_t buf_size,
32  const char* format,
33  ...);
34 
38 static inline bool check_flag(int value, int flag)
39 {
40  return ((unsigned int)value & (unsigned int)flag) != 0;
41 }
42 
46 void* safe_calloc(size_t num, size_t size);
47 
51 bool safe_memcpy(void* dest, size_t dest_size, const void* src, size_t count);
52 
56 bool safe_memset(void* dest, size_t dest_size, int value, size_t count);
57 
65 void safe_strncpy(char* dest, size_t dest_size, const char* src,
66  size_t src_size);
67 
74 void safe_strncat(char* dest, size_t dest_size, const char* src);
75 
85 bool is_safe_filename(const char* filename);
86 
96 bool is_safe_relative_path(const char* path);
97 
101 static inline void cleanup_file(FILE** file_ptr)
102 {
103  if (file_ptr && *file_ptr) {
104  (void)fclose(*file_ptr);
105  }
106 }
107 
109 #define CLEANUP_FILE __attribute__((cleanup(cleanup_file)))
110 
114 #ifdef __clang_analyzer__
115 void raii_satisfy_analyzer_file(FILE* file_ptr);
116 #define RAII_SATISFY_FILE(f) raii_satisfy_analyzer_file(f)
117 #else
118 #define RAII_SATISFY_FILE(f) (void)0
119 #endif
120 
124 static inline void cleanup_free(void* ptr_ptr)
125 {
126  void** ptr = (void**)ptr_ptr;
127  if (ptr && *ptr) {
128  free(*ptr);
129  }
130 }
131 
133 #define CLEANUP_FREE __attribute__((cleanup(cleanup_free)))
134 
138 #ifdef __clang_analyzer__
139 void raii_satisfy_analyzer_free(void* ptr);
140 #define RAII_SATISFY_FREE(p) raii_satisfy_analyzer_free(p)
141 #else
142 #define RAII_SATISFY_FREE(p) (void)0
143 #endif
144 
151 #define TRANSFER_OWNERSHIP(ptr) \
152  ({ \
153  __typeof__(ptr) _tmp_ptr = (ptr); \
154  (ptr) = 0; \
155  _tmp_ptr; \
156  })
157 
158 #endif /* UTILS_H */
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