Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp307752ybi; Thu, 1 Aug 2019 19:31:59 -0700 (PDT) X-Google-Smtp-Source: APXvYqyVu/qNio4kKgBuQjV5+XdOAjH9YPdkmIexbv7Vtd5pqgXZq7J6Y1fmUWX3e6rzKaf2Iw6C X-Received: by 2002:a0c:8722:: with SMTP id 31mr96008529qvh.164.1564713119314; Thu, 01 Aug 2019 19:31:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564713119; cv=none; d=google.com; s=arc-20160816; b=CM6pXdApzOdfW5ejxp631gxI88cuIrN5ZuZaCvHNmSaGhq6nPEj9fJQ3+BL6hUkI5W xNUkubD0scT/DX0B4RmPLLX/5OKz8YsixLnO0dg05dVuHRP+R8ZhGWc2eBEaQzIy/7Fa 3nmeQ7G6B9rSnSr6M85FRn5pnmiKBO023HKf4XDLbUUcT+mYhdGfY8OUKJmukBSVLUmT TbawYNrs8Jlx7OUyAnbin+ScHuLY2leAfrd7Ka1zNg5OmxRkxYbBGOhSGjkFwbPVFf9Y eH+r0xHW+nrUiI6GncB1L9hpeM8mIFgGsai5CjReAqJPrBWgu7FRNomTMMytrAdYhy8n qEJA== 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=+vxHc5RDDwEK17Sr2QrAPRn0RxhaEPU9AZ0VMzC7/4Q=; b=zlTASyE52iAUPMSzvSVjW7tPjZnijzXwS4FSKbzfTuQFe4Xp+tCJSmEjj5ceC+gPxv UN0sL0wDti2MBYneApDysrGujCQlDjIo2zt0pD/oCdF3r/Es/WUIOAL/VYDCv5ZBGkb6 dkxSMPzKbfghIPCGtEFVZecaYbWsp4R0r2b6egnKXY2YT81iyT7xKUna7FwvNxY2dGiF 4x8as95gNwZXQILaxIVpSh7I29PT0QHWJFpr/kXC0DoIn3SZv4pUSSVJgCHA7VYj3ZYm L41/KzOw3hDd7LNlOSZnXlsjIB3kPf2kHVxwudPGfmJlTHluHrOEVwJAmieAAH+ph1C/ wJCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=Kzsp8Lap; 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 n13si43232565qvg.60.2019.08.01.19.31.45; Thu, 01 Aug 2019 19:31:59 -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=Kzsp8Lap; 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 S2388858AbfHBBrY (ORCPT + 99 others); Thu, 1 Aug 2019 21:47:24 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:38985 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728937AbfHBBrY (ORCPT ); Thu, 1 Aug 2019 21:47:24 -0400 Received: by mail-pl1-f194.google.com with SMTP id b7so33021833pls.6 for ; Thu, 01 Aug 2019 18:47:23 -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=+vxHc5RDDwEK17Sr2QrAPRn0RxhaEPU9AZ0VMzC7/4Q=; b=Kzsp8LaptraISsN7glqbkoD3sG8HXBmBzcgk0faMRd8we7GGrKKtNPsAIpSXCjW8I1 mfbVpQQ9+QrJV9HsZrF9T+loqXhtMP7C7Hu+n0vfJHCMBnMs+43c+qsYHfIrD3hwKmH3 GhsGGXmKW5V5YG0rwsmq/hO4qqHdGc/FvhQ/qA4WQnYa6aDr3cGeuBDkEvnMdKwAzhji /x9f5gt5b4AzFlv9IqBK3j9uOtl7PWbVvRAgWq0ckzxYzGUYTGOsIwcWaI9PgFr10Zc2 j1cGjTz8py/7MijjdYXjqTP4JTfdtcZgL4hUrFAOcCyXldouPJp4BK+d6iAB5AT7WUCi RYkA== 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=+vxHc5RDDwEK17Sr2QrAPRn0RxhaEPU9AZ0VMzC7/4Q=; b=lqcewdrn8q7XuEqKlbgcK+BP6qCCPjUO+69ow0L60cU4e3BizBzs1Md6nHcha6k3/u 7jgSnW7yROcBupmENBf/oz/BUUmAb17cjFMkaemwoQK7pZf1ViWF6YHBzGPVs6r05pI4 AP4FKEpnaWD8DCrtNq9rod0HLEfb6ipfGkArDlrvlF03ZO9+WkHHBMgloUbjcrKikCr7 hublHuuR2kHvSesTgxydBMUnArrUTQJphEBs/3yfi5Imu05+RHzIW/pfR433A1F+M0/t JFD6Fhq1qDfyYTQYVXoknorDd1sE8kmXmkZDbeFIDjilzkNhGOkjjW4WB+RkM2qIyKZ0 HBvg== X-Gm-Message-State: APjAAAUv8ZJK5pkpegalPgrxAhSpUur0d4WAu43rbRGjKOSJkV9Qe31i kLdw3NPtvtBG/2QsDVO1FNNs6DVj59f51A== X-Received: by 2002:a17:902:d715:: with SMTP id w21mr91477383ply.261.1564710443269; Thu, 01 Aug 2019 18:47:23 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id q126sm78872796pfq.123.2019.08.01.18.47.20 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Thu, 01 Aug 2019 18:47:22 -0700 (PDT) From: Chuhong Yuan Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Joe Perches , linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v2 06/10] printk: Replace strncmp with str_has_prefix Date: Fri, 2 Aug 2019 09:47:18 +0800 Message-Id: <20190802014718.8952-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. Signed-off-by: Chuhong Yuan --- Changes in v2: - Revise the description. - Utilize str_has_prefix's return value to eliminate some hard codes. kernel/printk/braille.c | 10 ++++++---- kernel/printk/printk.c | 14 ++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/kernel/printk/braille.c b/kernel/printk/braille.c index 1d21ebacfdb8..e451b8b1d3d5 100644 --- a/kernel/printk/braille.c +++ b/kernel/printk/braille.c @@ -11,11 +11,13 @@ int _braille_console_setup(char **str, char **brl_options) { - if (!strncmp(*str, "brl,", 4)) { + size_t len; + + if ((len = str_has_prefix(*str, "brl,"))) { *brl_options = ""; - *str += 4; - } else if (!strncmp(*str, "brl=", 4)) { - *brl_options = *str + 4; + *str += len; + } else if ((len = str_has_prefix(*str, "brl="))) { + *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..21b28c7dd18f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -118,18 +118,20 @@ 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)) { + if ((len = str_has_prefix(str, "on"))) { devkmsg_log = DEVKMSG_LOG_MASK_ON; - return 2; - } else if (!strncmp(str, "off", 3)) { + return len; + } else if ((len = str_has_prefix(str, "off"))) { devkmsg_log = DEVKMSG_LOG_MASK_OFF; - return 3; - } else if (!strncmp(str, "ratelimit", 9)) { + return len; + } else if ((len = str_has_prefix(str, "ratelimit"))) { devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; - return 9; + return len; } return -EINVAL; } -- 2.20.1