$darkmode
Suckless OGL 1.0.0
A lean, high-performance C11 PBR Renderer
utils.h File Reference

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>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define CLEANUP_FILE   __attribute__((cleanup(cleanup_file)))
 Macro to define a FILE* that closes itself at scope exit. More...
 
#define RAII_SATISFY_FILE(f)   (void)0
 Satisfies Static Analyzers for file resource management. More...
 
#define CLEANUP_FREE   __attribute__((cleanup(cleanup_free)))
 Macro to define a pointer that frees itself at scope exit. More...
 
#define RAII_SATISFY_FREE(p)   (void)0
 Satisfies Static Analyzers for memory resource management. More...
 
#define TRANSFER_OWNERSHIP(ptr)
 Transfers ownership of an RAII-managed variable to the caller. More...
 

Functions

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...
 

Detailed Description

Zero-overhead utility functions and RAII cleanup helpers.

Macro Definition Documentation

◆ CLEANUP_FILE

#define CLEANUP_FILE   __attribute__((cleanup(cleanup_file)))

Macro to define a FILE* that closes itself at scope exit.

◆ CLEANUP_FREE

#define CLEANUP_FREE   __attribute__((cleanup(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.

Function Documentation

◆ 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

RAII callback for FILE*.

◆ 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
filenameThe 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
pathThe 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
bufDestination buffer.
buf_sizeBuffer capacity.
formatPrintf-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
destDestination buffer.
dest_sizeTotal size of destination buffer.
srcSource 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
destDestination buffer.
dest_sizeSize of destination buffer.
srcSource string.
src_sizeMax 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
offsetThe byte offset to cast.
Returns
A void pointer representing the offset.