Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a couple of null checks #307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/c2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)); \
} \

/**
Expand Down
50 changes: 47 additions & 3 deletions src/compton.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 11 additions & 1 deletion src/opengl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
Expand Down