Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp7832347ybh; Fri, 9 Aug 2019 00:22:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqyfocO017hBZ41EKWLh/lHpncJxjGZm/knqY1sHDmfxBRfW2vx2FGtKOeOeBxgg1bsl1fXd X-Received: by 2002:a17:902:524:: with SMTP id 33mr5143527plf.27.1565335327961; Fri, 09 Aug 2019 00:22:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565335327; cv=none; d=google.com; s=arc-20160816; b=rgPdl0d0VwpfyFlvR0txQI7CVB8vD9vF0Nse+jcvf1nSmLZ42BV8XgoPiAjXleTI5b YKyCF8GxBpMEtBxflpGf1SHeDBn8wR8Aba8EukiI0PMbI9sv8ZJGqVaiQcXylhAxwkSY Aom756d1ctO95qvUGEHtRCQ55weTzKFJF/iALVZE5389oMXmtzqS8f7IPgUdGstTnXda B166PjbrSReYVEUcgxdXIL1URlJFOhWlm5ftupLjvhqgfX+rNb1qqN0uZb5SbqwtgmP5 niJRh3iIrkMKjDqyvSwHS4vUn5EiD+6cxN0h/VzbXpcK1Uau6r+wRwvHeuVODzCLYBko mfyw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:content-transfer-encoding:mime-version :message-id:date:subject:cc:from:dkim-signature; bh=a5eAdforJPfI5AutgpY5E1bO5+BesI2S6rA/D6o6CCg=; b=BhR7ywUu1iNJO4cNI0i82iGmWc84l4D5otrlnLRyu0hr/wxbPyjuicakxZLCv78OuW ru8AzVq7YgVpCC93wu2n8ryo61IxcDM0u6sSgUgyRaq1qF6Nz8Ota3Lqg9ea2Li+vzGO 0xMQEQfpdIKE9Q4jryIJwREVzkEbAIJfxKuPn1umghlOX96DVYXijoZloNUgIwd94jKY rI8OHtcFxOD+Scs6yyZQbPgM0mKrqCQwp+gzKcNUW+tM1lZmo5t4yfz0N7UQ4DaqV54k GGqs8Cd4b0cM0YlEx44WmMTfWmylEAmByz6JR2W85d4u55xCoE1LMGMSQD1NM4hjGJsc QcWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=mDpqdxWD; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s132si54014047pfc.244.2019.08.09.00.21.52; Fri, 09 Aug 2019 00:22:07 -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; dkim=fail header.i=@gmail.com header.s=20161025 header.b=mDpqdxWD; 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; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2405764AbfHIHKk (ORCPT + 99 others); Fri, 9 Aug 2019 03:10:40 -0400 Received: from mail-pg1-f193.google.com ([209.85.215.193]:34008 "EHLO mail-pg1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405700AbfHIHKk (ORCPT ); Fri, 9 Aug 2019 03:10:40 -0400 Received: by mail-pg1-f193.google.com with SMTP id n9so39176644pgc.1 for ; Fri, 09 Aug 2019 00:10:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=a5eAdforJPfI5AutgpY5E1bO5+BesI2S6rA/D6o6CCg=; b=mDpqdxWDR7UvCn1miherHFNhhyu2Kkby2aYHMvu/Z6T2H4gQWGSJfK6hIWYjRSBfQn KDyu0PLfk59UXPoaYBJ2xI1gqYX5k5UgZMgxshPGV3THmYfGTH3rf+vKRKdSHXSMJv77 4m/zBzvWu8+I0NqnXCGSL8xi6xIAShYizPpHugV6SoyAUtLntF0C75dw4YuDkmt7xYIT n072RcSqR9vMBpZzWyHWMBdv9Rko+v2uuUpJfyYx18I1xhtFPti0gIxyOMyn+PFUSgey Px+OGtHKtB0JH8RK/9AFfJlhCBeiD6Ba3S58ZpD7IrD+LSZTwEtBCUrNV68BCW7rMr9d CZeQ== 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:mime-version :content-transfer-encoding; bh=a5eAdforJPfI5AutgpY5E1bO5+BesI2S6rA/D6o6CCg=; b=ZhsYHU0nTTPTjsLF4tRHJQ0ZBYdntsbOYWVUHlR5TJx3O9AFjrNdxYHmtSHG6GkV/J 37psQliZWsjOgrpRPxwX7fHiGmxIT65G4QTPUyMjSX1Jt3udnYa5dEcTkQuHmiKLVRbC zpSb7tDSuz5djDR4WS/z0pPU0I10vxmqsMs6Yn6DU67OWdNP4g6GDxTCdNS93TCryNra 2dm1tHQeJpuMhUHN1YynnjvrUqjwln4rsH/ijbBx2Q9G1xJfvcKfgaW4wNtiD0cOKMvZ 8hJ1b3CClzOqm11D4oITVTLNSmNB8WipukNvXmxSHqbjrjGJHjpSCyASHykr2KWR3baK EHZw== X-Gm-Message-State: APjAAAVPDiuaRJFeFjxZnPOmOWi4qkp440TZbzD+lCafviwxvpVvSriu kVokXVOOzLKMhVkgNTrOckc= X-Received: by 2002:a65:43c2:: with SMTP id n2mr16339061pgp.110.1565334639457; Fri, 09 Aug 2019 00:10:39 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id k25sm82073711pgt.53.2019.08.09.00.10.37 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 09 Aug 2019 00:10:38 -0700 (PDT) From: Chuhong Yuan Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v4 4/8] printk: Replace strncmp with str_has_prefix Date: Fri, 9 Aug 2019 15:10:34 +0800 Message-Id: <20190809071034.17279-1-hslester96@gmail.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org strncmp(str, const, len) is error-prone because len is easy to have typo. The example is the hard-coded len has counting error or sizeof(const) forgets - 1. So we prefer using newly introduced str_has_prefix() to substitute such strncmp to make code better. Signed-off-by: Chuhong Yuan --- Changes in v4: - Eliminate assignments in if conditions. kernel/printk/braille.c | 15 +++++++++++---- kernel/printk/printk.c | 22 ++++++++++++++++------ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c index 1d21ebacfdb8..17a9591e54ff 100644 --- a/kernel/printk/braille.c +++ b/kernel/printk/braille.c @@ -11,11 +11,18 @@ int _braille_console_setup(char **str, char **brl_options) { - if (!strncmp(*str, "brl,", 4)) { + size_t len; + + len = str_has_prefix(*str, "brl,"); + if (len) { *brl_options = ""; - *str += 4; - } else if (!strncmp(*str, "brl=", 4)) { - *brl_options = *str + 4; + *str += len; + return 0; + } + + len = str_has_prefix(*str, "brl="); + if (len) { + *brl_options = *str + len; *str = strchr(*brl_options, ','); if (!*str) { pr_err("need port name after brl=\n"); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1888f6a3b694..43a31015ec93 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -118,19 +118,29 @@ static unsigned int __read_mostly devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; static int __control_devkmsg(char *str) { + size_t len; + if (!str) return -EINVAL; - if (!strncmp(str, "on", 2)) { + len = str_has_prefix(str, "on"); + if (len) { devkmsg_log = DEVKMSG_LOG_MASK_ON; - return 2; - } else if (!strncmp(str, "off", 3)) { + return len; + } + + len = str_has_prefix(str, "off"); + if (len) { devkmsg_log = DEVKMSG_LOG_MASK_OFF; - return 3; - } else if (!strncmp(str, "ratelimit", 9)) { + return len; + } + + len = str_has_prefix(str, "ratelimit"); + if (len) { devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; - return 9; + return len; } + return -EINVAL; } -- 2.20.1