Skip to content

Commit

Permalink
add safe mbuf_printf
Browse files Browse the repository at this point in the history
  • Loading branch information
sreimers committed Aug 9, 2023
1 parent 4840f37 commit b5993e6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 11 additions & 1 deletion include/re_mbuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,17 @@ uint64_t mbuf_read_u64(struct mbuf *mb);
int mbuf_read_str(struct mbuf *mb, char *str, size_t size);
int mbuf_strdup(struct mbuf *mb, char **strp, size_t len);
int mbuf_vprintf(struct mbuf *mb, const char *fmt, va_list ap);
int mbuf_printf(struct mbuf *mb, const char *fmt, ...);

#ifdef HAVE_RE_ARG
#define mbuf_printf(mb, fmt, ...) \
_mbuf_printf_s((mb), (fmt), RE_VA_ARGS(__VA_ARGS__))
#else
#define mbuf_printf _mbuf_printf
#endif

int _mbuf_printf(struct mbuf *mb, const char *fmt, ...);
int _mbuf_printf_s(struct mbuf *mb, const char *fmt, ...);

int mbuf_write_pl_skip(struct mbuf *mb, const struct pl *pl,
const struct pl *skip);
int mbuf_fill(struct mbuf *mb, uint8_t c, size_t n);
Expand Down
23 changes: 22 additions & 1 deletion src/mbuf/mbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ int mbuf_vprintf(struct mbuf *mb, const char *fmt, va_list ap)
*
* @return 0 if success, otherwise errorcode
*/
int mbuf_printf(struct mbuf *mb, const char *fmt, ...)
int _mbuf_printf(struct mbuf *mb, const char *fmt, ...)
{
int err = 0;
va_list ap;
Expand All @@ -544,6 +544,27 @@ int mbuf_printf(struct mbuf *mb, const char *fmt, ...)
}


/**
* Print a safe formatted string to a memory buffer
*
* @param mb Memory buffer
* @param fmt Formatted string
*
* @return 0 if success, otherwise errorcode
*/
int _mbuf_printf_s(struct mbuf *mb, const char *fmt, ...)
{
int err = 0;
va_list ap;

va_start(ap, fmt);
err = re_vhprintf_s(fmt, ap, vprintf_handler, mb);
va_end(ap);

return err;
}


/**
* Write a pointer-length string to a memory buffer, excluding a section
*
Expand Down

0 comments on commit b5993e6

Please sign in to comment.