mirror of
https://github.com/tmate-io/tmate.git
synced 2024-12-01 12:33:20 +01:00
Sync the entire xmalloc.[ch] with the other users, but with the addition
of xrealloc, xvasprintf, xvsnprintf.
This commit is contained in:
parent
ca5e6bf5f2
commit
64571368dc
6
input.c
6
input.c
@ -1924,7 +1924,7 @@ input_utf8_open(struct input_ctx *ictx)
|
|||||||
struct utf8_data *ud = &ictx->utf8data;
|
struct utf8_data *ud = &ictx->utf8data;
|
||||||
|
|
||||||
if (utf8_open(ud, ictx->ch) != UTF8_MORE)
|
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);
|
log_debug("%s %hhu", __func__, ud->size);
|
||||||
|
|
||||||
@ -1938,7 +1938,7 @@ input_utf8_add(struct input_ctx *ictx)
|
|||||||
struct utf8_data *ud = &ictx->utf8data;
|
struct utf8_data *ud = &ictx->utf8data;
|
||||||
|
|
||||||
if (utf8_append(ud, ictx->ch) != UTF8_MORE)
|
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__);
|
log_debug("%s", __func__);
|
||||||
|
|
||||||
@ -1952,7 +1952,7 @@ input_utf8_close(struct input_ctx *ictx)
|
|||||||
struct utf8_data *ud = &ictx->utf8data;
|
struct utf8_data *ud = &ictx->utf8data;
|
||||||
|
|
||||||
if (utf8_append(ud, ictx->ch) != UTF8_DONE)
|
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,
|
log_debug("%s %hhu '%*s' (width %hhu)", __func__, ud->size,
|
||||||
(int)ud->size, ud->data, ud->width);
|
(int)ud->size, ud->data, ud->width);
|
||||||
|
4
log.c
4
log.c
@ -102,7 +102,7 @@ log_debug(const char *msg, ...)
|
|||||||
|
|
||||||
/* Log a critical error with error string and die. */
|
/* Log a critical error with error string and die. */
|
||||||
__dead void
|
__dead void
|
||||||
log_fatal(const char *msg, ...)
|
fatal(const char *msg, ...)
|
||||||
{
|
{
|
||||||
char *fmt;
|
char *fmt;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
@ -116,7 +116,7 @@ log_fatal(const char *msg, ...)
|
|||||||
|
|
||||||
/* Log a critical error and die. */
|
/* Log a critical error and die. */
|
||||||
__dead void
|
__dead void
|
||||||
log_fatalx(const char *msg, ...)
|
fatalx(const char *msg, ...)
|
||||||
{
|
{
|
||||||
char *fmt;
|
char *fmt;
|
||||||
va_list ap;
|
va_list ap;
|
||||||
|
12
options.c
12
options.c
@ -150,9 +150,9 @@ options_get_string(struct options *oo, const char *name)
|
|||||||
struct options_entry *o;
|
struct options_entry *o;
|
||||||
|
|
||||||
if ((o = options_find(oo, name)) == NULL)
|
if ((o = options_find(oo, name)) == NULL)
|
||||||
log_fatalx("missing option %s", name);
|
fatalx("missing option %s", name);
|
||||||
if (o->type != OPTIONS_STRING)
|
if (o->type != OPTIONS_STRING)
|
||||||
log_fatalx("option %s not a string", name);
|
fatalx("option %s not a string", name);
|
||||||
return (o->str);
|
return (o->str);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,9 +180,9 @@ options_get_number(struct options *oo, const char *name)
|
|||||||
struct options_entry *o;
|
struct options_entry *o;
|
||||||
|
|
||||||
if ((o = options_find(oo, name)) == NULL)
|
if ((o = options_find(oo, name)) == NULL)
|
||||||
log_fatalx("missing option %s", name);
|
fatalx("missing option %s", name);
|
||||||
if (o->type != OPTIONS_NUMBER)
|
if (o->type != OPTIONS_NUMBER)
|
||||||
log_fatalx("option %s not a number", name);
|
fatalx("option %s not a number", name);
|
||||||
return (o->num);
|
return (o->num);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,8 +220,8 @@ options_get_style(struct options *oo, const char *name)
|
|||||||
struct options_entry *o;
|
struct options_entry *o;
|
||||||
|
|
||||||
if ((o = options_find(oo, name)) == NULL)
|
if ((o = options_find(oo, name)) == NULL)
|
||||||
log_fatalx("missing option %s", name);
|
fatalx("missing option %s", name);
|
||||||
if (o->type != OPTIONS_STYLE)
|
if (o->type != OPTIONS_STYLE)
|
||||||
log_fatalx("option %s not a style", name);
|
fatalx("option %s not a style", name);
|
||||||
return (&o->style);
|
return (&o->style);
|
||||||
}
|
}
|
||||||
|
21
tmux.h
21
tmux.h
@ -45,6 +45,8 @@ struct session;
|
|||||||
struct tmuxpeer;
|
struct tmuxpeer;
|
||||||
struct tmuxproc;
|
struct tmuxproc;
|
||||||
|
|
||||||
|
#include "xmalloc.h"
|
||||||
|
|
||||||
/* Default global configuration file. */
|
/* Default global configuration file. */
|
||||||
#define TMUX_CONF "/etc/tmux.conf"
|
#define TMUX_CONF "/etc/tmux.conf"
|
||||||
|
|
||||||
@ -67,10 +69,6 @@ struct tmuxproc;
|
|||||||
#define READ_BACKOFF 512
|
#define READ_BACKOFF 512
|
||||||
#define READ_TIME 100
|
#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. */
|
/* Definition to shut gcc up about unused arguments. */
|
||||||
#define unused __attribute__ ((unused))
|
#define unused __attribute__ ((unused))
|
||||||
|
|
||||||
@ -2214,19 +2212,8 @@ char *get_proc_name(int, char *);
|
|||||||
void log_open(const char *);
|
void log_open(const char *);
|
||||||
void log_close(void);
|
void log_close(void);
|
||||||
void printflike(1, 2) log_debug(const char *, ...);
|
void printflike(1, 2) log_debug(const char *, ...);
|
||||||
__dead void printflike(1, 2) log_fatal(const char *, ...);
|
__dead void printflike(1, 2) fatal(const char *, ...);
|
||||||
__dead void printflike(1, 2) log_fatalx(const char *, ...);
|
__dead void printflike(1, 2) 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);
|
|
||||||
|
|
||||||
/* style.c */
|
/* style.c */
|
||||||
int style_parse(const struct grid_cell *,
|
int style_parse(const struct grid_cell *,
|
||||||
|
@ -556,7 +556,7 @@ tty_term_string(struct tty_term *term, enum tty_code_code code)
|
|||||||
if (!tty_term_has(term, code))
|
if (!tty_term_has(term, code))
|
||||||
return ("");
|
return ("");
|
||||||
if (term->codes[code].type != TTYCODE_STRING)
|
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);
|
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))
|
if (!tty_term_has(term, code))
|
||||||
return (0);
|
return (0);
|
||||||
if (term->codes[code].type != TTYCODE_NUMBER)
|
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);
|
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))
|
if (!tty_term_has(term, code))
|
||||||
return (0);
|
return (0);
|
||||||
if (term->codes[code].type != TTYCODE_FLAG)
|
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);
|
return (term->codes[code].value.flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
128
xmalloc.c
128
xmalloc.c
@ -1,37 +1,40 @@
|
|||||||
/* $OpenBSD$ */
|
/* $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
|
* As far as I am concerned, the code I have written for this software
|
||||||
* purpose with or without fee is hereby granted, provided that the above
|
* can be used freely for any purpose. Any derived versions of this
|
||||||
* copyright notice and this permission notice appear in all copies.
|
* 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
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
* called by a name other than "ssh" or "Secure Shell".
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <errno.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "tmux.h"
|
#include "tmux.h"
|
||||||
|
|
||||||
char *
|
void *
|
||||||
xstrdup(const char *str)
|
xmalloc(size_t size)
|
||||||
{
|
{
|
||||||
char *cp;
|
void *ptr;
|
||||||
|
|
||||||
if ((cp = strdup(str)) == NULL)
|
if (size == 0)
|
||||||
fatal("xstrdup");
|
fatal("xmalloc: zero size");
|
||||||
return (cp);
|
ptr = malloc(size);
|
||||||
|
if (ptr == NULL)
|
||||||
|
fatal("xmalloc: allocating %zu bytes: %s",
|
||||||
|
size, strerror(errno));
|
||||||
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
@ -40,51 +43,42 @@ xcalloc(size_t nmemb, size_t size)
|
|||||||
void *ptr;
|
void *ptr;
|
||||||
|
|
||||||
if (size == 0 || nmemb == 0)
|
if (size == 0 || nmemb == 0)
|
||||||
fatalx("xcalloc: zero size");
|
fatal("xcalloc: zero size");
|
||||||
if ((ptr = calloc(nmemb, size)) == NULL)
|
ptr = calloc(nmemb, size);
|
||||||
log_fatal("xcalloc: allocating %zu bytes", size);
|
if (ptr == NULL)
|
||||||
|
fatal("xcalloc: allocating %zu * %zu bytes: %s",
|
||||||
return (ptr);
|
nmemb, size, strerror(errno));
|
||||||
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
xmalloc(size_t size)
|
xrealloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
void *ptr;
|
return xreallocarray(ptr, 1, size);
|
||||||
|
|
||||||
if (size == 0)
|
|
||||||
fatalx("xmalloc: zero size");
|
|
||||||
if ((ptr = malloc(size)) == NULL)
|
|
||||||
log_fatal("xmalloc: allocating %zu bytes", size);
|
|
||||||
|
|
||||||
return (ptr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void *
|
void *
|
||||||
xrealloc(void *oldptr, size_t newsize)
|
xreallocarray(void *ptr, size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
void *newptr;
|
void *new_ptr;
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
if (nmemb == 0 || size == 0)
|
if (nmemb == 0 || size == 0)
|
||||||
fatalx("xreallocarray: zero size");
|
fatal("xreallocarray: zero size");
|
||||||
if ((newptr = reallocarray(oldptr, nmemb, size)) == NULL)
|
new_ptr = reallocarray(ptr, nmemb, size);
|
||||||
log_fatal("xreallocarray: allocating %zu * %zu bytes",
|
if (new_ptr == NULL)
|
||||||
nmemb, size);
|
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
|
int
|
||||||
@ -97,7 +91,7 @@ xasprintf(char **ret, const char *fmt, ...)
|
|||||||
i = xvasprintf(ret, fmt, ap);
|
i = xvasprintf(ret, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return (i);
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -106,36 +100,38 @@ xvasprintf(char **ret, const char *fmt, va_list ap)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = vasprintf(ret, fmt, ap);
|
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
|
int
|
||||||
xsnprintf(char *buf, size_t len, const char *fmt, ...)
|
xsnprintf(char *str, size_t len, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list ap;
|
va_list ap;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
i = xvsnprintf(buf, len, fmt, ap);
|
i = xvsnprintf(str, len, fmt, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
return (i);
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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)
|
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)
|
if (i < 0 || i >= (int)len)
|
||||||
fatalx("xvsnprintf: overflow");
|
fatal("xsnprintf: overflow");
|
||||||
|
|
||||||
return (i);
|
return i;
|
||||||
}
|
}
|
||||||
|
40
xmalloc.h
Normal file
40
xmalloc.h
Normal 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 */
|
Loading…
Reference in New Issue
Block a user