Skip to content

Commit

Permalink
Disabled checking of malloc() and realloc() return values by default
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Dec 7, 2017
1 parent e5eaad8 commit 5133d70
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 41 deletions.
10 changes: 10 additions & 0 deletions include/b6b/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
#define b6b_likely(cond) __builtin_expect(!!(cond), 1)
#define b6b_unlikely(cond) __builtin_expect(!!(cond), 0)

/* if malloc() can return a non-NULL pointer when out of memory and writing to
* that address will crash the process, checking the return value of malloc() is
* useless; if malloc() gets to return NULL, we let the process crash due to
* dereference of NULL */
#ifdef B6B_OPTIMISTIC_ALLOC
# define b6b_allocated(cond) 1
#else
# define b6b_allocated(cond) b6b_likely(cond)
#endif

/**
* @enum b6b_res
* Procedure status codes, which control the flow of execution
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ option('with_threads', type: 'boolean')
option('with_miniz', type: 'boolean')
option('with_linenoise', type: 'boolean')
option('with_valgrind', type: 'boolean', value: false)
option('optimistic_alloc', type: 'boolean')
8 changes: 4 additions & 4 deletions src/b6b_ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static enum b6b_res b6b_ffi_func_proc(struct b6b_interp *interp,
f = (struct b6b_ffi_func *)o->priv;

argps = (void **)malloc(sizeof(void *) * f->argc);
if (b6b_unlikely(!argps))
if (!b6b_allocated(argps))
return B6B_ERR;

li = b6b_list_first(argpos);
Expand Down Expand Up @@ -272,7 +272,7 @@ static int b6b_ffi_get_types(struct b6b_interp *interp,
size_t i;

*types = (ffi_type **)malloc(sizeof(ffi_type *) * names->slen);
if (b6b_unlikely(!*types))
if (!b6b_allocated(*types))
return 0;

for (i = 0; i < names->slen; ++i) {
Expand Down Expand Up @@ -305,7 +305,7 @@ static enum b6b_res b6b_ffi_proc_func(struct b6b_interp *interp,
return B6B_ERR;

f = (struct b6b_ffi_func *)malloc(sizeof(*f));
if (b6b_unlikely(!f))
if (!b6b_allocated(f))
return B6B_ERR;

f->rtype = b6b_ffi_type(interp, rtype->s[0]);
Expand Down Expand Up @@ -486,7 +486,7 @@ static enum b6b_res b6b_ffi_proc_pack(struct b6b_interp *interp,
}

buf = malloc(B6B_FFI_MAX_STRUCT_SZ);
if (b6b_unlikely(!buf))
if (!b6b_allocated(buf))
return B6B_ERR;

for (; i < fmt->slen; ++i, vli = b6b_list_next(vli)) {
Expand Down
4 changes: 2 additions & 2 deletions src/b6b_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ static struct b6b_obj *b6b_file_new(struct b6b_interp *interp,
struct b6b_stdio_strm *s;

s = (struct b6b_stdio_strm *)malloc(sizeof(*s));
if (b6b_unlikely(!s))
if (!b6b_allocated(s))
return NULL;

s->fp = fp;
Expand All @@ -73,7 +73,7 @@ static struct b6b_obj *b6b_file_new(struct b6b_interp *interp,

if (bmode == _IOFBF) {
s->buf = malloc(B6B_STRM_BUFSIZ);
if (b6b_unlikely(!s->buf)) {
if (!b6b_allocated(s->buf)) {
b6b_destroy(o);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct b6b_frame *b6b_frame_new(struct b6b_frame *prev)
{
struct b6b_frame *f = (struct b6b_frame *)malloc(sizeof(*f));

if (b6b_likely(f)) {
if (b6b_allocated(f)) {
f->locals = b6b_dict_new();
if (b6b_unlikely(!f->locals)) {
free(f);
Expand Down
6 changes: 3 additions & 3 deletions src/b6b_interp.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ enum b6b_res b6b_source(struct b6b_interp *interp, const char *path)
}

s = (char *)malloc((size_t)stbuf.st_size + 1);
if (b6b_unlikely(!s)) {
if (!b6b_allocated(s)) {
close(fd);
return B6B_ERR;
}
Expand Down Expand Up @@ -936,7 +936,7 @@ static enum b6b_res b6b_interp_proc_repr(struct b6b_interp *interp,
len = s->slen + 2;

buf = (char *)malloc(len + 1);
if (b6b_unlikely(!buf))
if (!b6b_allocated(buf))
return B6B_ERR;

buf[0] = '{';
Expand Down Expand Up @@ -1033,7 +1033,7 @@ static enum b6b_res b6b_interp_proc_b6b(struct b6b_interp *interp,
return B6B_ERR;

b6b = (struct b6b_interp *)malloc(sizeof(*b6b));
if (b6b_unlikely(!b6b))
if (!b6b_allocated(b6b))
return B6B_ERR;

o = b6b_str_fmt("b6b:%"PRIxPTR, (uintptr_t)b6b);
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static int b6b_list_do_add(struct b6b_obj *l, struct b6b_obj *o)
struct b6b_litem *li;

li = (struct b6b_litem *)malloc(sizeof(*li));
if (b6b_unlikely(!li))
if (!b6b_allocated(li))
return 0;

li->o = b6b_ref(o);
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct b6b_obj *b6b_new(void)
{
struct b6b_obj *o = (struct b6b_obj *)malloc(sizeof(*o));

if (o) {
if (b6b_allocated(o)) {
o->refc = 1;
o->proc = b6b_obj_proc;
o->del = NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static enum b6b_res b6b_poll_proc(struct b6b_interp *interp,

evs = (struct epoll_event *)malloc(
sizeof(struct epoll_event) * n->i);
if (!evs) {
if (!b6b_allocated(evs)) {
b6b_destroy(l);
return B6B_ERR;
}
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static enum b6b_res b6b_proc_proc_proc(struct b6b_interp *interp,
return B6B_ERR;

priv = (struct b6b_proc *)malloc(sizeof(*priv));
if (b6b_unlikely(!priv))
if (!b6b_allocated(priv))
return B6B_ERR;

o = b6b_str_copy(n->s, n->slen);
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ static struct b6b_obj *b6b_sh_new_pipe(struct b6b_interp *interp,
int fl, err;

s = (struct b6b_sh *)malloc(sizeof(*s));
if (b6b_unlikely(!s))
if (!b6b_allocated(s))
return NULL;

if (pipe2(fds, O_CLOEXEC) < 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/b6b_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ static enum b6b_res b6b_signal_proc_signal(struct b6b_interp *interp,
return B6B_ERR;

sig = (struct b6b_signal *)malloc(sizeof(*sig));
if (b6b_unlikely(!sig))
if (!b6b_allocated(sig))
return B6B_ERR;

if (sigemptyset(&sig->set) < 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/b6b_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ static struct b6b_obj *b6b_socket_inet_peer(struct b6b_interp *interp,
unsigned short port = ntohs(sin->sin_port);

buf = (char *)malloc(len);
if (b6b_unlikely(!buf))
if (!b6b_allocated(buf))
return NULL;

switch (s->peer.ss_family) {
Expand Down Expand Up @@ -211,7 +211,7 @@ static struct b6b_obj *b6b_socket_new(struct b6b_interp *interp,
}

s = (struct b6b_socket *)malloc(sizeof(*s));
if (b6b_unlikely(!s)) {
if (!b6b_allocated(s)) {
close(fd);
return NULL;
}
Expand Down
4 changes: 2 additions & 2 deletions src/b6b_stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ ssize_t b6b_stdio_write(struct b6b_interp *interp,
/* we must copy the buffer, since it may be the string representation of an
* object freed during context switch */
copy = (unsigned char *)malloc(len);
if (b6b_unlikely(!copy))
if (!b6b_allocated(copy))
return -1;
memcpy(copy, buf, len);

Expand Down Expand Up @@ -242,7 +242,7 @@ static int b6b_stdio_wrap(struct b6b_interp *interp,
struct b6b_stdio_strm *s;

s = (struct b6b_stdio_strm *)malloc(sizeof(*s));
if (b6b_unlikely(!s))
if (!b6b_allocated(s))
return 0;

s->fp = fp;
Expand Down
22 changes: 11 additions & 11 deletions src/b6b_str.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ char *b6b_strndup(const char *s, const size_t len)
{
char *s2 = (char *)malloc(len + 1);

if (b6b_likely(s2)) {
if (b6b_allocated(s2)) {
memcpy(s2, s, len);
s2[len] = '\0';
}
Expand Down Expand Up @@ -130,7 +130,7 @@ int b6b_as_str(struct b6b_obj *o)
if (o->s) { /* non-empty with whitespace */
nlen = o->slen + li->o->slen + 3;
s2 = (char *)realloc(o->s, nlen + 1);
if (b6b_unlikely(!s2)) {
if (!b6b_allocated(s2)) {
free(o->s);
return 0;
}
Expand All @@ -147,7 +147,7 @@ int b6b_as_str(struct b6b_obj *o)
else { /* first, non-empty with whitespace */
o->slen = li->o->slen + 2;
o->s = (char *)malloc(o->slen + 1);
if (b6b_unlikely(!o->s))
if (!b6b_allocated(o->s))
return 0;

o->s[0] = '{';
Expand All @@ -160,7 +160,7 @@ int b6b_as_str(struct b6b_obj *o)
if (o->s) { /* non-empty without whitespace */
nlen = o->slen + li->o->slen + 1;
s2 = (char *)realloc(o->s, nlen + 1);
if (b6b_unlikely(!s2)) {
if (!b6b_allocated(s2)) {
free(o->s);
return 0;
}
Expand All @@ -185,7 +185,7 @@ int b6b_as_str(struct b6b_obj *o)
if (o->s) { /* empty */
nlen = o->slen + 3;
s2 = (char *)realloc(o->s, nlen + 1);
if (b6b_unlikely(!s2)) {
if (!b6b_allocated(s2)) {
free(o->s);
return 0;
}
Expand All @@ -200,7 +200,7 @@ int b6b_as_str(struct b6b_obj *o)
}
else { /* first, empty */
o->s = (char *)malloc(3);
if (b6b_unlikely(!o->s))
if (!b6b_allocated(o->s))
return 0;

o->s[0] = '{';
Expand All @@ -214,7 +214,7 @@ int b6b_as_str(struct b6b_obj *o)
/* special case: empty list */
if (!o->s) {
o->s = (char *)malloc(3);
if (b6b_unlikely(!o->s))
if (!b6b_allocated(o->s))
return 0;

o->s[0] = '{';
Expand Down Expand Up @@ -378,7 +378,7 @@ static enum b6b_res b6b_str_proc_join(struct b6b_interp *interp,

len = li->o->slen;
s = (char *)malloc(len + 1);
if (b6b_unlikely(!s))
if (!b6b_allocated(s))
return B6B_ERR;

memcpy(s, li->o->s, li->o->slen);
Expand All @@ -392,7 +392,7 @@ static enum b6b_res b6b_str_proc_join(struct b6b_interp *interp,
mlen = len + d->slen + li->o->slen;

ms = (char *)realloc(s, mlen + 1);
if (b6b_unlikely(!ms)) {
if (!b6b_allocated(ms)) {
free(s);
return B6B_ERR;
}
Expand Down Expand Up @@ -499,7 +499,7 @@ static enum b6b_res b6b_str_proc_chr(struct b6b_interp *interp,
return B6B_ERR;

buf = (char *)malloc(2);
if (b6b_unlikely(!buf))
if (!b6b_allocated(buf))
return B6B_ERR;

s = b6b_str_new(buf, 1);
Expand All @@ -524,7 +524,7 @@ static enum b6b_res b6b_str_proc_expand(struct b6b_interp *interp,
return B6B_ERR;

s2 = (char *)malloc(s->slen + 1);
if (b6b_unlikely(!s2))
if (!b6b_allocated(s2))
return B6B_ERR;

last = s->slen - 1;
Expand Down
6 changes: 3 additions & 3 deletions src/b6b_strm.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static enum b6b_res b6b_strm_read(struct b6b_interp *interp,
len += est;

nbuf = (unsigned char *)realloc(buf, len);
if (b6b_unlikely(!nbuf)) {
if (!b6b_allocated(nbuf)) {
free(buf);
return B6B_ERR;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ static enum b6b_res b6b_strm_writeln(struct b6b_interp *interp,
char *s;

s = (char *)malloc(len + 2);
if (b6b_unlikely(!s))
if (!b6b_allocated(s))
return B6B_ERR;

memcpy(s, buf, len);
Expand Down Expand Up @@ -317,7 +317,7 @@ static struct b6b_obj *b6b_strm_new(struct b6b_interp *interp,

if (b6b_unlikely(o)) {
strm = (struct b6b_strm *)malloc(sizeof(*strm));
if (b6b_unlikely(!strm)) {
if (!b6b_allocated(strm)) {
b6b_destroy(o);
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions src/b6b_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static int b6b_thread_prep(struct b6b_thread *t,

if (!t->stack) {
t->stack = malloc(stksiz);
if (b6b_unlikely(!t->stack))
if (!b6b_allocated(t->stack))
goto bail;

#ifdef B6B_HAVE_VALGRIND
Expand Down Expand Up @@ -139,7 +139,7 @@ struct b6b_thread *b6b_thread_new(struct b6b_threads *threads,
struct b6b_thread *t;

t = (struct b6b_thread *)malloc(sizeof(*t));
if (b6b_unlikely(!t))
if (!b6b_allocated(t))
return NULL;

if (!b6b_thread_prep(t,
Expand Down Expand Up @@ -173,7 +173,7 @@ struct b6b_thread *b6b_thread_self(struct b6b_frame *global,
struct b6b_thread *t;

t = (struct b6b_thread *)malloc(sizeof(*t));
if (b6b_unlikely(!t))
if (!b6b_allocated(t))
return NULL;

t->curr = b6b_frame_new(global);
Expand Down
8 changes: 4 additions & 4 deletions src/b6b_zlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ static enum b6b_res b6b_zlib_deflate(struct b6b_interp *interp,

/* copy the buffer, since s->s may be freed after context switch */
src = (unsigned char *)malloc(s->slen);
if (b6b_unlikely(!src)) {
if (!b6b_allocated(src)) {
mz_deflateEnd(&data.strm);
return B6B_ERR;
}

dlen = (size_t)mz_deflateBound(&data.strm, (mz_ulong)s->slen);
dst = (unsigned char *)malloc(dlen);
if (b6b_unlikely(!dst)) {
if (!b6b_allocated(dst)) {
free(src);
mz_deflateEnd(&data.strm);
return B6B_ERR;
Expand Down Expand Up @@ -160,7 +160,7 @@ static void b6b_zlib_do_inflate(void *arg)

do {
mdst = (unsigned char *)realloc(data->dst, data->dlen);
if (b6b_unlikely(!mdst))
if (!b6b_allocated(mdst))
return;

data->strm.next_out = mdst + (data->strm.next_out - data->dst);
Expand Down Expand Up @@ -207,7 +207,7 @@ static enum b6b_res b6b_zlib_inflate(struct b6b_interp *interp,
return B6B_ERR;

src = (unsigned char *)malloc(s->slen);
if (b6b_unlikely(!src)) {
if (!b6b_allocated(src)) {
mz_inflateEnd(&data.strm);
return B6B_ERR;
}
Expand Down

0 comments on commit 5133d70

Please sign in to comment.