Add OutOfMemory log helper

Add a special LOG_OOM() function to log all OutOfMemory errors (i.e.
allocations returning NULL).
This commit is contained in:
Romain Vimont 2021-11-24 22:06:11 +01:00
parent 92a458e846
commit 3653fb6b15
26 changed files with 77 additions and 56 deletions

View file

@ -86,7 +86,8 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
#define MAX_COMMAND_STRING_LEN 1024 #define MAX_COMMAND_STRING_LEN 1024
char *buf = malloc(MAX_COMMAND_STRING_LEN); char *buf = malloc(MAX_COMMAND_STRING_LEN);
if (!buf) { if (!buf) {
LOGE("Failed to execute (could not allocate error message)"); LOG_OOM();
LOGE("Failed to execute");
return; return;
} }
@ -118,6 +119,7 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
const char **argv = malloc((len + 4) * sizeof(*argv)); const char **argv = malloc((len + 4) * sizeof(*argv));
if (!argv) { if (!argv) {
LOG_OOM();
return SC_PROCESS_NONE; return SC_PROCESS_NONE;
} }

View file

@ -4,6 +4,7 @@
#include <stdio.h> #include <stdio.h>
#include "aoa_hid.h" #include "aoa_hid.h"
#include "util/log.h"
// See <https://source.android.com/devices/accessories/aoa2#hid-support>. // See <https://source.android.com/devices/accessories/aoa2#hid-support>.
#define ACCESSORY_REGISTER_HID 54 #define ACCESSORY_REGISTER_HID 54
@ -20,6 +21,7 @@ sc_hid_event_log(const struct sc_hid_event *event) {
unsigned buffer_size = event->size * 3 + 1; unsigned buffer_size = event->size * 3 + 1;
char *buffer = malloc(buffer_size); char *buffer = malloc(buffer_size);
if (!buffer) { if (!buffer) {
LOG_OOM();
return; return;
} }
for (unsigned i = 0; i < event->size; ++i) { for (unsigned i = 0; i < event->size; ++i) {

View file

@ -595,6 +595,7 @@ sc_getopt_adapter_create_longopts(void) {
struct option *longopts = struct option *longopts =
malloc((ARRAY_LEN(options) + 1) * sizeof(*longopts)); malloc((ARRAY_LEN(options) + 1) * sizeof(*longopts));
if (!longopts) { if (!longopts) {
LOG_OOM();
return NULL; return NULL;
} }

View file

@ -42,7 +42,7 @@ static bool
decoder_open(struct decoder *decoder, const AVCodec *codec) { decoder_open(struct decoder *decoder, const AVCodec *codec) {
decoder->codec_ctx = avcodec_alloc_context3(codec); decoder->codec_ctx = avcodec_alloc_context3(codec);
if (!decoder->codec_ctx) { if (!decoder->codec_ctx) {
LOGC("Could not allocate decoder context"); LOG_OOM();
return false; return false;
} }
@ -54,7 +54,7 @@ decoder_open(struct decoder *decoder, const AVCodec *codec) {
decoder->frame = av_frame_alloc(); decoder->frame = av_frame_alloc();
if (!decoder->frame) { if (!decoder->frame) {
LOGE("Could not create decoder frame"); LOG_OOM();
avcodec_close(decoder->codec_ctx); avcodec_close(decoder->codec_ctx);
avcodec_free_context(&decoder->codec_ctx); avcodec_free_context(&decoder->codec_ctx);
return false; return false;

View file

@ -24,7 +24,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
} }
char *text = malloc(clipboard_len + 1); char *text = malloc(clipboard_len + 1);
if (!text) { if (!text) {
LOGW("Could not allocate text for clipboard"); LOG_OOM();
return -1; return -1;
} }
if (clipboard_len) { if (clipboard_len) {

View file

@ -34,7 +34,6 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
ok = sc_intr_init(&file_handler->intr); ok = sc_intr_init(&file_handler->intr);
if (!ok) { if (!ok) {
LOGE("Could not create intr");
sc_cond_destroy(&file_handler->event_cond); sc_cond_destroy(&file_handler->event_cond);
sc_mutex_destroy(&file_handler->mutex); sc_mutex_destroy(&file_handler->mutex);
return false; return false;
@ -42,7 +41,7 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
file_handler->serial = strdup(serial); file_handler->serial = strdup(serial);
if (!file_handler->serial) { if (!file_handler->serial) {
LOGE("Could not strdup serial"); LOG_OOM();
sc_intr_destroy(&file_handler->intr); sc_intr_destroy(&file_handler->intr);
sc_cond_destroy(&file_handler->event_cond); sc_cond_destroy(&file_handler->event_cond);
sc_mutex_destroy(&file_handler->mutex); sc_mutex_destroy(&file_handler->mutex);

View file

@ -10,11 +10,13 @@ bool
sc_frame_buffer_init(struct sc_frame_buffer *fb) { sc_frame_buffer_init(struct sc_frame_buffer *fb) {
fb->pending_frame = av_frame_alloc(); fb->pending_frame = av_frame_alloc();
if (!fb->pending_frame) { if (!fb->pending_frame) {
LOG_OOM();
return false; return false;
} }
fb->tmp_frame = av_frame_alloc(); fb->tmp_frame = av_frame_alloc();
if (!fb->tmp_frame) { if (!fb->tmp_frame) {
LOG_OOM();
av_frame_free(&fb->pending_frame); av_frame_free(&fb->pending_frame);
return false; return false;
} }

View file

@ -160,6 +160,7 @@ static bool
sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) { sc_hid_keyboard_event_init(struct sc_hid_event *hid_event) {
unsigned char *buffer = malloc(HID_KEYBOARD_EVENT_SIZE); unsigned char *buffer = malloc(HID_KEYBOARD_EVENT_SIZE);
if (!buffer) { if (!buffer) {
LOG_OOM();
return false; return false;
} }

View file

@ -31,7 +31,7 @@ get_icon_path(void) {
char *icon_path = strdup(icon_path_env); char *icon_path = strdup(icon_path_env);
#endif #endif
if (!icon_path) { if (!icon_path) {
LOGE("Could not allocate memory"); LOG_OOM();
return NULL; return NULL;
} }
LOGD("Using SCRCPY_ICON_PATH: %s", icon_path); LOGD("Using SCRCPY_ICON_PATH: %s", icon_path);
@ -42,7 +42,7 @@ get_icon_path(void) {
LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH); LOGD("Using icon: " SCRCPY_DEFAULT_ICON_PATH);
char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH); char *icon_path = strdup(SCRCPY_DEFAULT_ICON_PATH);
if (!icon_path) { if (!icon_path) {
LOGE("Could not allocate memory"); LOG_OOM();
return NULL; return NULL;
} }
#else #else
@ -63,7 +63,7 @@ decode_image(const char *path) {
AVFormatContext *ctx = avformat_alloc_context(); AVFormatContext *ctx = avformat_alloc_context();
if (!ctx) { if (!ctx) {
LOGE("Could not allocate image decoder context"); LOG_OOM();
return NULL; return NULL;
} }
@ -93,7 +93,7 @@ decode_image(const char *path) {
AVCodecContext *codec_ctx = avcodec_alloc_context3(codec); AVCodecContext *codec_ctx = avcodec_alloc_context3(codec);
if (!codec_ctx) { if (!codec_ctx) {
LOGE("Could not allocate codec context"); LOG_OOM();
goto close_input; goto close_input;
} }
@ -109,13 +109,13 @@ decode_image(const char *path) {
AVFrame *frame = av_frame_alloc(); AVFrame *frame = av_frame_alloc();
if (!frame) { if (!frame) {
LOGE("Could not allocate frame"); LOG_OOM();
goto close_codec; goto close_codec;
} }
AVPacket *packet = av_packet_alloc(); AVPacket *packet = av_packet_alloc();
if (!packet) { if (!packet) {
LOGE("Could not allocate packet"); LOG_OOM();
av_frame_free(&frame); av_frame_free(&frame);
goto close_codec; goto close_codec;
} }

View file

@ -34,11 +34,13 @@ static struct record_packet *
record_packet_new(const AVPacket *packet) { record_packet_new(const AVPacket *packet) {
struct record_packet *rec = malloc(sizeof(*rec)); struct record_packet *rec = malloc(sizeof(*rec));
if (!rec) { if (!rec) {
LOG_OOM();
return NULL; return NULL;
} }
rec->packet = av_packet_alloc(); rec->packet = av_packet_alloc();
if (!rec->packet) { if (!rec->packet) {
LOG_OOM();
free(rec); free(rec);
return NULL; return NULL;
} }
@ -81,7 +83,7 @@ recorder_write_header(struct recorder *recorder, const AVPacket *packet) {
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t)); uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
if (!extradata) { if (!extradata) {
LOGC("Could not allocate extradata"); LOG_OOM();
return false; return false;
} }
@ -228,13 +230,11 @@ static bool
recorder_open(struct recorder *recorder, const AVCodec *input_codec) { recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
bool ok = sc_mutex_init(&recorder->mutex); bool ok = sc_mutex_init(&recorder->mutex);
if (!ok) { if (!ok) {
LOGC("Could not create mutex");
return false; return false;
} }
ok = sc_cond_init(&recorder->queue_cond); ok = sc_cond_init(&recorder->queue_cond);
if (!ok) { if (!ok) {
LOGC("Could not create cond");
goto error_mutex_destroy; goto error_mutex_destroy;
} }
@ -254,7 +254,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
recorder->ctx = avformat_alloc_context(); recorder->ctx = avformat_alloc_context();
if (!recorder->ctx) { if (!recorder->ctx) {
LOGE("Could not allocate output context"); LOG_OOM();
goto error_cond_destroy; goto error_cond_destroy;
} }
@ -338,7 +338,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
struct record_packet *rec = record_packet_new(packet); struct record_packet *rec = record_packet_new(packet);
if (!rec) { if (!rec) {
LOGC("Could not allocate record packet"); LOG_OOM();
sc_mutex_unlock(&recorder->mutex); sc_mutex_unlock(&recorder->mutex);
return false; return false;
} }
@ -375,7 +375,7 @@ recorder_init(struct recorder *recorder,
struct sc_size declared_frame_size) { struct sc_size declared_frame_size) {
recorder->filename = strdup(filename); recorder->filename = strdup(filename);
if (!recorder->filename) { if (!recorder->filename) {
LOGE("Could not strdup filename"); LOG_OOM();
return false; return false;
} }

View file

@ -271,7 +271,7 @@ av_log_callback(void *avcl, int level, const char *fmt, va_list vl) {
size_t fmt_len = strlen(fmt); size_t fmt_len = strlen(fmt);
char *local_fmt = malloc(fmt_len + 10); char *local_fmt = malloc(fmt_len + 10);
if (!local_fmt) { if (!local_fmt) {
LOGC("Could not allocate string"); LOG_OOM();
return; return;
} }
memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0' memcpy(local_fmt, "[FFmpeg] ", 9); // do not write the final '\0'

View file

@ -366,18 +366,15 @@ screen_init(struct screen *screen, const struct screen_params *params) {
bool ok = sc_video_buffer_init(&screen->vb, params->buffering_time, &cbs, bool ok = sc_video_buffer_init(&screen->vb, params->buffering_time, &cbs,
screen); screen);
if (!ok) { if (!ok) {
LOGE("Could not initialize video buffer");
return false; return false;
} }
ok = sc_video_buffer_start(&screen->vb); ok = sc_video_buffer_start(&screen->vb);
if (!ok) { if (!ok) {
LOGE("Could not start video_buffer");
goto error_destroy_video_buffer; goto error_destroy_video_buffer;
} }
if (!fps_counter_init(&screen->fps_counter)) { if (!fps_counter_init(&screen->fps_counter)) {
LOGE("Could not initialize FPS counter");
goto error_stop_and_join_video_buffer; goto error_stop_and_join_video_buffer;
} }
@ -478,7 +475,7 @@ screen_init(struct screen *screen, const struct screen_params *params) {
screen->frame = av_frame_alloc(); screen->frame = av_frame_alloc();
if (!screen->frame) { if (!screen->frame) {
LOGC("Could not create screen frame"); LOG_OOM();
goto error_destroy_texture; goto error_destroy_texture;
} }

View file

@ -34,7 +34,7 @@ get_server_path(void) {
char *server_path = strdup(server_path_env); char *server_path = strdup(server_path_env);
#endif #endif
if (!server_path) { if (!server_path) {
LOGE("Could not allocate memory"); LOG_OOM();
return NULL; return NULL;
} }
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path); LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
@ -45,7 +45,7 @@ get_server_path(void) {
LOGD("Using server: " SC_SERVER_PATH_DEFAULT); LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
char *server_path = strdup(SC_SERVER_PATH_DEFAULT); char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
if (!server_path) { if (!server_path) {
LOGE("Could not allocate memory"); LOG_OOM();
return NULL; return NULL;
} }
#else #else
@ -309,20 +309,18 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
const struct sc_server_callbacks *cbs, void *cbs_userdata) { const struct sc_server_callbacks *cbs, void *cbs_userdata) {
bool ok = sc_server_params_copy(&server->params, params); bool ok = sc_server_params_copy(&server->params, params);
if (!ok) { if (!ok) {
LOGE("Could not copy server params"); LOG_OOM();
return false; return false;
} }
ok = sc_mutex_init(&server->mutex); ok = sc_mutex_init(&server->mutex);
if (!ok) { if (!ok) {
LOGE("Could not create server mutex");
sc_server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
return false; return false;
} }
ok = sc_cond_init(&server->cond_stopped); ok = sc_cond_init(&server->cond_stopped);
if (!ok) { if (!ok) {
LOGE("Could not create server cond_stopped");
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
sc_server_params_destroy(&server->params); sc_server_params_destroy(&server->params);
return false; return false;
@ -330,7 +328,6 @@ sc_server_init(struct sc_server *server, const struct sc_server_params *params,
ok = sc_intr_init(&server->intr); ok = sc_intr_init(&server->intr);
if (!ok) { if (!ok) {
LOGE("Could not create intr");
sc_cond_destroy(&server->cond_stopped); sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex); sc_mutex_destroy(&server->mutex);
sc_server_params_destroy(&server->params); sc_server_params_destroy(&server->params);

View file

@ -42,7 +42,7 @@ stream_recv_packet(struct stream *stream, AVPacket *packet) {
assert(len); assert(len);
if (av_new_packet(packet, len)) { if (av_new_packet(packet, len)) {
LOGE("Could not allocate packet"); LOG_OOM();
return false; return false;
} }
@ -111,18 +111,18 @@ stream_push_packet(struct stream *stream, AVPacket *packet) {
if (stream->pending) { if (stream->pending) {
offset = stream->pending->size; offset = stream->pending->size;
if (av_grow_packet(stream->pending, packet->size)) { if (av_grow_packet(stream->pending, packet->size)) {
LOGE("Could not grow packet"); LOG_OOM();
return false; return false;
} }
} else { } else {
offset = 0; offset = 0;
stream->pending = av_packet_alloc(); stream->pending = av_packet_alloc();
if (!stream->pending) { if (!stream->pending) {
LOGE("Could not allocate packet"); LOG_OOM();
return false; return false;
} }
if (av_new_packet(stream->pending, packet->size)) { if (av_new_packet(stream->pending, packet->size)) {
LOGE("Could not create packet"); LOG_OOM();
av_packet_free(&stream->pending); av_packet_free(&stream->pending);
return false; return false;
} }
@ -200,7 +200,7 @@ run_stream(void *data) {
stream->codec_ctx = avcodec_alloc_context3(codec); stream->codec_ctx = avcodec_alloc_context3(codec);
if (!stream->codec_ctx) { if (!stream->codec_ctx) {
LOGC("Could not allocate codec context"); LOG_OOM();
goto end; goto end;
} }
@ -221,7 +221,7 @@ run_stream(void *data) {
AVPacket *packet = av_packet_alloc(); AVPacket *packet = av_packet_alloc();
if (!packet) { if (!packet) {
LOGE("Could not allocate packet"); LOG_OOM();
goto finally_close_parser; goto finally_close_parser;
} }

View file

@ -7,6 +7,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "util/log.h"
bool bool
sc_file_executable_exists(const char *file) { sc_file_executable_exists(const char *file) {
char *path = getenv("PATH"); char *path = getenv("PATH");
@ -24,7 +26,10 @@ sc_file_executable_exists(const char *file) {
size_t dir_len = strlen(dir); size_t dir_len = strlen(dir);
char *fullpath = malloc(dir_len + file_len + 2); char *fullpath = malloc(dir_len + file_len + 2);
if (!fullpath) if (!fullpath)
{
LOG_OOM();
continue; continue;
}
memcpy(fullpath, dir, dir_len); memcpy(fullpath, dir, dir_len);
fullpath[dir_len] = '/'; fullpath[dir_len] = '/';
memcpy(fullpath + dir_len + 1, file, file_len + 1); memcpy(fullpath + dir_len + 1, file, file_len + 1);

View file

@ -26,7 +26,7 @@ bool
sc_file_is_regular(const char *path) { sc_file_is_regular(const char *path) {
wchar_t *wide_path = sc_str_to_wchars(path); wchar_t *wide_path = sc_str_to_wchars(path);
if (!wide_path) { if (!wide_path) {
LOGC("Could not allocate wide char string"); LOG_OOM();
return false; return false;
} }

View file

@ -100,6 +100,7 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
lpAttributeList = malloc(size); lpAttributeList = malloc(size);
if (!lpAttributeList) { if (!lpAttributeList) {
LOG_OOM();
goto error_close_stderr; goto error_close_stderr;
} }
@ -133,13 +134,14 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
char *cmd = malloc(CMD_MAX_LEN); char *cmd = malloc(CMD_MAX_LEN);
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) { if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
LOG_OOM();
goto error_free_attribute_list; goto error_free_attribute_list;
} }
wchar_t *wide = sc_str_to_wchars(cmd); wchar_t *wide = sc_str_to_wchars(cmd);
free(cmd); free(cmd);
if (!wide) { if (!wide) {
LOGC("Could not allocate wide char string"); LOG_OOM();
goto error_free_attribute_list; goto error_free_attribute_list;
} }

View file

@ -31,7 +31,7 @@ sc_file_get_local_path(const char *name) {
size_t len = dirlen + namelen + 2; // +2: '/' and '\0' size_t len = dirlen + namelen + 2; // +2: '/' and '\0'
char *file_path = malloc(len); char *file_path = malloc(len);
if (!file_path) { if (!file_path) {
LOGE("Could not alloc path"); LOG_OOM();
free(executable_path); free(executable_path);
return NULL; return NULL;
} }

View file

@ -8,7 +8,7 @@ bool
sc_intr_init(struct sc_intr *intr) { sc_intr_init(struct sc_intr *intr) {
bool ok = sc_mutex_init(&intr->mutex); bool ok = sc_mutex_init(&intr->mutex);
if (!ok) { if (!ok) {
LOGE("Could not init intr mutex"); LOG_OOM();
return false; return false;
} }

View file

@ -7,6 +7,9 @@
#include "options.h" #include "options.h"
#define LOG_STR_IMPL_(x) # x
#define LOG_STR(x) LOG_STR_IMPL_(x)
#define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGV(...) SDL_LogVerbose(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGD(...) SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGI(...) SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
@ -14,6 +17,9 @@
#define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGE(...) SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__) #define LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOG_OOM() \
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
void void
sc_set_log_level(enum sc_log_level level); sc_set_log_level(enum sc_log_level level);

View file

@ -52,6 +52,7 @@ wrap(sc_raw_socket sock) {
struct sc_socket_windows *socket = malloc(sizeof(*socket)); struct sc_socket_windows *socket = malloc(sizeof(*socket));
if (!socket) { if (!socket) {
LOG_OOM();
closesocket(sock); closesocket(sock);
return SC_SOCKET_NONE; return SC_SOCKET_NONE;
} }

View file

@ -5,13 +5,15 @@
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "util/strbuf.h"
#ifdef _WIN32 #ifdef _WIN32
# include <windows.h> # include <windows.h>
# include <tchar.h> # include <tchar.h>
#endif #endif
#include "log.h"
#include "strbuf.h"
size_t size_t
sc_strncpy(char *dest, const char *src, size_t n) { sc_strncpy(char *dest, const char *src, size_t n) {
size_t i; size_t i;
@ -51,6 +53,7 @@ sc_str_quote(const char *src) {
size_t len = strlen(src); size_t len = strlen(src);
char *quoted = malloc(len + 3); char *quoted = malloc(len + 3);
if (!quoted) { if (!quoted) {
LOG_OOM();
return NULL; return NULL;
} }
memcpy(&quoted[1], src, len); memcpy(&quoted[1], src, len);
@ -188,6 +191,7 @@ sc_str_to_wchars(const char *utf8) {
wchar_t *wide = malloc(len * sizeof(wchar_t)); wchar_t *wide = malloc(len * sizeof(wchar_t));
if (!wide) { if (!wide) {
LOG_OOM();
return NULL; return NULL;
} }
@ -204,6 +208,7 @@ sc_str_from_wchars(const wchar_t *ws) {
char *utf8 = malloc(len); char *utf8 = malloc(len);
if (!utf8) { if (!utf8) {
LOG_OOM();
return NULL; return NULL;
} }

View file

@ -1,15 +1,17 @@
#include "strbuf.h" #include "strbuf.h"
#include <assert.h> #include <assert.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include "log.h"
bool bool
sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) { sc_strbuf_init(struct sc_strbuf *buf, size_t init_cap) {
buf->s = malloc(init_cap + 1); // +1 for '\0' buf->s = malloc(init_cap + 1); // +1 for '\0'
if (!buf->s) { if (!buf->s) {
LOG_OOM();
return false; return false;
} }
@ -25,6 +27,7 @@ sc_strbuf_reserve(struct sc_strbuf *buf, size_t len) {
char *s = realloc(buf->s, new_cap + 1); // +1 for '\0' char *s = realloc(buf->s, new_cap + 1); // +1 for '\0'
if (!s) { if (!s) {
// Leave the old buf->s // Leave the old buf->s
LOG_OOM();
return false; return false;
} }
buf->s = s; buf->s = s;

View file

@ -10,6 +10,7 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
void *userdata) { void *userdata) {
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata); SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
if (!sdl_thread) { if (!sdl_thread) {
LOG_OOM();
return false; return false;
} }
@ -26,6 +27,7 @@ bool
sc_mutex_init(sc_mutex *mutex) { sc_mutex_init(sc_mutex *mutex) {
SDL_mutex *sdl_mutex = SDL_CreateMutex(); SDL_mutex *sdl_mutex = SDL_CreateMutex();
if (!sdl_mutex) { if (!sdl_mutex) {
LOG_OOM();
return false; return false;
} }
@ -94,6 +96,7 @@ bool
sc_cond_init(sc_cond *cond) { sc_cond_init(sc_cond *cond) {
SDL_cond *sdl_cond = SDL_CreateCond(); SDL_cond *sdl_cond = SDL_CreateCond();
if (!sdl_cond) { if (!sdl_cond) {
LOG_OOM();
return false; return false;
} }

View file

@ -33,7 +33,7 @@ write_header(struct sc_v4l2_sink *vs, const AVPacket *packet) {
uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t)); uint8_t *extradata = av_malloc(packet->size * sizeof(uint8_t));
if (!extradata) { if (!extradata) {
LOGC("Could not allocate extradata"); LOG_OOM();
return false; return false;
} }
@ -163,25 +163,21 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs); bool ok = sc_video_buffer_init(&vs->vb, vs->buffering_time, &cbs, vs);
if (!ok) { if (!ok) {
LOGE("Could not initialize video buffer");
return false; return false;
} }
ok = sc_video_buffer_start(&vs->vb); ok = sc_video_buffer_start(&vs->vb);
if (!ok) { if (!ok) {
LOGE("Could not start video buffer");
goto error_video_buffer_destroy; goto error_video_buffer_destroy;
} }
ok = sc_mutex_init(&vs->mutex); ok = sc_mutex_init(&vs->mutex);
if (!ok) { if (!ok) {
LOGC("Could not create mutex");
goto error_video_buffer_stop_and_join; goto error_video_buffer_stop_and_join;
} }
ok = sc_cond_init(&vs->cond); ok = sc_cond_init(&vs->cond);
if (!ok) { if (!ok) {
LOGC("Could not create cond");
goto error_mutex_destroy; goto error_mutex_destroy;
} }
@ -203,7 +199,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
vs->format_ctx = avformat_alloc_context(); vs->format_ctx = avformat_alloc_context();
if (!vs->format_ctx) { if (!vs->format_ctx) {
LOGE("Could not allocate v4l2 output context"); LOG_OOM();
return false; return false;
} }
@ -215,7 +211,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
#ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL #ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
vs->format_ctx->url = strdup(vs->device_name); vs->format_ctx->url = strdup(vs->device_name);
if (!vs->format_ctx->url) { if (!vs->format_ctx->url) {
LOGE("Could not strdup v4l2 device name"); LOG_OOM();
goto error_avformat_free_context; goto error_avformat_free_context;
} }
#else #else
@ -225,7 +221,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder); AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder);
if (!ostream) { if (!ostream) {
LOGE("Could not allocate new v4l2 stream"); LOG_OOM();
goto error_avformat_free_context; goto error_avformat_free_context;
} }
@ -244,7 +240,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
vs->encoder_ctx = avcodec_alloc_context3(encoder); vs->encoder_ctx = avcodec_alloc_context3(encoder);
if (!vs->encoder_ctx) { if (!vs->encoder_ctx) {
LOGC("Could not allocate codec context for v4l2"); LOG_OOM();
goto error_avio_close; goto error_avio_close;
} }
@ -261,13 +257,13 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
vs->frame = av_frame_alloc(); vs->frame = av_frame_alloc();
if (!vs->frame) { if (!vs->frame) {
LOGE("Could not create v4l2 frame"); LOG_OOM();
goto error_avcodec_close; goto error_avcodec_close;
} }
vs->packet = av_packet_alloc(); vs->packet = av_packet_alloc();
if (!vs->packet) { if (!vs->packet) {
LOGE("Could not allocate packet"); LOG_OOM();
goto error_av_frame_free; goto error_av_frame_free;
} }

View file

@ -14,11 +14,13 @@ static struct sc_video_buffer_frame *
sc_video_buffer_frame_new(const AVFrame *frame) { sc_video_buffer_frame_new(const AVFrame *frame) {
struct sc_video_buffer_frame *vb_frame = malloc(sizeof(*vb_frame)); struct sc_video_buffer_frame *vb_frame = malloc(sizeof(*vb_frame));
if (!vb_frame) { if (!vb_frame) {
LOG_OOM();
return NULL; return NULL;
} }
vb_frame->frame = av_frame_alloc(); vb_frame->frame = av_frame_alloc();
if (!vb_frame->frame) { if (!vb_frame->frame) {
LOG_OOM();
free(vb_frame); free(vb_frame);
return NULL; return NULL;
} }
@ -132,14 +134,12 @@ sc_video_buffer_init(struct sc_video_buffer *vb, sc_tick buffering_time,
if (buffering_time) { if (buffering_time) {
ok = sc_mutex_init(&vb->b.mutex); ok = sc_mutex_init(&vb->b.mutex);
if (!ok) { if (!ok) {
LOGC("Could not create mutex");
sc_frame_buffer_destroy(&vb->fb); sc_frame_buffer_destroy(&vb->fb);
return false; return false;
} }
ok = sc_cond_init(&vb->b.queue_cond); ok = sc_cond_init(&vb->b.queue_cond);
if (!ok) { if (!ok) {
LOGC("Could not create cond");
sc_mutex_destroy(&vb->b.mutex); sc_mutex_destroy(&vb->b.mutex);
sc_frame_buffer_destroy(&vb->fb); sc_frame_buffer_destroy(&vb->fb);
return false; return false;
@ -147,7 +147,6 @@ sc_video_buffer_init(struct sc_video_buffer *vb, sc_tick buffering_time,
ok = sc_cond_init(&vb->b.wait_cond); ok = sc_cond_init(&vb->b.wait_cond);
if (!ok) { if (!ok) {
LOGC("Could not create wait cond");
sc_cond_destroy(&vb->b.queue_cond); sc_cond_destroy(&vb->b.queue_cond);
sc_mutex_destroy(&vb->b.mutex); sc_mutex_destroy(&vb->b.mutex);
sc_frame_buffer_destroy(&vb->fb); sc_frame_buffer_destroy(&vb->fb);
@ -234,7 +233,7 @@ sc_video_buffer_push(struct sc_video_buffer *vb, const AVFrame *frame) {
struct sc_video_buffer_frame *vb_frame = sc_video_buffer_frame_new(frame); struct sc_video_buffer_frame *vb_frame = sc_video_buffer_frame_new(frame);
if (!vb_frame) { if (!vb_frame) {
sc_mutex_unlock(&vb->b.mutex); sc_mutex_unlock(&vb->b.mutex);
LOGE("Could not allocate frame"); LOG_OOM();
return false; return false;
} }