Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1965364imu; Sat, 22 Dec 2018 09:07:32 -0800 (PST) X-Google-Smtp-Source: ALg8bN6VKGtWiDoHwFk4M9zwpfKU6IVF5atJz5pno+vlA023ZZD/Y5jkrGbqIJp9AzWCYvSa5+MV X-Received: by 2002:a17:902:622:: with SMTP id 31mr6950692plg.171.1545498452633; Sat, 22 Dec 2018 09:07:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1545498452; cv=none; d=google.com; s=arc-20160816; b=OvJxWJ/OC+j5Z8lzVLL6nNhbAubUloGayIAVo+MDk5c2RbXc/WCJITtrIe/JIlNdEu e7JfW3OGP+N5kTdmdydeJdrf/W73XfLNabK/EoE1ABt236VcPthWqq7uZndHbKDitwuj dg/gNbihINFAIKqCmYN3Jep5IqCMrKc8fjI7ar/kWdamF5ExD1XqDJs2lHVbk5CfW0Rm FOqOZAlaNiSuhLZqMg6yo2lrUi3ho8oJxMQ9j2dvYOCk/+P+o6sKXmLKLRejwSxzRgHK rIP/jTRUYSx4Zc0AWS+7jmF/V+KljLb+ZiydVSVO/YquAgsJ6ZvlmNCJVVQj2k4HvSZn y7KA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:subject:cc:to :from:date:user-agent:message-id; bh=zCyJfXXRfHgKHbrkdmr8LCEm4nD0d+yxlm6V5jJMwek=; b=Ns3ERr7dz+2Vw7JFDca3DCxpMd8hZO/85yOenrJSixgYLJY8RLazx6y2BBVNtc32P/ ebchw2YI6jxsAPhgD2//U6/6vZzz4+C7sadH3RIl7mg9LJZ7nZXQ5gzDsIdYV1XFs6c2 JzSl9tI2FPsrfchjnu0c9uHWds+Fy8Mqcvu2UA1NCeOcNzWalvtEp8NCqxrwv85tmhT8 9LUSb10iUTbbnX+UQg0YfcrdMqHJoh6xC4KFrqVbDsD6++bsz3zP/yY/FSSppMQtlZiH zVg9IShAhd5RfdpGWO570/vYAyhlhgyXRVS1cyYyZYnf5mer9o/qIUB9Jqmwnq2ingDb dM6Q== 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 y20si23087440plr.106.2018.12.22.09.07.17; Sat, 22 Dec 2018 09:07:32 -0800 (PST) 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 S2391216AbeLUR5Q (ORCPT + 99 others); Fri, 21 Dec 2018 12:57:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:55970 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391167AbeLUR5A (ORCPT ); Fri, 21 Dec 2018 12:57:00 -0500 Received: from gandalf.local.home (cpe-66-24-56-78.stny.res.rr.com [66.24.56.78]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3269C2197F; Fri, 21 Dec 2018 17:57:00 +0000 (UTC) Received: from rostedt by gandalf.local.home with local (Exim 4.91) (envelope-from ) id 1gaP2h-0002Si-A3; Fri, 21 Dec 2018 12:56:59 -0500 Message-Id: <20181221175659.208858193@goodmis.org> User-Agent: quilt/0.65 Date: Fri, 21 Dec 2018 12:56:41 -0500 From: Steven Rostedt To: linux-kernel@vger.kernel.org Cc: Ingo Molnar , Andrew Morton , Namhyung Kim , Masami Hiramatsu , Joe Perches , Tom Zanussi , Linus Torvalds , Greg Kroah-Hartman Subject: [for-next][PATCH 23/24] string.h: Add strncmp_prefix() helper macro References: <20181221175618.968519903@goodmis.org> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Steven Rostedt A discussion came up in the trace triggers thread about converting a bunch of: strncmp(str, "const", sizeof("const") - 1) use cases into a helper macro. It started with: strncmp(str, const, sizeof(const) - 1) But then Joe Perches mentioned that if a const is not used, the sizeof() will be the size of a pointer, which can be bad. And that gcc will optimize strlen("const") into "sizeof("const") - 1". Thinking about this more, a quick grep in the kernel tree found several (thousands!) of cases that use this construct. A quick grep also revealed that there's probably several bugs in that use case. Some are that people forgot the "- 1" (which I found) and others could be that the constant for the sizeof is different than the constant (although, I haven't found any of those, but I also didn't look hard). I figured the best thing to do is to create a helper macro and place it into include/linux/string.h. And go around and fix all the open coded versions of it later. I plan on only applying this patch and updating the tracing hooks for this merge window. And perhaps use it to fix some of the bugs that were found. I was going to just use: strncmp(str, prefix, strlen(prefix)) but then realized that "prefix" is used twice, and will break if someone does something like: strncmp_prefix(str, ptr++); So instead I check with __builtin_constant_p() to see if the second parameter is truly a constant, which I use sizeof() anyway (why bother gcc to optimize it, if we already know it's a constant), and copy the parameter into a local variable and use that local variable for the non constant part (with strlen). Link: http://lkml.kernel.org/r/e3e754f2bd18e56eaa8baf79bee619316ebf4cfc.1545161087.git.tom.zanussi@linux.intel.com Link: http://lkml.kernel.org/r/20181219211615.2298e781@gandalf.local.home Cc: Joe Perches Cc: Tom Zanussi Cc: Linus Torvalds Cc: Greg Kroah-Hartman Signed-off-by: Steven Rostedt (VMware) --- include/linux/string.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/include/linux/string.h b/include/linux/string.h index 27d0482e5e05..a998a53333ef 100644 --- a/include/linux/string.h +++ b/include/linux/string.h @@ -14,6 +14,28 @@ extern void *memdup_user(const void __user *, size_t); extern void *vmemdup_user(const void __user *, size_t); extern void *memdup_user_nul(const void __user *, size_t); +/* + * A common way to test a prefix of a string is to do: + * strncmp(str, prefix, sizeof(prefix) - 1) + * + * But this can lead to bugs due to typos, or if prefix is a pointer + * and not a constant. Instead use strncmp_prefix(). + */ +#define strncmp_prefix(str, prefix) \ + ({ \ + int ____strcmp_prefix_ret____; \ + if (__builtin_constant_p(&prefix)) { \ + ____strcmp_prefix_ret____ = \ + strncmp(str, prefix, sizeof(prefix) - 1); \ + } else { \ + typeof(prefix) ____strcmp_prefix____ = prefix; \ + ____strcmp_prefix_ret____ = \ + strncmp(str, ____strcmp_prefix____, \ + strlen(____strcmp_prefix____)); \ + } \ + ____strcmp_prefix_ret____; \ + }) + /* * Include machine specific inline routines */ -- 2.19.2