Add safe wrapper macro for sprintf

This commit is contained in:
Patryk Obara 2020-12-10 17:54:27 +01:00 committed by Patryk Obara
parent cbd1ae540b
commit fa4b74c760
2 changed files with 36 additions and 0 deletions

View file

@ -21,9 +21,27 @@
#ifndef DOSBOX_STRING_UTILS_H
#define DOSBOX_STRING_UTILS_H
#include <cstdarg>
#include <cstring>
#include <string>
#include "support.h"
template <size_t N>
int safe_sprintf(char (&dst)[N], const char *fmt, ...)
GCC_ATTRIBUTE(format(printf, 2, 3));
template <size_t N>
int safe_sprintf(char (&dst)[N], const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
const int ret = vsnprintf(dst, N, fmt, args);
va_end(args);
return ret;
}
template <size_t N>
bool starts_with(const char (&pfx)[N], const char *str) noexcept
{