mirror of https://github.com/xzeldon/htop.git
Reimplement xAsnprintf and xSnprintf as type-safe functions
This commit is contained in:
parent
241e4b3dbf
commit
8b55113ea8
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) {
|
||||
|
|
8
XAlloc.h
8
XAlloc.h
|
@ -19,11 +19,11 @@ void* xCalloc(size_t nmemb, size_t size);
|
|||
|
||||
void* xRealloc(void* ptr, size_t size);
|
||||
|
||||
#undef xAsprintf
|
||||
ATTR_FORMAT(printf, 2, 3)
|
||||
int xAsprintf(char **strp, const char* fmt, ...);
|
||||
|
||||
#define xAsprintf(strp, fmt, ...) do { int _r=asprintf(strp, fmt, __VA_ARGS__); if (_r < 0) { fail(); } } while(0)
|
||||
|
||||
#define xSnprintf(fmt, len, ...) do { int _l=len; int _n=snprintf(fmt, _l, __VA_ARGS__); if (!(_n > -1 && _n < _l)) { curs_set(1); endwin(); err(1, NULL); } } while(0)
|
||||
ATTR_FORMAT(printf, 3, 4)
|
||||
int xSnprintf(char *buf, int len, const char* fmt, ...);
|
||||
|
||||
#undef xStrdup
|
||||
#undef xStrdup_
|
||||
|
|
Loading…
Reference in New Issue