Zero-overhead utility functions and RAII cleanup helpers.
More...
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
|
| void * | utils_buffer_offset (size_t offset) |
| | Helper to securely cast an integer offset to a pointer, often used for VBO/EBO byte offsets. More...
|
| |
| int | safe_snprintf (char *buf, size_t buf_size, const char *format,...) |
| | Safe wrapper around vsnprintf to format strings with bounds checking. More...
|
| |
| static bool | check_flag (int value, int flag) |
| | Bitwise flag check helper. More...
|
| |
| void * | safe_calloc (size_t num, size_t size) |
| | calloc wrapper with zero-size check. More...
|
| |
| bool | safe_memcpy (void *dest, size_t dest_size, const void *src, size_t count) |
| | memcpy wrapper with bounds checking. More...
|
| |
| bool | safe_memset (void *dest, size_t dest_size, int value, size_t count) |
| | memset wrapper with bounds checking. More...
|
| |
| void | safe_strncpy (char *dest, size_t dest_size, const char *src, size_t src_size) |
| | Safe wrapper around strncpy to ensure null-termination. More...
|
| |
| void | safe_strncat (char *dest, size_t dest_size, const char *src) |
| | Safe wrapper around strncat to ensure bounds safety. More...
|
| |
| bool | is_safe_filename (const char *filename) |
| | Validates a filename to prevent path traversal and shell injection. More...
|
| |
| bool | is_safe_relative_path (const char *path) |
| | Validates a relative path to prevent arbitrary file access. More...
|
| |
| static void | cleanup_file (FILE **file_ptr) |
| | RAII callback for FILE*. More...
|
| |
| static void | cleanup_free (void *ptr_ptr) |
| | RAII callback for free(). More...
|
| |
Zero-overhead utility functions and RAII cleanup helpers.
◆ CLEANUP_FILE
Macro to define a FILE* that closes itself at scope exit.
◆ CLEANUP_FREE
Macro to define a pointer that frees itself at scope exit.
◆ RAII_SATISFY_FILE
| #define RAII_SATISFY_FILE |
( |
|
f | ) |
(void)0 |
Satisfies Static Analyzers for file resource management.
◆ RAII_SATISFY_FREE
| #define RAII_SATISFY_FREE |
( |
|
p | ) |
(void)0 |
Satisfies Static Analyzers for memory resource management.
◆ TRANSFER_OWNERSHIP
| #define TRANSFER_OWNERSHIP |
( |
|
ptr | ) |
|
Value: ({ \
__typeof__(ptr) _tmp_ptr = (ptr); \
(ptr) = 0; \
_tmp_ptr; \
})
Transfers ownership of an RAII-managed variable to the caller.
Sets the local variable to NULL to prevent the cleanup attribute from triggering.
◆ check_flag()
| static bool check_flag |
( |
int |
value, |
|
|
int |
flag |
|
) |
| |
|
inlinestatic |
Bitwise flag check helper.
◆ cleanup_file()
| static void cleanup_file |
( |
FILE ** |
file_ptr | ) |
|
|
inlinestatic |
◆ cleanup_free()
| static void cleanup_free |
( |
void * |
ptr_ptr | ) |
|
|
inlinestatic |
RAII callback for free().
◆ is_safe_filename()
| bool is_safe_filename |
( |
const char * |
filename | ) |
|
Validates a filename to prevent path traversal and shell injection.
Rejects strings containing path separators ('/', '\') or directory traversal sequences ("..") or current directory (".").
- Parameters
-
| filename | The filename to check. |
- Returns
- true if the filename is safe, false otherwise.
◆ is_safe_relative_path()
| bool is_safe_relative_path |
( |
const char * |
path | ) |
|
Validates a relative path to prevent arbitrary file access.
Rejects absolute paths, parent directory traversal (".."), and platform-specific path features like backslashes or drive letters.
- Parameters
-
| path | The relative path to check. |
- Returns
- true if the path is safe, false otherwise.
◆ safe_calloc()
| void* safe_calloc |
( |
size_t |
num, |
|
|
size_t |
size |
|
) |
| |
calloc wrapper with zero-size check.
◆ safe_memcpy()
| bool safe_memcpy |
( |
void * |
dest, |
|
|
size_t |
dest_size, |
|
|
const void * |
src, |
|
|
size_t |
count |
|
) |
| |
memcpy wrapper with bounds checking.
◆ safe_memset()
| bool safe_memset |
( |
void * |
dest, |
|
|
size_t |
dest_size, |
|
|
int |
value, |
|
|
size_t |
count |
|
) |
| |
memset wrapper with bounds checking.
◆ safe_snprintf()
| int safe_snprintf |
( |
char * |
buf, |
|
|
size_t |
buf_size, |
|
|
const char * |
format, |
|
|
|
... |
|
) |
| |
Safe wrapper around vsnprintf to format strings with bounds checking.
- Parameters
-
| buf | Destination buffer. |
| buf_size | Buffer capacity. |
| format | Printf-style format string. |
- Returns
- number of characters written (excluding null terminator) on success, -1 if truncated or error.
◆ safe_strncat()
| void safe_strncat |
( |
char * |
dest, |
|
|
size_t |
dest_size, |
|
|
const char * |
src |
|
) |
| |
Safe wrapper around strncat to ensure bounds safety.
- Parameters
-
| dest | Destination buffer. |
| dest_size | Total size of destination buffer. |
| src | Source string. |
◆ safe_strncpy()
| void safe_strncpy |
( |
char * |
dest, |
|
|
size_t |
dest_size, |
|
|
const char * |
src, |
|
|
size_t |
src_size |
|
) |
| |
Safe wrapper around strncpy to ensure null-termination.
- Parameters
-
| dest | Destination buffer. |
| dest_size | Size of destination buffer. |
| src | Source string. |
| src_size | Max characters to copy (or just use sizeof(dest)). |
◆ utils_buffer_offset()
| void* utils_buffer_offset |
( |
size_t |
offset | ) |
|
Helper to securely cast an integer offset to a pointer, often used for VBO/EBO byte offsets.
- Parameters
-
| offset | The byte offset to cast. |
- Returns
- A void pointer representing the offset.