diff --git a/src/c2.c b/src/c2.c index 6baf1337..433a725c 100644 --- a/src/c2.c +++ b/src/c2.c @@ -573,6 +573,10 @@ c2_parse_pattern(session_t *ps, const char *pattern, int offset, c2_ptr_t *presu // 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)); + if (!tptnstr){ + printf_errf("(): Failed to allocate memory for pattern string."); + exit(1); + } char *ptptnstr = tptnstr; pleaf->ptnstr = tptnstr; for (; pattern[offset] && delim != pattern[offset]; ++offset) { @@ -940,7 +944,10 @@ c2_dump_raw(c2_ptr_t p) { putchar('@'); if (pleaf->index >= 0) printf("[%d]", pleaf->index); - printf(":%d%s", pleaf->format, c2h_dump_str_type(pleaf)); + char* type = c2h_dump_str_type(pleaf); + if (type) { + printf(":%d%s", pleaf->format, type); + } } // Print operator diff --git a/src/common.h b/src/common.h index 2b75ff7a..4110415f 100644 --- a/src/common.h +++ b/src/common.h @@ -1816,11 +1816,11 @@ fds_drop(session_t *ps, int fd, short events) { fd_set * key = NULL; \ if (ps->key) { \ key = malloc(sizeof(fd_set)); \ - memcpy(key, ps->key, sizeof(fd_set)); \ if (!key) { \ fprintf(stderr, "Failed to allocate memory for copying select() fdset.\n"); \ exit(1); \ } \ + memcpy(key, ps->key, sizeof(fd_set)); \ } \ /** diff --git a/src/compton.c b/src/compton.c index 92d08dc5..e6cb65c3 100644 --- a/src/compton.c +++ b/src/compton.c @@ -181,6 +181,10 @@ make_gaussian_map(double r) { double g; c = malloc(sizeof(conv) + size * size * sizeof(double)); + if (!c){ + printf_errf("(): Failed to allocate memory for gaussian map."); + exit(1); + } c->size = size; c->data = (double *) (c + 1); t = 0.0; @@ -287,6 +291,11 @@ presum_gaussian(session_t *ps, conv *map) { ps->shadow_corner = malloc((ps->cgsize + 1) * (ps->cgsize + 1) * 26); ps->shadow_top = malloc((ps->cgsize + 1) * 26); + if ((!ps->shadow_corner) || (!ps->shadow_top)){ + printf_errf("(): Failed to allocate memory for shadow corners and sides."); + exit(1); + } + for (x = 0; x <= ps->cgsize; x++) { ps->shadow_top[25 * (ps->cgsize + 1) + x] = sum_gaussian(map, 1, x - center, center, ps->cgsize * 2, ps->cgsize * 2); @@ -4132,6 +4141,11 @@ ev_expose(session_t *ps, XExposeEvent *ev) { } } + if (!ps->expose_rects){ + printf_errf("(): Failed to allocate memory for expose rects."); + exit(1); + } + ps->expose_rects[ps->n_expose].x = ev->x; ps->expose_rects[ps->n_expose].y = ev->y; ps->expose_rects[ps->n_expose].width = ev->width; @@ -4899,6 +4913,10 @@ register_cm(session_t *ps) { } char *buf = malloc(len); + if (!buf){ + printf_errf("(): Failed to allocate memory for creating window."); + exit(1); + } snprintf(buf, len, REGISTER_PROP "%d", ps->scr); buf[len - 1] = '\0'; XSetSelectionOwner(ps->dpy, get_atom(ps, buf), ps->reg_win, 0); @@ -5040,7 +5058,7 @@ parse_matrix(session_t *ps, const char *src, const char **endptr) { int wid = 0, hei = 0; const char *pc = NULL; XFixed *matrix = NULL; - + // Get matrix width and height { double val = 0.0; @@ -5178,7 +5196,7 @@ parse_conv_kern_lst(session_t *ps, const char *src, XFixed **dest, int max) { return false; } - if (*pc) { + if ((pc) && (*pc)) { printf_errf("(): Too many blur kernels!"); return false; } @@ -5798,7 +5816,12 @@ get_cfg(session_t *ps, int argc, char *const *argv, bool first_pass) { .menu_opacity = 1.0, }; bool shadow_enable = false, fading_enable = false; - char *lc_numeric_old = mstrcpy(setlocale(LC_NUMERIC, NULL)); + char *locale_old = setlocale(LC_NUMERIC, NULL); + if (!locale_old){ + printf_errf("(): Failed to allocate memory for old locale."); + exit(1); + } + char *lc_numeric_old = mstrcpy(locale_old); for (i = 0; i < NUM_WINTYPES; ++i) { ps->o.wintype_fade[i] = false; @@ -6495,6 +6518,10 @@ init_alpha_picts(session_t *ps) { int num = round(1.0 / ps->o.alpha_step) + 1; ps->alpha_picts = malloc(sizeof(Picture) * num); + if (!ps->alpha_picts) { + printf_errf("(): Failed to allocate memory for alpha picts."); + exit(1); + } for (i = 0; i < num; ++i) { double o = i * ps->o.alpha_step; @@ -6857,12 +6884,20 @@ mainloop(session_t *ps) { // Consider ev_received firstly if (ps->ev_received || ps->o.benchmark) { ptv = malloc(sizeof(struct timeval)); + if (!ptv){ + printf_errf("(): Failed to allocate memory for timout timeval."); + exit(1); + } ptv->tv_sec = 0L; ptv->tv_usec = 0L; } // Then consider fading timeout else if (!ps->idling) { ptv = malloc(sizeof(struct timeval)); + if (!ptv){ + printf_errf("(): Failed to allocate memory for timout timeval."); + exit(1); + } *ptv = ms_to_tv(fade_timeout(ps)); } @@ -6882,6 +6917,10 @@ mainloop(session_t *ps) { if (tmout_ms < TIME_MS_MAX) { if (!ptv) { ptv = malloc(sizeof(struct timeval)); + if (!ptv){ + printf_errf("(): Failed to allocate memory for timout timeval."); + exit(1); + } *ptv = ms_to_tv(tmout_ms); } else if (timeval_ms_cmp(ptv, tmout_ms) > 0) { @@ -7131,6 +7170,11 @@ 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)); + if (!ps){ + printf_errf("(): Failed to allocate memory for session."); + exit(1); + } + memcpy(ps, &s_def, sizeof(session_t)); ps_g = ps; ps->ignore_tail = &ps->ignore_head; diff --git a/src/opengl.c b/src/opengl.c index 5a98f4e0..9918fb25 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -402,7 +402,13 @@ glx_init_blur(session_t *ps) { #ifdef CONFIG_VSYNC_OPENGL_GLSL { - char *lc_numeric_old = mstrcpy(setlocale(LC_NUMERIC, NULL)); + char *locale_old = setlocale(LC_NUMERIC, NULL); + if (!locale_old){ + printf_errf("(): Failed to allocate memory for old locale."); + exit(1); + } + char *lc_numeric_old = mstrcpy(locale_old); + // Enforce LC_NUMERIC locale "C" here to make sure decimal point is sane // Thanks to hiciu for reporting. setlocale(LC_NUMERIC, "C"); @@ -752,6 +758,10 @@ glx_bind_pixmap(session_t *ps, glx_texture_t **pptex, Pixmap pixmap, }; ptex = malloc(sizeof(glx_texture_t)); + if (!ptex){ + printf_errf("(): Failed to allocate memory for pixmap."); + exit(1); + } allocchk(ptex); memcpy(ptex, &GLX_TEX_DEF, sizeof(glx_texture_t)); *pptex = ptex;