Sync the entire xmalloc.[ch] with the other users, but with the addition

of xrealloc, xvasprintf, xvsnprintf.
This commit is contained in:
nicm 2015-11-18 13:06:54 +00:00
parent ca5e6bf5f2
commit 64571368dc
7 changed files with 125 additions and 102 deletions

View File

@ -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);

4
log.c
View File

@ -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;

View File

@ -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);
}

21
tmux.h
View File

@ -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 *,

View File

@ -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);
}

128
xmalloc.c
View File

@ -1,37 +1,40 @@
/* $OpenBSD$ */
/*
* Copyright (c) 2004 Nicholas Marriott <nicm@users.sourceforge.net>
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, 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 <sys/types.h>
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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 *
@ -40,51 +43,42 @@ xcalloc(size_t nmemb, size_t size)
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);
return (newptr);
}
void *
xreallocarray(void *oldptr, size_t nmemb, size_t size)
{
void *newptr;
void *new_ptr;
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);
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;
}
return (newptr);
char *
xstrdup(const char *str)
{
char *cp;
if ((cp = strdup(str)) == NULL)
fatal("xstrdup: %s", strerror(errno));
return cp;
}
int
@ -97,7 +91,7 @@ xasprintf(char **ret, const char *fmt, ...)
i = xvasprintf(ret, fmt, ap);
va_end(ap);
return (i);
return i;
}
int
@ -106,36 +100,38 @@ xvasprintf(char **ret, const char *fmt, va_list ap)
int i;
i = vasprintf(ret, fmt, ap);
if (i < 0 || *ret == NULL)
fatal("xvasprintf");
return (i);
if (i < 0 || *ret == NULL)
fatal("xasprintf: %s", strerror(errno));
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;
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;
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;
}

40
xmalloc.h Normal file
View File

@ -0,0 +1,40 @@
/* $OpenBSD$ */
/*
* Author: Tatu Ylonen <ylo@cs.hut.fi>
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, 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 */