From 7db28fad846288b5e23da3bf9705d116dbb4233c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Ekstr=C3=B6m?= Date: Fri, 31 Jan 2014 17:26:09 +0200 Subject: [PATCH] encmain: Make the main function more resilient to allocation failures Application will no longer crash if config or encoder_control allocation fails. --- src/encmain.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/encmain.c b/src/encmain.c index 49d36311..5ebaef8d 100644 --- a/src/encmain.c +++ b/src/encmain.c @@ -73,6 +73,8 @@ int main(int argc, char *argv[]) FILE *recout = fopen("encrec_832x480_60.yuv","wb"); //!< reconstructed YUV output (only on debug mode) #endif encoder_control *encoder = (encoder_control*)malloc(sizeof(encoder_control)); + if (!encoder) + fprintf(stderr, "Failed to allocate the encoder_control object!\n"); // Windows needs all of the standard in/outputs to be set to _O_BINARY #ifdef _WIN32 @@ -85,7 +87,7 @@ int main(int argc, char *argv[]) cfg = config_alloc(); // If problem with configuration, print banner and shutdown - if (!config_init(cfg) || !config_read(cfg,argc,argv)) { + if (!cfg || !encoder || !config_init(cfg) || !config_read(cfg,argc,argv)) { fprintf(stderr, "/***********************************************/\r\n"); fprintf(stderr, " * Kvazaar HEVC Encoder v. " VERSION_STRING "*\r\n"); fprintf(stderr, " * Tampere University of Technology 2014 *\r\n"); @@ -101,7 +103,9 @@ int main(int argc, char *argv[]) fprintf(stderr, " 1: all pictures are intra\r\n"); fprintf(stderr, " 2-N: every Nth picture is intra\r\n"); - config_destroy(cfg); + if (cfg) + config_destroy(cfg); + free(encoder); return EXIT_FAILURE; }