What is the String Module?
The String module provides bounded string operations to prevent buffer overflows. All operations specify maximum lengths and return status codes instead of silently truncating or crashing.
Key idea:** Strings with known boundaries. No overflows, no truncation surprises.
Quick Start
1. Include Header
#include "rte/string/rte_string.h"
2. Safe Copy
strcpy(dest, src);
}
const char * rte_status_to_string(rte_status_t status)
Returns a short, static, human-readable string for a status code. Intended for diagnostics/logging on...
rte_status_t
Common result/status codes.
3. Safe Concatenation
char path[256];
strcpy(path, "/home/");
log_error("Path too long");
return rc;
}
@ RTE_STATUS_RESOURCE_EXHAUSTED
4. Safe String Length
size_t len = rte_strnlen(str, 256);
if (len == 256) {
log_error("String not null-terminated within 256 bytes");
}
Operations
rte_status_t rte_strncpy(
char *dest,
const char *src,
size_t max_len);
rte_status_t rte_strncat(
char *dest,
const char *src,
size_t max_len);
size_t rte_strnlen(const char *str, size_t max_len);
int rte_strncmp(const char *a, const char *b, size_t max_len);
rte_status_t rte_snprintf(
char *buf,
size_t max_len,
const char *fmt, ...);
Practical Examples
Example 1: Command Parsing
void parse_command(const char *input) {
char cmd[32];
char arg[64];
if (sscanf(input, "%31s %63s", cmd, arg) != 2) {
log_error("Invalid command format");
return;
}
if (rte_strncmp(cmd, "status", 32) == 0) {
handle_status(arg);
} else if (rte_strncmp(cmd, "reset", 32) == 0) {
handle_reset(arg);
}
}
Example 2: Log Message Formatting
void log_event(const char *event, int code, const char *detail) {
char buffer[256];
"[%u] EVENT: %s (code=%d) - %s",
get_timestamp(), event, code, detail);
log_error("Log message formatting failed");
return;
}
write_to_log(buffer);
}
Best Practices
- Always Specify Maximum Length
- No open-ended operations
- Always know buffer size
- Use sizeof(buffer) when possible
- Check Return Codes
- RTE_STATUS_OK: Success, string complete
- RTE_STATUS_RESOURCE_EXHAUSTED: String truncated/overflow
- RTE_STATUS_INVALID_PARAM: Bad input
- Use Fixed-Size Buffers
- Never dynamically sized strings
- Allocate statically
- Know max length at compile-time
- Prefer snprintf Over sprintf
- snprintf has length limit
- sprintf doesn't (buffer overflow risk)
- Always use rte_snprintf()
See Also