From bf1e1b73fcac4320bfcc9a5a84f1bbd4d681003c Mon Sep 17 00:00:00 2001 From: jfreegman Date: Sun, 12 Dec 2021 11:30:27 -0500 Subject: [PATCH] Do proper error handling for a few malloc calls --- src/notify.c | 15 +++++++++++++-- src/notify.h | 2 +- src/video_device.c | 12 ++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/notify.c b/src/notify.c index 9e946dc..8863ed1 100644 --- a/src/notify.c +++ b/src/notify.c @@ -489,16 +489,27 @@ void terminate_notify(void) } #ifdef SOUND_NOTIFY -int set_sound(Notification sound, const char *value) + +/* + * Sets notification sound designated by `sound` to file path `value`. + * + * Return true if the sound is successfully set. + */ +bool set_sound(Notification sound, const char *value) { if (sound == silent) { - return 0; + return false; } free(Control.sounds[sound]); size_t len = strlen(value) + 1; Control.sounds[sound] = calloc(len, 1); + + if (Control.sounds[sound] == NULL) { + return false; + } + memcpy(Control.sounds[sound], value, len); struct stat buf; diff --git a/src/notify.h b/src/notify.h index 3adba0f..693ff94 100644 --- a/src/notify.h +++ b/src/notify.h @@ -77,7 +77,7 @@ int box_silent_notify(ToxWindow *self, uint64_t flags, int *id_indicator, const int box_silent_notify2(ToxWindow *self, uint64_t flags, int id, const char *format, ...); #ifdef SOUND_NOTIFY -int set_sound(Notification sound, const char *value); +bool set_sound(Notification sound, const char *value); #endif /* SOUND_NOTIFY */ #endif /* NOTIFY_H */ diff --git a/src/video_device.c b/src/video_device.c index 0948c01..62f42a5 100644 --- a/src/video_device.c +++ b/src/video_device.c @@ -209,12 +209,24 @@ VideoDeviceError init_video_devices(void) /* Query V4L for capture capabilities */ if (-1 != ioctl(fd, VIDIOC_QUERYCAP, &cap)) { video_input_name = (char *)malloc(strlen((const char *)cap.card) + strlen(device_address) + 4); + + if (video_input_name == NULL) { + close(fd); + return vde_InternalError; + } + strcpy(video_input_name, (char *)cap.card); strcat(video_input_name, " ("); strcat(video_input_name, (char *)device_address); strcat(video_input_name, ")"); } else { video_input_name = (char *)malloc(strlen(device_address) + 3); + + if (video_input_name == NULL) { + close(fd); + return vde_InternalError; + } + strcpy(video_input_name, "("); strcat(video_input_name, device_address); strcat(video_input_name, ")");