mirror of
https://github.com/xzeldon/htop.git
synced 2025-07-13 12:44:34 +03:00
Reimplement xAsnprintf and xSnprintf as type-safe functions
This commit is contained in:
28
XAlloc.c
28
XAlloc.c
@ -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) {
|
||||
|
Reference in New Issue
Block a user