From patchwork Sun Aug 7 18:00:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 17016 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id BF2D4C3272 for ; Sun, 7 Aug 2022 18:01:15 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 17F7D63332; Sun, 7 Aug 2022 20:01:15 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org; s=mail; t=1659895275; bh=wYfz9+aPgxCtDCWKhZd5OrCfMyu7hutN/qxqf3+DEpU=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=VRjVUOlcIjhi4k5rM4HcSlp/uKuHVMpUzYrPrzAwjPSglhKsGSFL46OrTZ4ZAhFah O79wytaCbWSG1I3xCKeTBj59GeeHm/n6DrV0oa1FKo2VXq0CX1CAtCkuetpNK3Rp09 ddwC5siT5J+3l+WAolCstO11zh8c9enlVgrVq2y3TZwWBWM8k4Q1TcoYJVVXSLkGRA sQ1wApLE5sl+WgYpAEkTtyUD6fXfuuximGDvCultqWyuHdebzT8eeF+/pH6HG/b1E8 Bir7OrE5RslbCnIZyeCOxzZVGzAJ4eGFbhOLwoonD26EP7fosAAmARxmPY1p4ma8xX /Zlrb2PqZsEEA== Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2CFC16332E for ; Sun, 7 Aug 2022 20:01:13 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="eVY9UIaR"; dkim-atps=neutral Received: from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id A01C69DA; Sun, 7 Aug 2022 20:01:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1659895272; bh=wYfz9+aPgxCtDCWKhZd5OrCfMyu7hutN/qxqf3+DEpU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eVY9UIaRCeDRCabs6v5/kwwtGFVygPHDVjzEWAfP9EYzdUhd/8Ju7U/nI8C39Xntz JSjP95721ly7A0xmv2Jxs4piEfys+pNcKaMHTqL1SZBzNbF4E7CRff+hGV0jBLexm4 oD3CTGtW1QlfCcGYnKGohVenV6eqkS1LPlk13uEE= To: libcamera-devel@lists.libcamera.org Date: Sun, 7 Aug 2022 21:00:59 +0300 Message-Id: <20220807180100.396-3-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220807180100.396-1-laurent.pinchart@ideasonboard.com> References: <20220807180100.396-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 2/3] cam: kms_sink: Make lifetime management of DRM request safer X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Laurent Pinchart via libcamera-devel From: Laurent Pinchart Reply-To: Laurent Pinchart Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The drmRequest is KMSSink::processRequest() is created as a naked pointer, passed to the constructor of the KMSSink::Request object that stores it in a std::unique_ptr<>, and used later in the function. The current implementation is safe, but could be prone to both memory leaks and use-after-free bugs if modified. Improve it by replacing the naked pointer with a std::unique_ptr<>. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham Reviewed-by: Eric Curtin --- src/cam/kms_sink.cpp | 7 ++++--- src/cam/kms_sink.h | 5 +++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/cam/kms_sink.cpp b/src/cam/kms_sink.cpp index 37a3bd50a2bf..16435ede6b6a 100644 --- a/src/cam/kms_sink.cpp +++ b/src/cam/kms_sink.cpp @@ -301,7 +301,8 @@ bool KMSSink::processRequest(libcamera::Request *camRequest) DRM::FrameBuffer *drmBuffer = iter->second.get(); unsigned int flags = DRM::AtomicRequest::FlagAsync; - DRM::AtomicRequest *drmRequest = new DRM::AtomicRequest(&dev_); + std::unique_ptr drmRequest = + std::make_unique(&dev_); drmRequest->addProperty(plane_, "FB_ID", drmBuffer->id()); if (!active_ && !queued_) { @@ -324,12 +325,12 @@ bool KMSSink::processRequest(libcamera::Request *camRequest) flags |= DRM::AtomicRequest::FlagAllowModeset; } - pending_ = std::make_unique(drmRequest, camRequest); + pending_ = std::make_unique(std::move(drmRequest), camRequest); std::lock_guard lock(lock_); if (!queued_) { - int ret = drmRequest->commit(flags); + int ret = pending_->drmRequest_->commit(flags); if (ret < 0) { std::cerr << "Failed to commit atomic request: " diff --git a/src/cam/kms_sink.h b/src/cam/kms_sink.h index 4a0a872cb653..8f5f08667cea 100644 --- a/src/cam/kms_sink.h +++ b/src/cam/kms_sink.h @@ -38,8 +38,9 @@ private: class Request { public: - Request(DRM::AtomicRequest *drmRequest, libcamera::Request *camRequest) - : drmRequest_(drmRequest), camRequest_(camRequest) + Request(std::unique_ptr drmRequest, + libcamera::Request *camRequest) + : drmRequest_(std::move(drmRequest)), camRequest_(camRequest) { }