Received: by 10.213.65.68 with SMTP id h4csp341893imn; Tue, 13 Mar 2018 06:13:41 -0700 (PDT) X-Google-Smtp-Source: AG47ELvq+Mr0j2hWlNNhKDN+bQU7/tzGyDJ0jilFuk32HX8SkrJ+dJLinCDh4hHGr3buP0FLg4Bt X-Received: by 10.98.13.211 with SMTP id 80mr580205pfn.69.1520946821251; Tue, 13 Mar 2018 06:13:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1520946821; cv=none; d=google.com; s=arc-20160816; b=WtkugYSEGAST0vFFnFh2Ca7Gi9siApkjDD/XWmOLqx0Tzifzc6rwKxnuYQE5if4pNx s4WxGQYg7snuefVIlnZZMOi0Kgi1RUMD82Q/csVvFQ+nl7B4UUd5UN9xXL3FNwxNpWof q1bPUMCktJeRqR0hktwW0whEfOFPab6D8i1qrDpPkL/OX7OG7kwXps6gUdpJ25+Llt2I RA77WcPDjdE7RBQElJhPVUGCMWS1Z29jKBQxIvWNI6gII9exog4PQmrH7eb/GMu1RmDM W8WzX0G5MHIdnHdmUAIdrQynaFLnebRfNlh5Vdfo1SdX50C9v/ePlV3nLcAihlWFJbfH dHRA== 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=N0fU3lF0GwQ2g3muKhgE2gCjJJ3DQ/zYtDbTO2ksm9E=; b=mX+t5yGLlRuN0ZtJ/nDCQeH0iBJ/OPHYJF/7cUimmPqTgzYftr9ltDl2V2HNK6hckc a8kp4o1RIvLwK2jqVUT8PCRJUNMc5K+zBlFJig2ZoL0euU+pCsuqoy+7yi/JfdRCOTSn bSyT4a2muBGTp2TF97fVDYYA4Dpk9dasY/Z4GjshnXSy3MNlQF11+GD7mMmut/T/8MTv MD0ZXoTSRjNw8TqlHXV8UfM3HMyrZnqeS9jrQcaBWrwsv0o+PRQ10GtDNnYbjfLLqYwR H5QxOx8MsNFs6Bzkr/GjrHUEQuN2NEtYokpj8+Mt4xpvXGKk6GeDmbEWy51+3Gvp6C/0 lH3Q== 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 y86si127445pfi.19.2018.03.13.06.13.26; Tue, 13 Mar 2018 06:13:41 -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 S1752503AbeCMNJi (ORCPT + 99 others); Tue, 13 Mar 2018 09:09:38 -0400 Received: from mx-rz-2.rrze.uni-erlangen.de ([131.188.11.21]:40317 "EHLO mx-rz-2.rrze.uni-erlangen.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932484AbeCMNJa (ORCPT ); Tue, 13 Mar 2018 09:09:30 -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 400wDS6qgQzPkDS; Tue, 13 Mar 2018 14:09:28 +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: <9mIWoGAQP-yN> X-Virus-Scanned: amavisd-new at boeck4.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:3c2:e00:dbd1:5c46:f440:b630 Received: from fau.de (p200300D503C20E00DBD15C46F440B630.dip0.t-ipconnect.de [IPv6:2003:d5:3c2:e00:dbd1:5c46:f440:b630]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: U2FsdGVkX1+ZY8XR6wzpvRvK8Agx/s6JLoQ+Up8pzwQ=) by smtp-auth.uni-erlangen.de (Postfix) with ESMTPSA id 400wDP4Z9rzPlQG; Tue, 13 Mar 2018 14:09:25 +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 Subject: [PATCH 2/8] block: sed-opal: unify space check in add_token_* Date: Tue, 13 Mar 2018 14:08:55 +0100 Message-Id: <946b37d9bba3d72c6a5e616452976fa7889b3e82.1520946114.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. Signed-off-by: Jonas Rabenstein --- block/sed-opal.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/block/sed-opal.c b/block/sed-opal.c index f9d94ce64b08..a228a13f0a08 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; } @@ -564,9 +573,8 @@ static void add_token_u64(int *err, struct opal_dev *cmd, u64 number) msb = fls(number); len = DIV_ROUND_UP(msb, 4); - 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); @@ -590,9 +598,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