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

Implementation of utility functions. More...

#include "utils.h"
#include <stdio.h>
#include <string.h>
Include dependency graph for utils.c:

Functions

int safe_snprintf (char *buf, size_t buf_size, const char *format,...)
 Safe wrapper around vsnprintf to format strings with bounds checking. 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...
 
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...
 

Detailed Description

Implementation of utility functions.

Function Documentation

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