From 725d8aec3cdc38d39990bc77b409f3902b78b121 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Wed, 18 Nov 2015 13:06:55 +0000 Subject: Sync the entire xmalloc.[ch] with the other users, but with the addition of xrealloc, xvasprintf, xvsnprintf. --- usr.bin/tmux/input.c | 8 +-- usr.bin/tmux/log.c | 6 +-- usr.bin/tmux/options.c | 14 ++--- usr.bin/tmux/tmux.h | 23 ++------- usr.bin/tmux/tty-term.c | 8 +-- usr.bin/tmux/xmalloc.c | 134 +++++++++++++++++++++++------------------------- usr.bin/tmux/xmalloc.h | 40 +++++++++++++++ 7 files changed, 128 insertions(+), 105 deletions(-) create mode 100644 usr.bin/tmux/xmalloc.h diff --git a/usr.bin/tmux/input.c b/usr.bin/tmux/input.c index 7c35f5af302..2d9e2460ddd 100644 --- a/usr.bin/tmux/input.c +++ b/usr.bin/tmux/input.c @@ -1,4 +1,4 @@ -/* $OpenBSD: input.c,v 1.93 2015/11/14 12:20:19 nicm Exp $ */ +/* $OpenBSD: input.c,v 1.94 2015/11/18 13:06:54 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -1924,7 +1924,7 @@ input_utf8_open(struct input_ctx *ictx) struct utf8_data *ud = &ictx->utf8data; if (utf8_open(ud, ictx->ch) != UTF8_MORE) - log_fatalx("UTF-8 open invalid %#x", ictx->ch); + fatalx("UTF-8 open invalid %#x", ictx->ch); log_debug("%s %hhu", __func__, ud->size); @@ -1938,7 +1938,7 @@ input_utf8_add(struct input_ctx *ictx) struct utf8_data *ud = &ictx->utf8data; if (utf8_append(ud, ictx->ch) != UTF8_MORE) - log_fatalx("UTF-8 add invalid %#x", ictx->ch); + fatalx("UTF-8 add invalid %#x", ictx->ch); log_debug("%s", __func__); @@ -1952,7 +1952,7 @@ input_utf8_close(struct input_ctx *ictx) struct utf8_data *ud = &ictx->utf8data; if (utf8_append(ud, ictx->ch) != UTF8_DONE) - log_fatalx("UTF-8 close invalid %#x", ictx->ch); + fatalx("UTF-8 close invalid %#x", ictx->ch); log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size, (int)ud->size, ud->data, ud->width); diff --git a/usr.bin/tmux/log.c b/usr.bin/tmux/log.c index 2428481a138..5e7cbef5380 100644 --- a/usr.bin/tmux/log.c +++ b/usr.bin/tmux/log.c @@ -1,4 +1,4 @@ -/* $OpenBSD: log.c,v 1.17 2015/09/24 12:03:58 nicm Exp $ */ +/* $OpenBSD: log.c,v 1.18 2015/11/18 13:06:54 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -102,7 +102,7 @@ log_debug(const char *msg, ...) /* Log a critical error with error string and die. */ __dead void -log_fatal(const char *msg, ...) +fatal(const char *msg, ...) { char *fmt; va_list ap; @@ -116,7 +116,7 @@ log_fatal(const char *msg, ...) /* Log a critical error and die. */ __dead void -log_fatalx(const char *msg, ...) +fatalx(const char *msg, ...) { char *fmt; va_list ap; diff --git a/usr.bin/tmux/options.c b/usr.bin/tmux/options.c index eb622e0f4ec..af3fb28902c 100644 --- a/usr.bin/tmux/options.c +++ b/usr.bin/tmux/options.c @@ -1,4 +1,4 @@ -/* $OpenBSD: options.c,v 1.14 2015/11/13 16:06:43 nicm Exp $ */ +/* $OpenBSD: options.c,v 1.15 2015/11/18 13:06:54 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -150,9 +150,9 @@ options_get_string(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - log_fatalx("missing option %s", name); + fatalx("missing option %s", name); if (o->type != OPTIONS_STRING) - log_fatalx("option %s not a string", name); + fatalx("option %s not a string", name); return (o->str); } @@ -180,9 +180,9 @@ options_get_number(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - log_fatalx("missing option %s", name); + fatalx("missing option %s", name); if (o->type != OPTIONS_NUMBER) - log_fatalx("option %s not a number", name); + fatalx("option %s not a number", name); return (o->num); } @@ -220,8 +220,8 @@ options_get_style(struct options *oo, const char *name) struct options_entry *o; if ((o = options_find(oo, name)) == NULL) - log_fatalx("missing option %s", name); + fatalx("missing option %s", name); if (o->type != OPTIONS_STYLE) - log_fatalx("option %s not a style", name); + fatalx("option %s not a style", name); return (&o->style); } diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index b746f1f33c2..98d4590e78a 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.580 2015/11/15 22:50:38 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.581 2015/11/18 13:06:54 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -45,6 +45,8 @@ struct session; struct tmuxpeer; struct tmuxproc; +#include "xmalloc.h" + /* Default global configuration file. */ #define TMUX_CONF "/etc/tmux.conf" @@ -67,10 +69,6 @@ struct tmuxproc; #define READ_BACKOFF 512 #define READ_TIME 100 -/* Fatal errors. */ -#define fatal(msg) log_fatal("%s: %s", __func__, msg); -#define fatalx(msg) log_fatalx("%s: %s", __func__, msg); - /* Definition to shut gcc up about unused arguments. */ #define unused __attribute__ ((unused)) @@ -2214,19 +2212,8 @@ char *get_proc_name(int, char *); void log_open(const char *); void log_close(void); void printflike(1, 2) log_debug(const char *, ...); -__dead void printflike(1, 2) log_fatal(const char *, ...); -__dead void printflike(1, 2) log_fatalx(const char *, ...); - -/* xmalloc.c */ -char *xstrdup(const char *); -void *xcalloc(size_t, size_t); -void *xmalloc(size_t); -void *xrealloc(void *, size_t); -void *xreallocarray(void *, size_t, size_t); -int printflike(2, 3) xasprintf(char **, const char *, ...); -int xvasprintf(char **, const char *, va_list); -int printflike(3, 4) xsnprintf(char *, size_t, const char *, ...); -int xvsnprintf(char *, size_t, const char *, va_list); +__dead void printflike(1, 2) fatal(const char *, ...); +__dead void printflike(1, 2) fatalx(const char *, ...); /* style.c */ int style_parse(const struct grid_cell *, diff --git a/usr.bin/tmux/tty-term.c b/usr.bin/tmux/tty-term.c index 555f0e4634a..1f6db556cb2 100644 --- a/usr.bin/tmux/tty-term.c +++ b/usr.bin/tmux/tty-term.c @@ -1,4 +1,4 @@ -/* $OpenBSD: tty-term.c,v 1.41 2015/10/27 15:58:43 nicm Exp $ */ +/* $OpenBSD: tty-term.c,v 1.42 2015/11/18 13:06:54 nicm Exp $ */ /* * Copyright (c) 2008 Nicholas Marriott @@ -556,7 +556,7 @@ tty_term_string(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (""); if (term->codes[code].type != TTYCODE_STRING) - log_fatalx("not a string: %d", code); + fatalx("not a string: %d", code); return (term->codes[code].value.string); } @@ -591,7 +591,7 @@ tty_term_number(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (0); if (term->codes[code].type != TTYCODE_NUMBER) - log_fatalx("not a number: %d", code); + fatalx("not a number: %d", code); return (term->codes[code].value.number); } @@ -601,7 +601,7 @@ tty_term_flag(struct tty_term *term, enum tty_code_code code) if (!tty_term_has(term, code)) return (0); if (term->codes[code].type != TTYCODE_FLAG) - log_fatalx("not a flag: %d", code); + fatalx("not a flag: %d", code); return (term->codes[code].value.flag); } diff --git a/usr.bin/tmux/xmalloc.c b/usr.bin/tmux/xmalloc.c index 4c489edca5b..e00074c9407 100644 --- a/usr.bin/tmux/xmalloc.c +++ b/usr.bin/tmux/xmalloc.c @@ -1,141 +1,137 @@ -/* $OpenBSD: xmalloc.c,v 1.8 2015/11/17 18:25:03 tobias Exp $ */ +/* $OpenBSD: xmalloc.c,v 1.9 2015/11/18 13:06:54 nicm Exp $ */ /* - * Copyright (c) 2004 Nicholas Marriott + * Author: Tatu Ylonen + * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland + * All rights reserved + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER - * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING - * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". */ -#include - +#include +#include #include +#include #include #include #include "tmux.h" -char * -xstrdup(const char *str) +void * +xmalloc(size_t size) { - char *cp; + void *ptr; - if ((cp = strdup(str)) == NULL) - fatal("xstrdup"); - return (cp); + if (size == 0) + fatal("xmalloc: zero size"); + ptr = malloc(size); + if (ptr == NULL) + fatal("xmalloc: allocating %zu bytes: %s", + size, strerror(errno)); + return ptr; } void * xcalloc(size_t nmemb, size_t size) { - void *ptr; + void *ptr; if (size == 0 || nmemb == 0) - fatalx("xcalloc: zero size"); - if ((ptr = calloc(nmemb, size)) == NULL) - log_fatal("xcalloc: allocating %zu bytes", size); - - return (ptr); + fatal("xcalloc: zero size"); + ptr = calloc(nmemb, size); + if (ptr == NULL) + fatal("xcalloc: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); + return ptr; } void * -xmalloc(size_t size) +xrealloc(void *ptr, size_t size) { - void *ptr; - - if (size == 0) - fatalx("xmalloc: zero size"); - if ((ptr = malloc(size)) == NULL) - log_fatal("xmalloc: allocating %zu bytes", size); - - return (ptr); + return xreallocarray(ptr, 1, size); } void * -xrealloc(void *oldptr, size_t newsize) +xreallocarray(void *ptr, size_t nmemb, size_t size) { - void *newptr; - - if (newsize == 0) - fatalx("xrealloc: zero size"); - if ((newptr = realloc(oldptr, newsize)) == NULL) - log_fatal("xrealloc: allocating %zu bytes", newsize); + void *new_ptr; - return (newptr); + if (nmemb == 0 || size == 0) + fatal("xreallocarray: zero size"); + new_ptr = reallocarray(ptr, nmemb, size); + if (new_ptr == NULL) + fatal("xreallocarray: allocating %zu * %zu bytes: %s", + nmemb, size, strerror(errno)); + return new_ptr; } -void * -xreallocarray(void *oldptr, size_t nmemb, size_t size) +char * +xstrdup(const char *str) { - void *newptr; + char *cp; - if (nmemb == 0 || size == 0) - fatalx("xreallocarray: zero size"); - if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL) - log_fatal("xreallocarray: allocating %zu * %zu bytes", - nmemb, size); - - return (newptr); + if ((cp = strdup(str)) == NULL) + fatal("xstrdup: %s", strerror(errno)); + return cp; } int xasprintf(char **ret, const char *fmt, ...) { va_list ap; - int i; + int i; va_start(ap, fmt); i = xvasprintf(ret, fmt, ap); va_end(ap); - return (i); + return i; } int xvasprintf(char **ret, const char *fmt, va_list ap) { - int i; + int i; i = vasprintf(ret, fmt, ap); + if (i < 0 || *ret == NULL) - fatal("xvasprintf"); + fatal("xasprintf: %s", strerror(errno)); - return (i); + return i; } int -xsnprintf(char *buf, size_t len, const char *fmt, ...) +xsnprintf(char *str, size_t len, const char *fmt, ...) { va_list ap; - int i; + int i; va_start(ap, fmt); - i = xvsnprintf(buf, len, fmt, ap); + i = xvsnprintf(str, len, fmt, ap); va_end(ap); - return (i); + return i; } int -xvsnprintf(char *buf, size_t len, const char *fmt, va_list ap) +xvsnprintf(char *str, size_t len, const char *fmt, va_list ap) { - int i; + int i; if (len > INT_MAX) - fatalx("xvsnprintf: len > INT_MAX"); + fatal("xsnprintf: len > INT_MAX"); + + i = vsnprintf(str, len, fmt, ap); - i = vsnprintf(buf, len, fmt, ap); if (i < 0 || i >= (int)len) - fatalx("xvsnprintf: overflow"); + fatal("xsnprintf: overflow"); - return (i); + return i; } diff --git a/usr.bin/tmux/xmalloc.h b/usr.bin/tmux/xmalloc.h new file mode 100644 index 00000000000..6d30e3cfdab --- /dev/null +++ b/usr.bin/tmux/xmalloc.h @@ -0,0 +1,40 @@ +/* $OpenBSD: xmalloc.h,v 1.1 2015/11/18 13:06:54 nicm Exp $ */ + +/* + * Author: Tatu Ylonen + * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland + * All rights reserved + * Created: Mon Mar 20 22:09:17 1995 ylo + * + * Versions of malloc and friends that check their results, and never return + * failure (they call fatal if they encounter an error). + * + * As far as I am concerned, the code I have written for this software + * can be used freely for any purpose. Any derived versions of this + * software must be clearly marked as such, and if the derived work is + * incompatible with the protocol description in the RFC file, it must be + * called by a name other than "ssh" or "Secure Shell". + */ + +#ifndef XMALLOC_H +#define XMALLOC_H + +void *xmalloc(size_t); +void *xcalloc(size_t, size_t); +void *xrealloc(void *, size_t); +void *xreallocarray(void *, size_t, size_t); +char *xstrdup(const char *); +int xasprintf(char **, const char *, ...) + __attribute__((__format__ (printf, 2, 3))) + __attribute__((__nonnull__ (2))); +int xvasprintf(char **, const char *, va_list) + __attribute__((__nonnull__ (2))); +int xsnprintf(char *, size_t, const char *, ...) + __attribute__((__format__ (printf, 3, 4))) + __attribute__((__nonnull__ (3))) + __attribute__((__bounded__ (__string__, 1, 2))); +int xvsnprintf(char *, size_t, const char *, va_list) + __attribute__((__nonnull__ (3))) + __attribute__((__bounded__ (__string__, 1, 2))); + +#endif /* XMALLOC_H */ -- cgit v1.2.3