Use checked allocation everywhere

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2018-12-15 18:47:21 +00:00
parent acb81bc9a9
commit b8912fa749
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
12 changed files with 74 additions and 62 deletions

View File

@ -31,6 +31,7 @@
#include "win.h"
#include "c2.h"
#include "string_utils.h"
#include "utils.h"
#include "log.h"
#define C2_MAX_LEVELS 10
@ -260,7 +261,7 @@ static inline c2_ptr_t
c2h_comb_tree(c2_b_op_t op, c2_ptr_t p1, c2_ptr_t p2) {
c2_ptr_t p = {
.isbranch = true,
.b = malloc(sizeof(c2_b_t))
.b = cmalloc(c2_b_t)
};
p.b->opr1 = p1;
@ -382,7 +383,7 @@ c2_parse(session_t *ps, c2_lptr_t **pcondlst, const char *pattern,
// Insert to pcondlst
{
static const c2_lptr_t lptr_def = C2_LPTR_INIT;
c2_lptr_t *plptr = malloc(sizeof(c2_lptr_t));
auto plptr = cmalloc(c2_lptr_t);
if (!plptr)
printf_errfq(1, "(): Failed to allocate memory for new condition linked"
" list element.");
@ -609,7 +610,7 @@ static int
c2_parse_target(session_t *ps, const char *pattern, int offset, c2_ptr_t *presult) {
// Initialize leaf
presult->isbranch = false;
presult->l = malloc(sizeof(c2_l_t));
presult->l = cmalloc(c2_l_t);
if (!presult->l)
c2_error("Failed to allocate memory for new leaf.");
@ -918,7 +919,7 @@ c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presu
// We can't determine the length of the pattern, so we use the length
// to the end of the pattern string -- currently escape sequences
// cannot be converted to a string longer than itself.
char *tptnstr = malloc((strlen(pattern + offset) + 1) * sizeof(char));
auto tptnstr = ccalloc((strlen(pattern + offset) + 1), char);
char *ptptnstr = tptnstr;
pleaf->ptnstr = tptnstr;
for (; pattern[offset] && delim != pattern[offset]; ++offset) {
@ -1006,7 +1007,7 @@ c2_parse_legacy(session_t *ps, const char *pattern, int offset, c2_ptr_t *presul
c2_error("Legacy parser: Invalid format.");
// Allocate memory for new leaf
c2_l_t *pleaf = malloc(sizeof(c2_l_t));
auto pleaf = cmalloc(c2_l_t);
if (!pleaf)
printf_errfq(1, "(): Failed to allocate memory for new leaf.");
presult->isbranch = false;
@ -1096,7 +1097,7 @@ c2_l_postprocess(session_t *ps, c2_l_t *pleaf) {
}
}
if (!found) {
latom_t *pnew = malloc(sizeof(latom_t));
auto pnew = cmalloc(latom_t);
if (!pnew)
printf_errfq(1, "(): Failed to allocate memory for new track atom.");
pnew->next = ps->track_atom_lst;

View File

@ -142,6 +142,8 @@
#include "x.h"
#include "region.h"
#include "log.h"
#include "utils.h"
#include "compiler.h"
// === Constants ===
@ -1380,7 +1382,7 @@ set_ignore(session_t *ps, unsigned long sequence) {
if (ps->o.show_all_xerrors)
return;
ignore_t *i = malloc(sizeof(ignore_t));
auto i = cmalloc(ignore_t);
if (!i) return;
i->sequence = sequence;
@ -1545,8 +1547,8 @@ glx_mark_(session_t *ps, const char *func, XID xid, bool start) {
if (glx_has_context(ps) && ps->psglx->glStringMarkerGREMEDY) {
if (!func) func = "(unknown)";
const char *postfix = (start ? " (start)": " (end)");
char *str = malloc((strlen(func) + 12 + 2
+ strlen(postfix) + 5) * sizeof(char));
auto str = ccalloc((strlen(func) + 12 + 2
+ strlen(postfix) + 5), char);
strcpy(str, func);
sprintf(str + strlen(str), "(%#010lx)%s", xid, postfix);
ps->psglx->glStringMarkerGREMEDY(strlen(str), str);

4
src/compiler.h Normal file
View File

@ -0,0 +1,4 @@
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2018 Yuxuan Shui <yshuiv7@gmail.com>
#pragma once
#define auto __auto_type

View File

@ -21,6 +21,7 @@
#include <ev.h>
#include "compiler.h"
#include "compton.h"
#ifdef CONFIG_OPENGL
#include "opengl.h"
@ -30,6 +31,7 @@
#include "config.h"
#include "diagnostic.h"
#include "string_utils.h"
#include "utils.h"
#include "log.h"
#define auto __auto_type
@ -289,7 +291,7 @@ resize_region(session_t *ps, region_t *region, short mod) {
int nrects;
int nnewrects = 0;
pixman_box32_t *rects = pixman_region32_rectangles(region, &nrects);
pixman_box32_t *newrects = calloc(nrects, sizeof *newrects);
auto newrects = ccalloc(nrects, pixman_box32_t);
for (int i = 0; i < nrects; i++) {
int x1 = max_i(rects[i].x1 - mod, 0);
int y1 = max_i(rects[i].y1 - mod, 0);
@ -384,8 +386,7 @@ cxinerama_upd_scrs(session_t *ps) {
xcb_xinerama_screen_info_t *scrs = xcb_xinerama_query_screens_screen_info(ps->xinerama_scrs);
ps->xinerama_nscrs = xcb_xinerama_query_screens_screen_info_length(ps->xinerama_scrs);
ps->xinerama_scr_regs = allocchk(malloc(sizeof(region_t)
* ps->xinerama_nscrs));
ps->xinerama_scr_regs = ccalloc(ps->xinerama_nscrs, region_t);
for (int i = 0; i < ps->xinerama_nscrs; ++i) {
const xcb_xinerama_screen_info_t * const s = &scrs[i];
pixman_region32_init_rect(&ps->xinerama_scr_regs[i], s->x_org, s->y_org, s->width, s->height);
@ -583,7 +584,7 @@ make_gaussian_map(double r) {
int center = size/2;
double t;
c = malloc(sizeof(conv)+size*size*sizeof(double));
c = cvalloc(sizeof(conv)+size*size*sizeof(double));
c->size = size;
t = 0.0;
@ -691,8 +692,8 @@ presum_gaussian(session_t *ps, conv *map) {
if (ps->shadow_top)
free(ps->shadow_top);
ps->shadow_corner = malloc((ps->cgsize + 1) * (ps->cgsize + 1) * 26);
ps->shadow_top = malloc((ps->cgsize + 1) * 26);
ps->shadow_corner = cvalloc((ps->cgsize + 1) * (ps->cgsize + 1) * 26);
ps->shadow_top = cvalloc((ps->cgsize + 1) * 26);
for (x = 0; x <= ps->cgsize; x++) {
ps->shadow_top[25 * (ps->cgsize + 1) + x] =
@ -1531,7 +1532,7 @@ win_blur_background(session_t *ps, win *w, xcb_render_picture_t tgt_buffer,
// Allocate cache space if needed
if (!kern_dst) {
kern_dst = malloc((kwid * khei + 2) * sizeof(xcb_render_fixed_t));
kern_dst = ccalloc(kwid * khei + 2, xcb_render_fixed_t);
if (!kern_dst) {
printf_errf("(): Failed to allocate memory for blur kernel.");
return;
@ -2935,11 +2936,10 @@ ev_expose(session_t *ps, xcb_expose_event_t *ev) {
int more = ev->count + 1;
if (ps->n_expose == ps->size_expose) {
if (ps->expose_rects) {
ps->expose_rects = realloc(ps->expose_rects,
(ps->size_expose + more) * sizeof(rect_t));
ps->expose_rects = crealloc(ps->expose_rects, ps->size_expose + more);
ps->size_expose += more;
} else {
ps->expose_rects = malloc(more * sizeof(rect_t));
ps->expose_rects = ccalloc(more, rect_t);
ps->size_expose = more;
}
}
@ -3719,7 +3719,7 @@ register_cm(session_t *ps) {
s /= 10;
}
char *buf = malloc(len);
auto buf = ccalloc(len, char);
snprintf(buf, len, REGISTER_PROP "%d", ps->scr);
buf[len - 1] = '\0';
atom = get_atom(ps, buf);
@ -4281,12 +4281,8 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) {
DOUBLE_TO_XFIXED(1), DOUBLE_TO_XFIXED(1), DOUBLE_TO_XFIXED(1),
DOUBLE_TO_XFIXED(1), DOUBLE_TO_XFIXED(1), DOUBLE_TO_XFIXED(1),
};
ps->o.blur_kerns[0] = malloc(sizeof(convolution_blur));
if (!ps->o.blur_kerns[0]) {
printf_errf("(): Failed to allocate memory for convolution kernel.");
exit(1);
}
memcpy(ps->o.blur_kerns[0], &convolution_blur, sizeof(convolution_blur));
ps->o.blur_kerns[0] = ccalloc(ARR_SIZE(convolution_blur), xcb_render_fixed_t);
memcpy(ps->o.blur_kerns[0], convolution_blur, sizeof(convolution_blur));
}
rebuild_shadow_exclude_reg(ps);
@ -4671,7 +4667,7 @@ vsync_deinit(session_t *ps) {
*/
static void
init_alpha_picts(session_t *ps) {
ps->alpha_picts = malloc(sizeof(xcb_render_picture_t) * (MAX_ALPHA+1));
ps->alpha_picts = ccalloc(MAX_ALPHA+1, xcb_render_picture_t);
for (int i = 0; i <= MAX_ALPHA; ++i) {
double o = (double) i / MAX_ALPHA;
@ -5187,7 +5183,7 @@ session_init(session_t *ps_old, int argc, char **argv) {
};
// Allocate a session and copy default values into it
session_t *ps = malloc(sizeof(session_t));
session_t *ps = cmalloc(session_t);
*ps = s_def;
ps->loop = EV_DEFAULT;
pixman_region32_init(&ps->screen_reg);

View File

@ -5,6 +5,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include "compiler.h"
#include "common.h"
#include "utils.h"
#include "c2.h"
@ -61,17 +62,16 @@ xcb_render_fixed_t *
parse_matrix(session_t *ps, const char *src, const char **endptr) {
int wid = 0, hei = 0;
const char *pc = NULL;
xcb_render_fixed_t *matrix = NULL;
// Get matrix width and height
{
double val = 0.0;
if (src == (pc = parse_matrix_readnum(src, &val)))
goto parse_matrix_err;
goto err1;
src = pc;
wid = val;
if (src == (pc = parse_matrix_readnum(src, &val)))
goto parse_matrix_err;
goto err1;
src = pc;
hei = val;
}
@ -79,21 +79,21 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) {
// Validate matrix width and height
if (wid <= 0 || hei <= 0) {
printf_errf("(): Invalid matrix width/height.");
goto parse_matrix_err;
goto err1;
}
if (!(wid % 2 && hei % 2)) {
printf_errf("(): Width/height not odd.");
goto parse_matrix_err;
goto err1;
}
if (wid > 16 || hei > 16)
printf_errf("(): Matrix width/height too large, may slow down"
"rendering, and/or consume lots of memory");
// Allocate memory
matrix = calloc(wid * hei + 2, sizeof(xcb_render_fixed_t));
auto matrix = ccalloc(wid * hei + 2, xcb_render_fixed_t);
if (!matrix) {
printf_errf("(): Failed to allocate memory for matrix.");
goto parse_matrix_err;
goto err1;
}
// Read elements
@ -108,7 +108,7 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) {
}
double val = 0;
if (src == (pc = parse_matrix_readnum(src, &val)))
goto parse_matrix_err;
goto err2;
src = pc;
if (val < 0) hasneg = true;
matrix[2 + i] = DOUBLE_TO_XFIXED(val);
@ -122,7 +122,7 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) {
for ( ;*pc && ';' != *pc; ++pc)
if (!isspace(*pc) && ',' != *pc) {
printf_errf("(): Trailing characters in matrix string.");
goto parse_matrix_err;
goto err2;
}
// Jump over spaces after ';'
@ -138,7 +138,7 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) {
*endptr = pc;
else if (*pc) {
printf_errf("(): Only one matrix expected.");
goto parse_matrix_err;
goto err2;
}
// Fill in width and height
@ -147,8 +147,9 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) {
return matrix;
parse_matrix_err:
err2:
free(matrix);
err1:
return NULL;
}

View File

@ -9,6 +9,7 @@
*
*/
#include "compiler.h"
#include "win.h"
#include "string_utils.h"
#include "log.h"
@ -164,7 +165,7 @@ static dbus_bool_t
cdbus_callback_add_timeout(DBusTimeout *timeout, void *data) {
session_t *ps = data;
ev_dbus_timer *t = calloc(1, sizeof *t);
auto t = ccalloc(1, ev_dbus_timer);
double i = dbus_timeout_get_interval(timeout) / 1000.0;
ev_timer_init(&t->w, cdbus_callback_handle_timeout, i, i);
t->t = timeout;
@ -251,7 +252,7 @@ static dbus_bool_t
cdbus_callback_add_watch(DBusWatch *watch, void *data) {
session_t *ps = data;
ev_dbus_io *w = calloc(1, sizeof *w);
auto w = ccalloc(1, ev_dbus_io);
w->dw = watch;
w->ps = ps;
ev_io_init(&w->w, cdbus_io_callback, dbus_watch_get_unix_fd(watch),
@ -420,7 +421,7 @@ cdbus_apdarg_wids(session_t *ps, DBusMessage *msg, const void *data) {
}
// Allocate memory for an array of window IDs
cdbus_window_t *arr = malloc(sizeof(cdbus_window_t) * count);
auto arr = ccalloc(count, cdbus_window_t);
if (!arr) {
printf_errf("(): Failed to allocate memory for window ID array.");
return false;

View File

@ -9,6 +9,7 @@
*
*/
#include "compiler.h"
#include "string_utils.h"
#include "log.h"
@ -77,8 +78,7 @@ glx_update_fbconfig_bydepth(session_t *ps, int depth, glx_fbconfig_t *pfbcfg) {
printf_dbgf("(%d): %#x overrides %#x, target %#x.\n", depth, (unsigned) pfbcfg->cfg, (ps->psglx->fbconfigs[depth] ? (unsigned) ps->psglx->fbconfigs[depth]->cfg: 0), pfbcfg->texture_tgts);
#endif
if (!ps->psglx->fbconfigs[depth]) {
ps->psglx->fbconfigs[depth] = malloc(sizeof(glx_fbconfig_t));
allocchk(ps->psglx->fbconfigs[depth]);
ps->psglx->fbconfigs[depth] = cmalloc(glx_fbconfig_t);
}
(*ps->psglx->fbconfigs[depth]) = *pfbcfg;
}
@ -260,7 +260,7 @@ glx_init(session_t *ps, bool need_render) {
// Initialize GLX data structure
if (!ps->psglx) {
static const glx_session_t CGLX_SESSION_DEF = CGLX_SESSION_INIT;
ps->psglx = cmalloc(1, glx_session_t);
ps->psglx = cmalloc(glx_session_t);
memcpy(ps->psglx, &CGLX_SESSION_DEF, sizeof(glx_session_t));
for (int i = 0; i < MAX_BLUR_PASS; ++i) {
@ -571,7 +571,7 @@ glx_init_blur(session_t *ps) {
(strlen(shader_add) + strlen(texture_func) + 42) * nele +
strlen(FRAG_SHADER_BLUR_SUFFIX) +
strlen(texture_func) + 12 + 1;
char *shader_str = calloc(len, sizeof(char));
char *shader_str = ccalloc(len, char);
if (!shader_str) {
printf_errf("(): Failed to allocate %d bytes for shader string.", len);
return false;
@ -708,7 +708,7 @@ glx_bind_pixmap(session_t *ps, glx_texture_t **pptex, xcb_pixmap_t pixmap,
.y_inverted = false,
};
ptex = malloc(sizeof(glx_texture_t));
ptex = cmalloc(glx_texture_t);
allocchk(ptex);
memcpy(ptex, &GLX_TEX_DEF, sizeof(glx_texture_t));
*pptex = ptex;
@ -1457,7 +1457,7 @@ glx_take_screenshot(session_t *ps, int *out_length) {
glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpack_align_old);
assert(unpack_align_old > 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
unsigned char *buf = cmalloc(length, unsigned char);
auto buf = ccalloc(length, unsigned char);
glReadBuffer(GL_FRONT);
glReadPixels(0, 0, ps->root_width, ps->root_height, GL_RGB,
GL_UNSIGNED_BYTE, buf);

View File

@ -1,12 +1,13 @@
#include <string.h>
#include "compiler.h"
#include "string_utils.h"
#include "utils.h"
/**
* Allocate the space and copy a string.
*/
char *mstrcpy(const char *src) {
char *str = cmalloc(strlen(src) + 1, char);
auto str = ccalloc(strlen(src) + 1, char);
strcpy(str, src);
@ -17,7 +18,7 @@ char *mstrcpy(const char *src) {
* Allocate the space and copy a string.
*/
char *mstrncpy(const char *src, unsigned len) {
char *str = cmalloc(len + 1, char);
auto str = ccalloc(len + 1, char);
strncpy(str, src, len);
str[len] = '\0';
@ -29,7 +30,7 @@ char *mstrncpy(const char *src, unsigned len) {
* Allocate the space and join two strings.
*/
char *mstrjoin(const char *src1, const char *src2) {
char *str = cmalloc(strlen(src1) + strlen(src2) + 1, char);
auto str = ccalloc(strlen(src1)+strlen(src2)+1, char);
strcpy(str, src1);
strcat(str, src2);
@ -42,8 +43,7 @@ char *mstrjoin(const char *src1, const char *src2) {
*/
char *
mstrjoin3(const char *src1, const char *src2, const char *src3) {
char *str = cmalloc(strlen(src1) + strlen(src2)
+ strlen(src3) + 1, char);
auto str = ccalloc(strlen(src1)+strlen(src2)+strlen(src3)+1, char);
strcpy(str, src1);
strcat(str, src2);
@ -56,8 +56,7 @@ mstrjoin3(const char *src1, const char *src2, const char *src3) {
* Concatenate a string on heap with another string.
*/
void mstrextend(char **psrc1, const char *src2) {
*psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1): 0) + strlen(src2) + 1,
char);
*psrc1 = crealloc(*psrc1, (*psrc1 ? strlen(*psrc1) : 0)+strlen(src2)+1);
strcat(*psrc1, src2);
}

View File

@ -119,13 +119,17 @@ allocchk_(const char *func_name, void *ptr) {
#define allocchk(ptr) allocchk_(__func__, ptr)
/// @brief Wrapper of malloc().
#define cmalloc(nmemb, type) ((type *) allocchk(malloc((nmemb) * sizeof(type))))
#define cmalloc(type) ((type *) allocchk(malloc(sizeof(type))))
/// @brief Wrapper of malloc() that takes a size
#define cvalloc(size) allocchk(malloc(size))
/// @brief Wrapper of calloc().
#define ccalloc(nmemb, type) ((type *) allocchk(calloc((nmemb), sizeof(type))))
/// @brief Wrapper of ealloc().
#define crealloc(ptr, nmemb, type) ((type *) allocchk(realloc((ptr), (nmemb) * sizeof(type))))
#define crealloc(ptr, nmemb) \
((__typeof__(ptr)) allocchk(realloc((ptr), (nmemb) * sizeof(*(ptr)))))
/// RC_TYPE generates a reference counted type from `type`
///
@ -152,7 +156,7 @@ typedef struct { \
} name##_internal_t; \
typedef type name##_t; \
Q type *name##_new(void) { \
name##_internal_t *ret = malloc(sizeof(name##_internal_t)); \
name##_internal_t *ret = cmalloc(name##_internal_t); \
ctor((type *)ret); \
ret->ref_count = 1; \
return (type *)ret; \

View File

@ -8,11 +8,13 @@
#include <stdbool.h>
#include <math.h>
#include "compiler.h"
#include "common.h"
#include "compton.h"
#include "c2.h"
#include "x.h"
#include "string_utils.h"
#include "utils.h"
#include "log.h"
#include "win.h"
@ -813,7 +815,7 @@ bool add_win(session_t *ps, Window id, Window prev) {
}
// Allocate and initialize the new win structure
win *new = malloc(sizeof(win));
auto new = cmalloc(win);
#ifdef DEBUG_EVENTS
printf_dbgf("(%#010lx): %p\n", id, new);

View File

@ -8,6 +8,7 @@
#include <xcb/xfixes.h>
#include <pixman.h>
#include "compiler.h"
#include "common.h"
#include "x.h"
#include "log.h"
@ -223,7 +224,7 @@ bool x_fetch_region(session_t *ps, xcb_xfixes_region_t r, pixman_region32_t *res
}
int nrect = xcb_xfixes_fetch_region_rectangles_length(xr);
pixman_box32_t *b = calloc(nrect, sizeof *b);
auto b = ccalloc(nrect, pixman_box32_t);
xcb_rectangle_t *xrect = xcb_xfixes_fetch_region_rectangles(xr);
for (int i = 0; i < nrect; i++) {
b[i] = (pixman_box32_t) {
@ -243,7 +244,7 @@ void x_set_picture_clip_region(session_t *ps, xcb_render_picture_t pict,
int clip_x_origin, int clip_y_origin, const region_t *reg) {
int nrects;
const rect_t *rects = pixman_region32_rectangles((region_t *)reg, &nrects);
xcb_rectangle_t *xrects = calloc(nrects, sizeof *xrects);
auto xrects = ccalloc(nrects, xcb_rectangle_t);
for (int i = 0; i < nrects; i++)
xrects[i] = (xcb_rectangle_t){
.x = rects[i].x1,

View File

@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2014 Richard Grenville <pyxlcy@gmail.com>
#include "compiler.h"
#include "log.h"
#include "xrescheck.h"
@ -23,7 +24,7 @@ static xrc_xid_record_t *gs_xid_records = NULL;
*/
void
xrc_add_xid_(XID xid, const char *type, M_POS_DATA_PARAMS) {
xrc_xid_record_t *prec = cmalloc(1, xrc_xid_record_t);
auto prec = ccalloc(1, xrc_xid_record_t);
prec->xid = xid;
prec->type = type;
M_CPY_POS_DATA(prec);