Received: by 10.213.65.68 with SMTP id h4csp37023imn; Mon, 19 Mar 2018 18:45:01 -0700 (PDT) X-Google-Smtp-Source: AG47ELvCCnl2e0RXRKHFaohBYcMKvnpIZsZXwRs42ImdROLyJcwWwp0gIH0bkVZoU7IVsgJd/kjI X-Received: by 10.101.93.138 with SMTP id f10mr10629468pgt.255.1521510301625; Mon, 19 Mar 2018 18:45:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521510301; cv=none; d=google.com; s=arc-20160816; b=mSuvFa+G/1X9J86r7r5a/9D2pJ3ZJQ5Vll0EKB3YHvUv/iV2t2nUtJeNHDz2LEk1Jl dIQP3tOtkDokcC98F7FbUOC+d+IR8h/zraq5mIeqMSMChX3SVPbC3hvCKX+JKlaKFdGq aVk3YLAoIA/PBV5S34S+oexUtuQKz7kvcWrZo196M+RUK+NrwkG1+g0NjbKk+V3raJ48 rB1CodAksn4Ny/xAxNt6mRFIF4L1UmPtZyg3XyC4fW/DRefhxt5PrRHg2puU3WXtzlns 8R3McTgLLUNJvwRGWEQZ1UrZmvkae375gTD/4zLSmZi3P13vIAHSIeWy9mO/SW5EP0Ra r5dw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=1A3ht4p7tUXmPkk6kvcwez2itit9L55gTClLtU3yvn4=; b=jD6fY/5J5X2GpU6qV+RuGdnaJwl3HGbyruAYZVDD1Bll3tZf9KmD1dG4zkV78GCjS1 PDyTI5Qzx5QDlziUlk9NJrgG9lR0PGl5MkbV7XKpZ7CD3xeBPiZ2qQAxs1bpWOffRQ4a 1bFyaYwNl7p3uOvxALAgiIKhs3hStIjCknyQy9Bk1G048sgReucCUytQufg9NLufDU7Y gtdTCAIM5Vk2py2Y4bUMwTdCp3n5IZWfareQrq+DfIWolMZ4LkgAbOPLE0Mu7BjHmfzn tnBjEwooyRub9aX5OG19wSq21E2JSvtv6mzb65sBf8HhdS3hm7oITRGZ7W/bYyf0kMu6 Dbig== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l128si406952pgl.248.2018.03.19.18.44.48; Mon, 19 Mar 2018 18:45:01 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S970410AbeCSSkL (ORCPT + 99 others); Mon, 19 Mar 2018 14:40:11 -0400 Received: from mx-rz-2.rrze.uni-erlangen.de ([131.188.11.21]:34863 "EHLO mx-rz-2.rrze.uni-erlangen.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S970430AbeCSShl (ORCPT ); Mon, 19 Mar 2018 14:37:41 -0400 Received: from mx-rz-2.rrze.uni-erlangen.de (mx-rz-2.rrze.uni-erlangen.de [IPv6:2001:638:a000:1025::15]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx-rz-2.rrze.uni-erlangen.de (Postfix) with ESMTPS id 404lDN0XF0zPkHV; Mon, 19 Mar 2018 19:37:40 +0100 (CET) Authentication-Results: mx-rz-2.rrze.uni-erlangen.de; dkim=none reason="no signature"; dkim-adsp=none (unprotected policy); dkim-atps=neutral X-Quarantine-ID: X-Virus-Scanned: amavisd-new at boeck2.rrze.uni-erlangen.de (RRZE) X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-RRZE-Flag: Not-Spam X-RRZE-Submit-IP: 2003:d5:3db:1800:60b5:55d8:d757:229 Received: from fau.de (p200300D503DB180060B555D8D7570229.dip0.t-ipconnect.de [IPv6:2003:d5:3db:1800:60b5:55d8:d757:229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: U2FsdGVkX18Wu9qntiIjvpNjQF3OHz4kb8w0E4VbXDk=) by smtp-auth.uni-erlangen.de (Postfix) with ESMTPSA id 404lDJ5KMJzPjpJ; Mon, 19 Mar 2018 19:37:36 +0100 (CET) From: Jonas Rabenstein To: Jonas Rabenstein , Scott Bauer , Jonathan Derrick , Jens Axboe Cc: linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Christoph Hellwig Subject: [PATCH v2 02/11] block: sed-opal: unify space check in add_token_* Date: Mon, 19 Mar 2018 19:36:44 +0100 Message-Id: <955fd10854ed68f6925612ae73cdaa4637cfb5e4.1521482295.git.jonas.rabenstein@studium.uni-erlangen.de> X-Mailer: git-send-email 2.16.1 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org All add_token_* functions have a common set of conditions that have to be checked. Use a common function for those checks in order to avoid different behaviour as well as code duplication. Signed-off-by: Jonas Rabenstein diff --git a/block/sed-opal.c b/block/sed-opal.c index 5aa41744b8f1..771b4cfff95c 100644 --- a/block/sed-opal.c +++ b/block/sed-opal.c @@ -510,15 +510,24 @@ static int opal_discovery0(struct opal_dev *dev, void *data) return opal_discovery0_end(dev); } -static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok) +static bool can_add(int *err, struct opal_dev *cmd, size_t len) { if (*err) - return; - if (cmd->pos >= IO_BUFFER_LENGTH - 1) { - pr_debug("Error adding u8: end of buffer.\n"); + return false; + + if (len > IO_BUFFER_LENGTH || cmd->pos >= IO_BUFFER_LENGTH - len) { + pr_debug("Error adding %zu bytes: end of buffer.\n", len); *err = -ERANGE; - return; + return false; } + + return true; +} + +static void add_token_u8(int *err, struct opal_dev *cmd, u8 tok) +{ + if (!can_add(err, cmd, 1)) + return; cmd->cmd[cmd->pos++] = tok; } @@ -563,9 +572,8 @@ static void add_token_u64(int *err, struct opal_dev *cmd, u64 number) msb = fls64(number); len = DIV_ROUND_UP(msb, 8); - if (cmd->pos >= IO_BUFFER_LENGTH - len - 1) { + if (!can_add(err, cmd, len + 1)) { pr_debug("Error adding u64: end of buffer.\n"); - *err = -ERANGE; return; } add_short_atom_header(cmd, false, false, len); @@ -587,9 +595,8 @@ static void add_token_bytestring(int *err, struct opal_dev *cmd, is_short_atom = false; } - if (len >= IO_BUFFER_LENGTH - cmd->pos - header_len) { + if (!can_add(err, cmd, header_len + len)) { pr_debug("Error adding bytestring: end of buffer.\n"); - *err = -ERANGE; return; } -- 2.16.1