Received: by 2002:a25:b794:0:0:0:0:0 with SMTP id n20csp2697636ybh; Mon, 5 Aug 2019 05:25:34 -0700 (PDT) X-Google-Smtp-Source: APXvYqwDReQ5OqN125TPo3BX3s22O66evguL6ac9fguW6dEGCzEIbMCAQvbclqP/gA75SzwlGHOn X-Received: by 2002:aa7:9117:: with SMTP id 23mr73245588pfh.206.1565007934032; Mon, 05 Aug 2019 05:25:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565007934; cv=none; d=google.com; s=arc-20160816; b=xetF+Zomf6O2zzK5/SJxn4yODP45iviG2PLbb2OIFzrC+Yob+61G1VAg7F7Ly7i4F/ uTRflk5nA47XjHbglo80mwLUaVt7b1rTHL1G1GXP8DVZKkZNPNJkv/b8Op/LCkAipHmu dY9Bx15NuaCg2S6oVtXU+fnEaFE/JFrtugZcQXYGMesEJRNlDYYkAAInQvLkT07zLpTb 739evefVnS4kANnMtAafqcT5RbsAdLgpi62mu8uw/pEsbpWVg3scY9CmwUzilFmj30jy zqAHiHJiQYmoxtqhhruUZzlev2dVwMcv1kOqioO3RGBB+kNnXBcOmaHq17fwBXbr0xD4 lpFg== 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=Ohj8KSWGo2nhApI0RM9RRDG6wa0D5NI6rZtcauJuo7w=; b=fsWu9pW0xcalSqkVMfLmbgDBchnEo2E3EJd8L+9eFQ2AgYCnURdEwX8D4KzgVX7Ihh /p3b8LaMqZIW1zipL1f71vS60SuhKM6UUxRubzthbhSZbb/Pp8x07HUwnGfihNdXEvhc KekpHJ2+ZfAFRWWH/DyOMudyXvUVSYQWcMjBgpwWWwxd3haO6WpHxT33nM1xeCj3kyed 3cV7wuW1PUeIY78ezmNpk18IvZgj53LZTLICiFP0LH8QwWXv7Byf9mXmFusKgXvOxatc Adwl2IJmJuSstcC2uY6RO62HsGKbqPwKO0JRrLqteCTCF3deoTgwk8FF5qI0LCG0nA4G rGog== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="Lq2/vV0P"; 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 h7si47223616pgc.130.2019.08.05.05.25.18; Mon, 05 Aug 2019 05:25:34 -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="Lq2/vV0P"; 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 S1728734AbfHEMXF (ORCPT + 99 others); Mon, 5 Aug 2019 08:23:05 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:46460 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727259AbfHEMXE (ORCPT ); Mon, 5 Aug 2019 08:23:04 -0400 Received: by mail-pg1-f196.google.com with SMTP id w3so2446170pgt.13 for ; Mon, 05 Aug 2019 05:23:04 -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=Ohj8KSWGo2nhApI0RM9RRDG6wa0D5NI6rZtcauJuo7w=; b=Lq2/vV0Pqe/r1QH+JpS1FgOvGYFIAlO01kM8v5N/KlNlxGwyokpMvqYyDCGS65/O/L ig2qzFoJ/Eub/409Zvm94AZfX6yebCavrf+xRY5jWY4cRssSE4HJivEYfH9Q2ISC6KVS mvYyI7U8XATTcf5mS8IGAewp2VfJoj/c0D6bY+X7XZlFz4NDQMagN0yHqNP6JEgXpH6Z azH8lLskoWbfDRnEZgDs+IPScmMfaZmRBv84ARcr80KuTHjlMA8ABqveKUV/zx+P94ED ntuMOK+EftegUYIHQ+c4LX9TlmVOZxsoBagZByZI6xv41pF6VdxJ5GCidYpPzGyyLjLk OO2Q== 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=Ohj8KSWGo2nhApI0RM9RRDG6wa0D5NI6rZtcauJuo7w=; b=k3/jw0122cR5UyHFqdF8WsFXhEJkMdWoGVSu0OUV77n46PRWx4brV9V33b+hmtPvKa 5d3HFoeU91MTXSCkfxEfoOYX72pH/ndSc4ft84S+oLTzZK7GzgOcTCwCGk46G8fkfkZI 76uIzZv0q7ThbFTTm9dJ5qvDCauGCFfQcpPT0C5SY1s/lytaQ0Rj9N9lroF0uvEx6Uk1 yYHgtiLNygsGVFQcqnRIvaRvHSWNjYE4kGY1eKDPJk1JWrSKJQMZX9g1vgyHkbo6Sidh 0ArCQp3EOFaGxfjNDUnoO/YrI1AED2qpPlBlf1DtuxRVLRU0MSAMXAaWUPD1+nAuVLoJ w/+w== X-Gm-Message-State: APjAAAWbIWJktD/r1vwQR3BE+XXKemzoWcKVa9KQHn1dba1wA159RNyK q6Elf1/c6M2VZD1AZ8g4N+WnBEUwuI0hYA== X-Received: by 2002:a65:48c2:: with SMTP id o2mr12700113pgs.45.1565007784005; Mon, 05 Aug 2019 05:23:04 -0700 (PDT) Received: from suzukaze.ipads-lab.se.sjtu.edu.cn ([89.31.126.54]) by smtp.gmail.com with ESMTPSA id 97sm21685371pjz.12.2019.08.05.05.23.00 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 05 Aug 2019 05:23:03 -0700 (PDT) From: Chuhong Yuan Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Joe Perches , linux-kernel@vger.kernel.org, Chuhong Yuan Subject: [PATCH v3 4/8] printk: Replace strncmp with str_has_prefix Date: Mon, 5 Aug 2019 20:22:54 +0800 Message-Id: <20190805122254.13041-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 v3: - Revise the description. - Remove else uses in printk.c. kernel/printk/braille.c | 10 ++++++---- kernel/printk/printk.c | 19 +++++++++++++------ 2 files changed, 19 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..6b8d9cfebc0b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -118,19 +118,26 @@ 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; + } + + if ((len = str_has_prefix(str, "off"))) { devkmsg_log = DEVKMSG_LOG_MASK_OFF; - return 3; - } else if (!strncmp(str, "ratelimit", 9)) { + return len; + } + + if ((len = str_has_prefix(str, "ratelimit"))) { devkmsg_log = DEVKMSG_LOG_MASK_DEFAULT; - return 9; + return len; } + return -EINVAL; } -- 2.20.1