From patchwork Thu May 31 22:23:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephan Holljes X-Patchwork-Id: 9188 Delivered-To: ffmpegpatchwork@gmail.com Received: by 2002:a02:11c:0:0:0:0:0 with SMTP id c28-v6csp161525jad; Thu, 31 May 2018 15:25:06 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIyudAC+AE8N4eEW10ada9+61JTEnb4fnPPhjdEzNBJlfCgIRJrOBxv6OLgVFa4+dd5LQjS X-Received: by 2002:a1c:b70b:: with SMTP id h11-v6mr966784wmf.1.1527805506698; Thu, 31 May 2018 15:25:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527805506; cv=none; d=google.com; s=arc-20160816; b=r6IuyTNT++N4kXQahjRppBDRw5g1HuvYxvXDSK1RVBnXDfHCbeSUNzuDhr+lOMZ8OZ b5ydwSuPpVGhGZsu0RHJQtZhLbPf0IENypk3t8EuLhG86qxOvG2HbNDv1cTRSo5eRwF6 I0pqxHg5UhVANC+3VTKH1dahL6W6bY0oChTj8W0NZBEDCVlrgvdRZmWOTPmenQ8KvwOR onHJjfJPDqNM8/nlW7nX65wy2LoIZh0bL48yuNtozXacyok/uJkIXxXqLNglMxuivCqO cCLYvMKUYKefSsqLBs6kjwaHghqvMJi5WSzSZZJt5ItWK/6jCU6JpzlNpfs1hCv4VxPt DyGg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:references:in-reply-to:message-id:date :to:from:dkim-signature:delivered-to:arc-authentication-results; bh=EfGS+71+VBgnw8Gax69D5VxEh0N+e1xDqlh+fq93tg8=; b=HDbDERCdVJVDu3Ce+jo1irDUI4lJmb6iHT2aUzxQpyfM9+nnomniwqd5TjYxYeLz+X rmsBid2QyaZO8cf6qapwtbYEtKsQsvqr9pHoIR6/KND72LK92V4G0NdMx6swx7OVPVgG Eao6QSXoOvpcscWWss6BwqiH+vtOVykq474DyFm5UGNZc0mc4A0oFCOuxpmHBqOwFcqV FKqk8wh3WWoSCbLUBfvJQkUk5RclfoIY/INhnY4bzYBrno59JAlxwfeiVWpNp7Uq9ge2 KjVMh62iYZ26ldzRKNqNHB6uH0mgbEP/y0OwPBCqFVYSo6Onbk5+T1BnKzG3Wxvm5gCP nCmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=s7P5cmFW; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id x80-v6si17462755wrb.354.2018.05.31.15.25.06; Thu, 31 May 2018 15:25:06 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@googlemail.com header.s=20161025 header.b=s7P5cmFW; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=googlemail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9DF8A68A49E; Fri, 1 Jun 2018 01:23:52 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm0-f50.google.com (mail-wm0-f50.google.com [74.125.82.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 674E868A48A for ; Fri, 1 Jun 2018 01:23:45 +0300 (EEST) Received: by mail-wm0-f50.google.com with SMTP id t11-v6so56696694wmt.0 for ; Thu, 31 May 2018 15:24:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=2JZOhlFDgoDXUeCyAh6rXauNDHXODH+KuNMKHVENjpw=; b=s7P5cmFWqOQ2dz6cpTIvgpqQYqEkMDSAQ7O+rLigSojp+k1iu7YjkKsEyX9dbsOXDW 4bfqDroGQFbbYu2HMqq0zyVhhEdtDt8D6OId+WqylsuomcmB9MCnsIqEzbbHitRWCOdl EZS+5E9X3/6bI6SpqrTDczdcUl+wBhH1ONazoIYLevPN1UpHgeUmlOJfLm2ZuDoK6fb4 AAPZ/8ZO4PZbkK6dlhQoCB8C8PsnYfl8Ij/H4XGUPI1qwOJ3Z97Qb4WDcCPbkNkSDRsq WQDE0lIwAIyzlLBe7KPUjI0MaLvAFP0sI8Zwh1WtlGQXEa7qziwe4UQ9WmgE+8xO8B3p 2dMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=2JZOhlFDgoDXUeCyAh6rXauNDHXODH+KuNMKHVENjpw=; b=o0uavmNT1KH9zM/55xgFIoVNHHq8sFrhBs4htWMDtt/Fm0XXaWfISJECPNpm2hJjUl A0WOPhzvXrLMub6B9SfAHNfmleMsz4XiIptDLRfvKqRByOw0d8VYTsaL7KsCFarulmYI 0hetHkMZeKmykHG5XPjrAbEf+TMKqXzhODTRvb5EYr+w2jhqoTUKijjK1tLv4hk7sLzr 5i2TFf9RAztlSGSgeC0+dIMByvVDMVnzClarZ03s4gFaRH7KecAWvZ9vXVR9KzJsZ81d CV1jo2dIuOMDc++TCX0X05ewxVJ6PSLeFpkkzOLqFGms90X8i+32JydbSE8sjXBSlF9A FADQ== X-Gm-Message-State: APt69E1aeD3gGYsAAScT6T4vDCRfJHLrUOIfvsXhxJQJcvvoE6R2tfUk I2fHQAaRUtd2eItSfvO+VUTdvw== X-Received: by 2002:a1c:3b54:: with SMTP id i81-v6mr909156wma.122.1527805469783; Thu, 31 May 2018 15:24:29 -0700 (PDT) Received: from localhost.localdomain ([46.5.2.0]) by smtp.gmail.com with ESMTPSA id b72-v6sm705017wmf.11.2018.05.31.15.24.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 31 May 2018 15:24:29 -0700 (PDT) From: Stephan Holljes To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Jun 2018 00:23:57 +0200 Message-Id: <20180531222415.24966-5-klaxa1337@googlemail.com> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180531222415.24966-1-klaxa1337@googlemail.com> References: <20180528182711.3221-1-klaxa1337@googlemail.com> <20180531222415.24966-1-klaxa1337@googlemail.com> Subject: [FFmpeg-devel] [PATCH 04/22] ffserver.c: Check allocations X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Stephan Holljes MIME-Version: 1.0 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Signed-off-by: Stephan Holljes --- ffserver.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 81 insertions(+), 6 deletions(-) diff --git a/ffserver.c b/ffserver.c index fe84b2e..ddb3e6f 100644 --- a/ffserver.c +++ b/ffserver.c @@ -223,12 +223,25 @@ void write_segment(struct Client *c) } avio_buffer = (unsigned char*) av_malloc(AV_BUFSIZE); + if (!avio_buffer) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate avio_buffer\n"); + avformat_free_context(fmt_ctx); + client_disconnect(c, 0); + return; + } avio_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 0, &info, &segment_read, NULL, NULL); - + if (!avio_ctx) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate avio_ctx\n"); + avformat_free_context(fmt_ctx); + av_free(avio_buffer); + client_disconnect(c, 0); + return; + } fmt_ctx->pb = avio_ctx; ret = avformat_open_input(&fmt_ctx, NULL, seg->ifmt, NULL); if (ret < 0) { av_log(avio_ctx, AV_LOG_ERROR, "Could not open input\n"); + avformat_close_input(&fmt_ctx); av_free(avio_ctx->buffer); avio_context_free(&avio_ctx); client_disconnect(c, 0); @@ -238,6 +251,7 @@ void write_segment(struct Client *c) ret = avformat_find_stream_info(fmt_ctx, NULL); if (ret < 0) { av_log(fmt_ctx, AV_LOG_ERROR, "Could not find stream information\n"); + avformat_close_input(&fmt_ctx); av_free(avio_ctx->buffer); avio_context_free(&avio_ctx); client_disconnect(c, 0); @@ -270,9 +284,8 @@ void write_segment(struct Client *c) return; } } - avformat_close_input(&fmt_ctx); av_free(avio_ctx->buffer); - avformat_free_context(fmt_ctx); + avformat_close_input(&fmt_ctx); avio_context_free(&avio_ctx); pthread_mutex_lock(&c->buffer_lock); av_fifo_drain(c->buffer, sizeof(struct Segment*)); @@ -366,13 +379,25 @@ void *accept_thread(void *arg) } avio_buffer = av_malloc(AV_BUFSIZE); + if (!avio_buffer) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate output format context.\n"); + publisher_cancel_reserve(pub); + info->httpd->close(server, client); + continue; + } ffinfo = av_malloc(sizeof(struct FFServerInfo)); + if (!ffinfo) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate FFServerInfo struct.\n"); + publisher_cancel_reserve(pub); + info->httpd->close(server, client); + continue; + } ffinfo->httpd = info->httpd; ffinfo->client = client; ffinfo->server = server; client_ctx = avio_alloc_context(avio_buffer, AV_BUFSIZE, 1, ffinfo, NULL, &ffserver_write, NULL); if (!client_ctx) { - av_log(client, AV_LOG_ERROR, "Could not allocate output format context.\n"); + av_log(NULL, AV_LOG_ERROR, "Could not allocate output format context.\n"); publisher_cancel_reserve(pub); info->httpd->close(server, client); av_free(client_ctx->buffer); @@ -382,7 +407,7 @@ void *accept_thread(void *arg) } avformat_alloc_output_context2(&ofmt_ctx, NULL, "matroska", NULL); if (!ofmt_ctx) { - av_log(client, AV_LOG_ERROR, "Could not allocate output format context.\n"); + av_log(client_ctx, AV_LOG_ERROR, "Could not allocate output format context.\n"); publisher_cancel_reserve(pub); info->httpd->close(server, client); avformat_free_context(ofmt_ctx); @@ -507,7 +532,16 @@ void *run_server(void *arg) { pthread_t **w_threads_p; pubs = av_mallocz_array(config->nb_streams, sizeof(struct PublisherContext*)); + if (!pubs) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate publishers\n"); + return NULL; + } ifmt_ctxs = av_mallocz_array(config->nb_streams, sizeof(AVFormatContext*)); + if (!ifmt_ctxs) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate input format contexts.\n"); + av_free(pubs); + return NULL; + } av_log_set_level(AV_LOG_INFO); @@ -518,9 +552,39 @@ void *run_server(void *arg) { ainfo.config = config; rinfos = av_mallocz_array(config->nb_streams, sizeof(struct ReadInfo)); + if (!rinfos) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate read infos.\n"); + av_free(pubs); + av_free(ifmt_ctxs); + return NULL; + } winfos_p = av_mallocz_array(config->nb_streams, sizeof(struct WriteInfo*)); + if (!winfos_p) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate write info pointers.\n"); + av_free(pubs); + av_free(ifmt_ctxs); + av_free(rinfos); + return NULL; + } r_threads = av_mallocz_array(config->nb_streams, sizeof(pthread_t)); + if (!r_threads) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate read thread handles.\n"); + av_free(pubs); + av_free(ifmt_ctxs); + av_free(rinfos); + av_free(winfos_p); + return NULL; + } w_threads_p = av_mallocz_array(config->nb_streams, sizeof(pthread_t*)); + if (!w_threads_p) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate write thread handle pointers.\n"); + av_free(pubs); + av_free(ifmt_ctxs); + av_free(rinfos); + av_free(winfos_p); + av_free(r_threads); + return NULL; + } for (stream_index = 0; stream_index < config->nb_streams; stream_index++) { struct PublisherContext *pub = NULL; @@ -547,8 +611,15 @@ void *run_server(void *arg) { rinfos[stream_index] = rinfo; w_threads = av_mallocz_array(pub->nb_threads, sizeof(pthread_t)); + if (!w_threads) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate write thread handles.\n"); + continue; + } winfos = av_mallocz_array(pub->nb_threads, sizeof(struct WriteInfo)); - + if (!winfos) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate write infos.\n"); + continue; + } w_threads_p[stream_index] = w_threads; winfos_p[stream_index] = winfos; @@ -620,6 +691,10 @@ int main(int argc, char *argv[]) return 1; } server_threads = av_mallocz_array(nb_configs, sizeof(pthread_t)); + if (!server_threads) { + av_log(NULL, AV_LOG_ERROR, "Could not allocate server thread handles.\n"); + return AVERROR(ENOMEM); + } for (i = 0; i < nb_configs; i++) { config_dump(configs + i, stderr); ret = pthread_create(&server_threads[i], NULL, run_server, configs + i);