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
char *buf = malloc(MAX_COMMAND_STRING_LEN);
if (!buf) {
LOGE("Failed to execute (could not allocate error message)");
LOG_OOM();
LOGE("Failed to execute");
return;
}
@ -118,6 +119,7 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
const char **argv = malloc((len + 4) * sizeof(*argv));
if (!argv) {
LOG_OOM();
return SC_PROCESS_NONE;
}

View file

@ -4,6 +4,7 @@
#include <stdio.h>
#include "aoa_hid.h"
#include "util/log.h"
// See <https://source.android.com/devices/accessories/aoa2#hid-support>.
#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;
char *buffer = malloc(buffer_size);
if (!buffer) {
LOG_OOM();
return;
}
for (unsigned i = 0; i < event->size; ++i) {

View file

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

View file

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

View file

@ -24,7 +24,7 @@ device_msg_deserialize(const unsigned char *buf, size_t len,
}
char *text = malloc(clipboard_len + 1);
if (!text) {
LOGW("Could not allocate text for clipboard");
LOG_OOM();
return -1;
}
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);
if (!ok) {
LOGE("Could not create intr");
sc_cond_destroy(&file_handler->event_cond);
sc_mutex_destroy(&file_handler->mutex);
return false;
@ -42,7 +41,7 @@ file_handler_init(struct file_handler *file_handler, const char *serial,
file_handler->serial = strdup(serial);
if (!file_handler->serial) {
LOGE("Could not strdup serial");
LOG_OOM();
sc_intr_destroy(&file_handler->intr);
sc_cond_destroy(&file_handler->event_cond);
sc_mutex_destroy(&file_handler->mutex);

View file

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

View file

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

View file

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

View file

@ -34,11 +34,13 @@ static struct record_packet *
record_packet_new(const AVPacket *packet) {
struct record_packet *rec = malloc(sizeof(*rec));
if (!rec) {
LOG_OOM();
return NULL;
}
rec->packet = av_packet_alloc();
if (!rec->packet) {
LOG_OOM();
free(rec);
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));
if (!extradata) {
LOGC("Could not allocate extradata");
LOG_OOM();
return false;
}
@ -228,13 +230,11 @@ static bool
recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
bool ok = sc_mutex_init(&recorder->mutex);
if (!ok) {
LOGC("Could not create mutex");
return false;
}
ok = sc_cond_init(&recorder->queue_cond);
if (!ok) {
LOGC("Could not create cond");
goto error_mutex_destroy;
}
@ -254,7 +254,7 @@ recorder_open(struct recorder *recorder, const AVCodec *input_codec) {
recorder->ctx = avformat_alloc_context();
if (!recorder->ctx) {
LOGE("Could not allocate output context");
LOG_OOM();
goto error_cond_destroy;
}
@ -338,7 +338,7 @@ recorder_push(struct recorder *recorder, const AVPacket *packet) {
struct record_packet *rec = record_packet_new(packet);
if (!rec) {
LOGC("Could not allocate record packet");
LOG_OOM();
sc_mutex_unlock(&recorder->mutex);
return false;
}
@ -375,7 +375,7 @@ recorder_init(struct recorder *recorder,
struct sc_size declared_frame_size) {
recorder->filename = strdup(filename);
if (!recorder->filename) {
LOGE("Could not strdup filename");
LOG_OOM();
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);
char *local_fmt = malloc(fmt_len + 10);
if (!local_fmt) {
LOGC("Could not allocate string");
LOG_OOM();
return;
}
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,
screen);
if (!ok) {
LOGE("Could not initialize video buffer");
return false;
}
ok = sc_video_buffer_start(&screen->vb);
if (!ok) {
LOGE("Could not start video_buffer");
goto error_destroy_video_buffer;
}
if (!fps_counter_init(&screen->fps_counter)) {
LOGE("Could not initialize FPS counter");
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();
if (!screen->frame) {
LOGC("Could not create screen frame");
LOG_OOM();
goto error_destroy_texture;
}

View file

@ -34,7 +34,7 @@ get_server_path(void) {
char *server_path = strdup(server_path_env);
#endif
if (!server_path) {
LOGE("Could not allocate memory");
LOG_OOM();
return NULL;
}
LOGD("Using SCRCPY_SERVER_PATH: %s", server_path);
@ -45,7 +45,7 @@ get_server_path(void) {
LOGD("Using server: " SC_SERVER_PATH_DEFAULT);
char *server_path = strdup(SC_SERVER_PATH_DEFAULT);
if (!server_path) {
LOGE("Could not allocate memory");
LOG_OOM();
return NULL;
}
#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) {
bool ok = sc_server_params_copy(&server->params, params);
if (!ok) {
LOGE("Could not copy server params");
LOG_OOM();
return false;
}
ok = sc_mutex_init(&server->mutex);
if (!ok) {
LOGE("Could not create server mutex");
sc_server_params_destroy(&server->params);
return false;
}
ok = sc_cond_init(&server->cond_stopped);
if (!ok) {
LOGE("Could not create server cond_stopped");
sc_mutex_destroy(&server->mutex);
sc_server_params_destroy(&server->params);
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);
if (!ok) {
LOGE("Could not create intr");
sc_cond_destroy(&server->cond_stopped);
sc_mutex_destroy(&server->mutex);
sc_server_params_destroy(&server->params);

View file

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

View file

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

View file

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

View file

@ -100,6 +100,7 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
lpAttributeList = malloc(size);
if (!lpAttributeList) {
LOG_OOM();
goto error_close_stderr;
}
@ -133,13 +134,14 @@ sc_process_execute_p(const char *const argv[], HANDLE *handle,
char *cmd = malloc(CMD_MAX_LEN);
if (!cmd || !build_cmd(cmd, CMD_MAX_LEN, argv)) {
LOG_OOM();
goto error_free_attribute_list;
}
wchar_t *wide = sc_str_to_wchars(cmd);
free(cmd);
if (!wide) {
LOGC("Could not allocate wide char string");
LOG_OOM();
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'
char *file_path = malloc(len);
if (!file_path) {
LOGE("Could not alloc path");
LOG_OOM();
free(executable_path);
return NULL;
}

View file

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

View file

@ -7,6 +7,9 @@
#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 LOGD(...) SDL_LogDebug(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 LOGC(...) SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, __VA_ARGS__)
#define LOG_OOM() \
LOGC("OOM: %s:%d %s()", __FILE__, __LINE__, __func__)
void
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));
if (!socket) {
LOG_OOM();
closesocket(sock);
return SC_SOCKET_NONE;
}

View file

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

View file

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

View file

@ -10,6 +10,7 @@ sc_thread_create(sc_thread *thread, sc_thread_fn fn, const char *name,
void *userdata) {
SDL_Thread *sdl_thread = SDL_CreateThread(fn, name, userdata);
if (!sdl_thread) {
LOG_OOM();
return false;
}
@ -26,6 +27,7 @@ bool
sc_mutex_init(sc_mutex *mutex) {
SDL_mutex *sdl_mutex = SDL_CreateMutex();
if (!sdl_mutex) {
LOG_OOM();
return false;
}
@ -94,6 +96,7 @@ bool
sc_cond_init(sc_cond *cond) {
SDL_cond *sdl_cond = SDL_CreateCond();
if (!sdl_cond) {
LOG_OOM();
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));
if (!extradata) {
LOGC("Could not allocate extradata");
LOG_OOM();
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);
if (!ok) {
LOGE("Could not initialize video buffer");
return false;
}
ok = sc_video_buffer_start(&vs->vb);
if (!ok) {
LOGE("Could not start video buffer");
goto error_video_buffer_destroy;
}
ok = sc_mutex_init(&vs->mutex);
if (!ok) {
LOGC("Could not create mutex");
goto error_video_buffer_stop_and_join;
}
ok = sc_cond_init(&vs->cond);
if (!ok) {
LOGC("Could not create cond");
goto error_mutex_destroy;
}
@ -203,7 +199,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
vs->format_ctx = avformat_alloc_context();
if (!vs->format_ctx) {
LOGE("Could not allocate v4l2 output context");
LOG_OOM();
return false;
}
@ -215,7 +211,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
#ifdef SCRCPY_LAVF_HAS_AVFORMATCONTEXT_URL
vs->format_ctx->url = strdup(vs->device_name);
if (!vs->format_ctx->url) {
LOGE("Could not strdup v4l2 device name");
LOG_OOM();
goto error_avformat_free_context;
}
#else
@ -225,7 +221,7 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
AVStream *ostream = avformat_new_stream(vs->format_ctx, encoder);
if (!ostream) {
LOGE("Could not allocate new v4l2 stream");
LOG_OOM();
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);
if (!vs->encoder_ctx) {
LOGC("Could not allocate codec context for v4l2");
LOG_OOM();
goto error_avio_close;
}
@ -261,13 +257,13 @@ sc_v4l2_sink_open(struct sc_v4l2_sink *vs) {
vs->frame = av_frame_alloc();
if (!vs->frame) {
LOGE("Could not create v4l2 frame");
LOG_OOM();
goto error_avcodec_close;
}
vs->packet = av_packet_alloc();
if (!vs->packet) {
LOGE("Could not allocate packet");
LOG_OOM();
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) {
struct sc_video_buffer_frame *vb_frame = malloc(sizeof(*vb_frame));
if (!vb_frame) {
LOG_OOM();
return NULL;
}
vb_frame->frame = av_frame_alloc();
if (!vb_frame->frame) {
LOG_OOM();
free(vb_frame);
return NULL;
}
@ -132,14 +134,12 @@ sc_video_buffer_init(struct sc_video_buffer *vb, sc_tick buffering_time,
if (buffering_time) {
ok = sc_mutex_init(&vb->b.mutex);
if (!ok) {
LOGC("Could not create mutex");
sc_frame_buffer_destroy(&vb->fb);
return false;
}
ok = sc_cond_init(&vb->b.queue_cond);
if (!ok) {
LOGC("Could not create cond");
sc_mutex_destroy(&vb->b.mutex);
sc_frame_buffer_destroy(&vb->fb);
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);
if (!ok) {
LOGC("Could not create wait cond");
sc_cond_destroy(&vb->b.queue_cond);
sc_mutex_destroy(&vb->b.mutex);
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);
if (!vb_frame) {
sc_mutex_unlock(&vb->b.mutex);
LOGE("Could not allocate frame");
LOG_OOM();
return false;
}