mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Perches <joe@perches.com>
To: Steven Rostedt <rostedt@goodmis.org>,
	LKML <linux-kernel@vger.kernel.org>
Cc: Julia Lawall <julia.lawall@lip6.fr>
Subject: [utility perl script] strncmp() -> str_has_prefix() conversions
Date: Fri, 11 Jan 2019 00:10:40 -0800	[thread overview]
Message-ID: <6f3a66eb8edacd3b6813558e8a4e9220350b09d7.camel@perches.com> (raw)
In-Reply-To: <20181221231924.4583e90b@vmware.local.home>

[-- Attachment #1: Type: text/plain, Size: 2342 bytes --]

On Fri, 2018-12-21 at 23:19 -0500, Steven Rostedt wrote:
> str_has_prefix

A coccinelle script could be more thorough but here
is a trivial perl script that can do most of the
	strncmp() -> str_has_prefix()
conversions where there is a constant string as one of
the first two arguments of strncmp like any of:

	strncmp(foo, "bar", 3)
	strncmp(foo, "bar", strlen("bar"))
	strncmp(foo, "bar", sizeof("bar") - 1)
	strncmp("foo", bar, 3)
	strncmp("foo", bar, strlen("foo"))
	strncmp("foo", bar",
sizeof("foo") - 1)

It could be used with a particular path or file:

$ git grep -w --name-only strncmp <path> | \
  grep -vP '^(tools|scripts)' | \
  while read file ; do \
    echo $file ; \
    perl -i ./strncmp.perl $file ; \
  done

It mostly works, but there are a few uses that
are not converted properly when the non const
string argument to strncmp is an expression like

	strncmp(a+b, "foo", 3)

There are also strncmp uses that remain after
this script is run where strncmp should just
be converted to strcmp instead like:

	strncmp(p, "foo", sizeof(foo))
and
	strncmp(p, "foo", 4)

The script converts the most common cases:

    ## counted length of string
    # strncmp(arg, string, counted length of string) == 0
    # strncmp(arg, string, counted length of string) != 0
    # !strncmp(arg, string, counted length of string)
    # strncmp(arg, string, counted length of string)

    ## Reversed string/arg counted length of string uses
    # strncmp(string, arg, counted length of string) == 0
    # strncmp(string, arg, counted length of string) != 0
    # !strncmp(string, arg, counted length of string)
    # strncmp(string, arg, counted length of string)

    ## strlen uses
    # strncmp(arg, string, strlen(string)) == 0
    # !strncmp(arg, string, strlen(string))

    ## reversed string/arg strlen uses
    # strncmp(string, arg, strlen(string)) == 0
    # !strncmp(string, arg, strlen(string))

    ## 'sizeof(string) - 1' uses
    # strncmp(arg, string, sizeof(string) - 1) == 0
    # !strncmp(arg, string, sizeof(string) - 1)


On linux-next, running the script below

$ git grep -w --name-only strncmp | \
  grep -vP '^(tools|scripts)' | \
  while read file ; do \
    echo $file ; \
    perl -i ./strncmp.perl $file ; \
  done

produces:

$ git diff --shortstat
 437 files changed, 1483 insertions(+), 1500 deletions(-)


[-- Attachment #2: strncmp.perl --]
[-- Type: application/x-perl, Size: 6945 bytes --]

      parent reply	other threads:[~2019-01-11  8:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-22  4:19 [PATCH v4] string.h: Add str_has_prefix() helper function Steven Rostedt
2018-12-22  4:42 ` Steven Rostedt
2018-12-22  9:33 ` Namhyung Kim
2018-12-22 12:24   ` Steven Rostedt
2018-12-22 14:24     ` Namhyung Kim
2018-12-22 15:12       ` Steven Rostedt
2018-12-22 16:16         ` Steven Rostedt
2018-12-22 16:46           ` Namhyung Kim
2018-12-22 17:19             ` Steven Rostedt
2018-12-22 17:23               ` Steven Rostedt
2018-12-22 17:24                 ` Steven Rostedt
2018-12-23  3:13                   ` Namhyung Kim
2018-12-23  3:23                     ` Steven Rostedt
2018-12-23  3:05               ` Namhyung Kim
2019-01-11  8:10 ` Joe Perches [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6f3a66eb8edacd3b6813558e8a4e9220350b09d7.camel@perches.com \
    --to=joe@perches.com \
    --cc=julia.lawall@lip6.fr \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®