X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Futil.h;h=a9c39b867ea1a05b58af339641973113f59b2630;hb=a9e12476ed32256690eb801099c41526834b6390;hp=e5728bd87e275eee904a5d021a105f0211ad8765;hpb=7d5e9c0f60cddf01ec803012cbdc02d2f55b78c1;p=elogind.git diff --git a/src/shared/util.h b/src/shared/util.h index e5728bd87..a9c39b867 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -111,13 +111,13 @@ size_t page_size(void); bool streq_ptr(const char *a, const char *b); -#define new(t, n) ((t*) malloc(sizeof(t)*(n))) +#define new(t, n) ((t*) malloc_multiply(sizeof(t), (n))) #define new0(t, n) ((t*) calloc((n), sizeof(t))) #define newa(t, n) ((t*) alloca(sizeof(t)*(n))) -#define newdup(t, p, n) ((t*) memdup(p, sizeof(t)*(n))) +#define newdup(t, p, n) ((t*) memdup_multiply(p, sizeof(t), (n))) #define malloc0(n) (calloc((n), 1)) @@ -141,7 +141,7 @@ static inline bool isempty(const char *p) { return !p || !p[0]; } -bool endswith(const char *s, const char *postfix); +char *endswith(const char *s, const char *postfix); bool startswith(const char *s, const char *prefix); bool startswith_no_case(const char *s, const char *prefix); @@ -384,10 +384,11 @@ int status_welcome(void); int fd_columns(int fd); unsigned columns(void); -unsigned columns_uncached(void); - int fd_lines(int fd); unsigned lines(void); +void columns_lines_cache_reset(int _unused_ signum); + +bool on_tty(void); int running_in_chroot(void); @@ -514,7 +515,7 @@ char *format_bytes(char *buf, size_t l, off_t t); int fd_wait_for_event(int fd, int event, usec_t timeout); -void* memdup(const void *p, size_t l); +void* memdup(const void *p, size_t l) _malloc_; int is_kernel_thread(pid_t pid); @@ -543,3 +544,26 @@ void fclosep(FILE **f); void closep(int *fd); void closedirp(DIR **d); void umaskp(mode_t *u); + +_malloc_ static inline void *malloc_multiply(size_t a, size_t b) { + if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) + return NULL; + + return malloc(a * b); +} + +_malloc_ static inline void *memdup_multiply(const void *p, size_t a, size_t b) { + if (_unlikely_(b == 0 || a > ((size_t) -1) / b)) + return NULL; + + return memdup(p, a * b); +} + +bool filename_is_safe(const char *p); +bool string_is_safe(const char *p); + +int parse_timestamp(const char *t, usec_t *usec); + +void *xbsearch_r(const void *key, const void *base, size_t nmemb, size_t size, + int (*compar) (const void *, const void *, void *), + void *arg);