Safe buffer string appending functions that avoid overflow.
File(s):
This module provides a mechanism to provide a fixed size character buffer and append characters and strings safely and avoiding buffer overflows.
Example:
#include <stdio.h>
#include <string.h>
#include "px_sbuf.h"
int main(void)
{
char buf[16];
memset(buf, 0xcc, sizeof(buf));
printf(
"%s\n", sbuf.
buf);
memset(buf, 0xcc, sizeof(buf));
for(int i = 0; i < 16; i++)
{
}
printf(
"%s\n", sbuf.
buf);
memset(buf, 0xcc, sizeof(buf));
printf(
"%s\n", sbuf.
buf);
while(true) {;}
}
char * buf
Pointer to character buffer.
void px_sbuf_putchar(px_sbuf_t *sbuf, char c)
Append a character to safe buffer.
void px_sbuf_reset(px_sbuf_t *sbuf)
Reset safe buffer.
void px_sbuf_printf(px_sbuf_t *sbuf, const char *format,...)
Append a formatted string to safe buffer.
void px_sbuf_print(px_sbuf_t *sbuf, const char *str)
Append a string to safe buffer.
void px_sbuf_init(px_sbuf_t *sbuf, char *buf, size_t buf_size)
Initialise safe buffer.
◆ px_sbuf_t
Definition at line 52 of file px_sbuf.h.
| Data Fields |
|
char * |
buf |
Pointer to character buffer. |
|
size_t |
buf_size |
Size of character buffer. |
|
size_t |
index |
Index of next character to be copied into buffer. |
◆ px_sbuf_init()
| void px_sbuf_init |
( |
px_sbuf_t * |
sbuf, |
|
|
char * |
buf, |
|
|
size_t |
buf_size |
|
) |
| |
Initialise safe buffer.
- Parameters
-
| sbuf | Pointer to safe buffer object |
| buf | Pointer to character buffer |
| buf_size | Size of character buffer |
Definition at line 39 of file px_sbuf.c.
◆ px_sbuf_reset()
Reset safe buffer.
- Parameters
-
| sbuf | Pointer to buffer object |
Definition at line 47 of file px_sbuf.c.
◆ px_sbuf_is_empty()
| bool px_sbuf_is_empty |
( |
const px_sbuf_t * |
sbuf | ) |
|
Is safe buffer empty?
- Parameters
-
| sbuf | Pointer to safe buffer object |
- Return values
-
| true | safe buffer is empty |
| false | safe buffer is not empty |
Definition at line 53 of file px_sbuf.c.
◆ px_sbuf_is_full()
| bool px_sbuf_is_full |
( |
const px_sbuf_t * |
sbuf | ) |
|
Is safe buffer full.
- Parameters
-
| sbuf | Pointer to safe buffer object |
- Return values
-
| true | safe buffer is full |
| false | safe buffer is empty |
Definition at line 65 of file px_sbuf.c.
◆ px_sbuf_putchar()
| void px_sbuf_putchar |
( |
px_sbuf_t * |
sbuf, |
|
|
char |
c |
|
) |
| |
Append a character to safe buffer.
- Parameters
-
| sbuf | Pointer to safe buffer object |
| c | Character to append |
Definition at line 77 of file px_sbuf.c.
◆ px_sbuf_puts()
| void px_sbuf_puts |
( |
px_sbuf_t * |
sbuf, |
|
|
const char * |
str |
|
) |
| |
Append a string and newline character to safe buffer.
- Parameters
-
| sbuf | Pointer to safe buffer object |
| str | String to append |
Definition at line 92 of file px_sbuf.c.
◆ px_sbuf_print()
| void px_sbuf_print |
( |
px_sbuf_t * |
sbuf, |
|
|
const char * |
str |
|
) |
| |
Append a string to safe buffer.
- Parameters
-
| sbuf | Pointer to safe buffer object |
| str | String to append |
Definition at line 98 of file px_sbuf.c.
◆ px_sbuf_printf()
| void px_sbuf_printf |
( |
px_sbuf_t * |
sbuf, |
|
|
const char * |
format, |
|
|
|
... |
|
) |
| |
Append a formatted string to safe buffer.
- Parameters
-
| sbuf | Pointer to safe buffer object |
| format | Printf format string |
Definition at line 131 of file px_sbuf.c.