Reimplement xAsnprintf and xSnprintf as type-safe functions

This commit is contained in:
Benny Baumann
2020-09-18 20:38:25 +02:00
parent 241e4b3dbf
commit 8b55113ea8
2 changed files with 32 additions and 4 deletions

View File

@ -5,6 +5,8 @@
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
@ -39,6 +41,32 @@ void* xRealloc(void* ptr, size_t size) {
return data;
}
int xAsprintf(char** strp, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int _r = vasprintf(strp, fmt, vl);
va_end(vl);
if (_r < 0) {
fail();
}
return _r;
}
int xSnprintf(char* buf, int len, const char* fmt, ...) {
va_list vl;
va_start(vl, fmt);
int _n=vsnprintf(buf, len, fmt, vl);
va_end(vl);
if (!(_n > -1 && _n < len)) {
fail();
}
return _n;
}
char* xStrdup_(const char* str) {
char* data = strdup(str);
if (!data) {