Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 16:01 -0500, Steven Rostedt wrote: > On Wed, 19 Dec 2018 14:38:39 -0600 > Tom Zanussi wrote: > > > On Wed, 2018-12-19 at 15:30 -0500, Steven Rostedt wrote: > > > On Wed, 19 Dec 2018 12:20:19 -0800 > > > Joe Perches wrote: > > > > > > > > Yeah, I had considered it but wasn't sure it was worth it. Since > > > > > you're suggesting it is, I can send another patch on top of > > > > > these, or > > > > > feel free if you want to too. ;-) > > > > > > > > I believe the 'strlen("foo") -> sizeof("foo") - 1' > > > > conversions do not change objects at all. > > > > > > > > strlen("constant") is already optimized by gcc to a > > > > constant value when fed a constant string. > > > > > > If that's the case (and it probably is), then yeah, strlen is > > > probably > > > better. As it can handle the "not a constant" that you stated in > > > another email. > > > > > > > OK, so I guess that means we should just drop this patch ('[PATCH 2/7] > > tracing: Change strlen to sizeof for hist trigger static strings'). > > > > No, that patch is fine, the macro was not. I've already applied your > patch set. Just need to run it through my tests. The patch included: - start += strlen("char["); + start += sizeof("char[") - 1; So, that 2/7 patch is unnecessary as there is no object code change and using 'sizeof("const") - 1' is less intelligible than strlen("const") If you convert the strncmp(ptr, "const", strlen("const")) uses to strncmp_prefix(ptr, "const") eventually, the patch just makes more variations to change.
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 12:51:59 -0800 Joe Perches wrote: > > #define strncmp_prefix(str, prefix) \ > > strncmp(str, prefix, strlen(prefix)) > > > > in include/linux/string.h > > > > And go around and use that throughout the kernel. By doing a quick > > grep, I already spotted a few bugs. > > I hope you also convert the existing uses like > > strncmp(str1, "str2", 4) > > where the length value is precalculated to the strlen > of the const string Yeah sure. > > But there seem to be _a lot_ of those... > > $ git grep -P "\bstrncmp\s*\([^,]+,[^,]+,\s*\d+\s*\)" | wc -l > 1681 > Right. I'm going to first create the macro and probably just apply it to the tracing directory. Then when the macro is added to the next merge window, I'll post a bunch of patches that clean up the rest of the code. -- Steve
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 14:38:39 -0600 Tom Zanussi wrote: > On Wed, 2018-12-19 at 15:30 -0500, Steven Rostedt wrote: > > On Wed, 19 Dec 2018 12:20:19 -0800 > > Joe Perches wrote: > > > > > > Yeah, I had considered it but wasn't sure it was worth it. Since > > > > you're suggesting it is, I can send another patch on top of > > > > these, or > > > > feel free if you want to too. ;-) > > > > > > I believe the 'strlen("foo") -> sizeof("foo") - 1' > > > conversions do not change objects at all. > > > > > > strlen("constant") is already optimized by gcc to a > > > constant value when fed a constant string. > > > > If that's the case (and it probably is), then yeah, strlen is > > probably > > better. As it can handle the "not a constant" that you stated in > > another email. > > > > OK, so I guess that means we should just drop this patch ('[PATCH 2/7] > tracing: Change strlen to sizeof for hist trigger static strings'). > No, that patch is fine, the macro was not. I've already applied your patch set. Just need to run it through my tests. -- Steve
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 15:34 -0500, Steven Rostedt wrote: > On Wed, 19 Dec 2018 12:22:38 -0800 > Joe Perches wrote: > > > On Wed, 2018-12-19 at 14:16 -0600, Tom Zanussi wrote: > > > How's this? > > > > > > [PATCH] tracing: Introduce and use strcmp_const() for hist triggers > > > > > > Provide a new strcmp_const() macro and make use of it instead of the > > > longer and more error-prone strncmp(str, "str", sizeof("str") - 1). > > [] > > > diff --git a/kernel/trace/trace_events_hist.c > > > b/kernel/trace/trace_events_hist.c > > [] > > > @@ -22,6 +22,9 @@ > > > > > > #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */ > > > > > > +#define strcmp_const(str, str_const) \ > > > + strncmp(str, str_const, sizeof(str_const) - 1) > > > > Not good as it's too easy to pass a pointer as str_const > > and sizeof(pointer) - 1 isn't likely the string length. > > Agreed. And I noticed that this is used all over the kernel, so I'm not > going to add this patch. I'm going to add a: > > #define strncmp_prefix(str, prefix) \ > strncmp(str, prefix, strlen(prefix)) > > in include/linux/string.h > > And go around and use that throughout the kernel. By doing a quick > grep, I already spotted a few bugs. I hope you also convert the existing uses like strncmp(str1, "str2", 4) where the length value is precalculated to the strlen of the const string But there seem to be _a lot_ of those... $ git grep -P "\bstrncmp\s*\([^,]+,[^,]+,\s*\d+\s*\)" | wc -l 1681
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 15:30 -0500, Steven Rostedt wrote: > On Wed, 19 Dec 2018 12:20:19 -0800 > Joe Perches wrote: > > > > Yeah, I had considered it but wasn't sure it was worth it. Since > > > you're suggesting it is, I can send another patch on top of > > > these, or > > > feel free if you want to too. ;-) > > > > I believe the 'strlen("foo") -> sizeof("foo") - 1' > > conversions do not change objects at all. > > > > strlen("constant") is already optimized by gcc to a > > constant value when fed a constant string. > > If that's the case (and it probably is), then yeah, strlen is > probably > better. As it can handle the "not a constant" that you stated in > another email. > OK, so I guess that means we should just drop this patch ('[PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings'). Tom > -- Steve > > > > > > the strcmp_const macro does seem to make sense as > > the copy/paste/typo possibility is real. > >
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 12:22:38 -0800 Joe Perches wrote: > On Wed, 2018-12-19 at 14:16 -0600, Tom Zanussi wrote: > > > > How's this? > > > > [PATCH] tracing: Introduce and use strcmp_const() for hist triggers > > > > Provide a new strcmp_const() macro and make use of it instead of the > > longer and more error-prone strncmp(str, "str", sizeof("str") - 1). > [] > > diff --git a/kernel/trace/trace_events_hist.c > > b/kernel/trace/trace_events_hist.c > [] > > @@ -22,6 +22,9 @@ > > > > #define STR_VAR_LEN_MAX32 /* must be multiple of sizeof(u64) */ > > > > +#define strcmp_const(str, str_const) \ > > + strncmp(str, str_const, sizeof(str_const) - 1) > > Not good as it's too easy to pass a pointer as str_const > and sizeof(pointer) - 1 isn't likely the string length. Agreed. And I noticed that this is used all over the kernel, so I'm not going to add this patch. I'm going to add a: #define strncmp_prefix(str, prefix) \ strncmp(str, prefix, strlen(prefix)) in include/linux/string.h And go around and use that throughout the kernel. By doing a quick grep, I already spotted a few bugs. -- Steve
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 12:20:19 -0800 Joe Perches wrote: > > Yeah, I had considered it but wasn't sure it was worth it. Since > > you're suggesting it is, I can send another patch on top of these, or > > feel free if you want to too. ;-) > > I believe the 'strlen("foo") -> sizeof("foo") - 1' > conversions do not change objects at all. > > strlen("constant") is already optimized by gcc to a > constant value when fed a constant string. If that's the case (and it probably is), then yeah, strlen is probably better. As it can handle the "not a constant" that you stated in another email. -- Steve > > the strcmp_const macro does seem to make sense as > the copy/paste/typo possibility is real.
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 14:16:31 -0600 Tom Zanussi wrote: > > Yeah, I had considered it but wasn't sure it was worth it. Since > > you're suggesting it is, I can send another patch on top of these, or > > feel free if you want to too. ;-) > > > > How's this? > > [PATCH] tracing: Introduce and use strcmp_const() for hist triggers > > Provide a new strcmp_const() macro and make use of it instead of the > longer and more error-prone strncmp(str, "str", sizeof("str") - 1). > > Idea-and-macro-by: Steve Rostedt > Signed-off-by: Tom Zanussi > --- And I just sent you my version :-) (cross emails). Note, I changed it to strncmp_const() because it should note that it's a strncmp() and not an strcmp(). Pretty much identical patches too ;-) -- Steve
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 19 Dec 2018 13:46:49 -0600 Tom Zanussi wrote: > Yeah, I had considered it but wasn't sure it was worth it. Since > you're suggesting it is, I can send another patch on top of these, or > feel free if you want to too. ;-) > Here, want to ack/review it? -- Steve >From 664457f5cc776aa624502bc628285ea6d70ac8c9 Mon Sep 17 00:00:00 2001 From: "Steven Rostedt (VMware)" Date: Wed, 19 Dec 2018 15:13:33 -0500 Subject: [PATCH] tracing: Add strncmp_const() macro to prevent typos and cut paste errors The trace_events_hist.c file has a lot of strncmp()s of the form: if (strncmp(str, "constant", sizeof("constant") - 1) == 0) To prevent typos and errors with the constant string being different from the string use in sizeof(), and also to condense the code, add a macro that does this: #define strncmp_const(str, str_const) \ strncmp(str, str_const, sizeof(str_const) - 1) Signed-off-by: Steven Rostedt (VMware) --- kernel/trace/trace_events_hist.c | 32 ++-- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index c5448c103770..40e76053d3d7 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -22,6 +22,10 @@ #define STR_VAR_LEN_MAX32 /* must be multiple of sizeof(u64) */ +/* Safe way to compare string constants (from typos and cut and paste errors) */ +#define strncmp_const(str, str_const) \ + strncmp(str, str_const, sizeof(str_const) - 1) + struct hist_field; typedef u64 (*hist_field_fn_t) (struct hist_field *field, @@ -1881,8 +1885,8 @@ static int parse_action(char *str, struct hist_trigger_attrs *attrs) if (attrs->n_actions >= HIST_ACTIONS_MAX) return ret; - if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == 0) || - (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) { + if ((strncmp_const(str, "onmatch(") == 0) || + (strncmp_const(str, "onmax(") == 0)) { attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); if (!attrs->action_str[attrs->n_actions]) { ret = -ENOMEM; @@ -1899,34 +1903,34 @@ static int parse_assignment(char *str, struct hist_trigger_attrs *attrs) { int ret = 0; - if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || - (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { + if ((strncmp_const(str, "key=") == 0) || + (strncmp_const(str, "keys=") == 0)) { attrs->keys_str = kstrdup(str, GFP_KERNEL); if (!attrs->keys_str) { ret = -ENOMEM; goto out; } - } else if ((strncmp(str, "val=", sizeof("val=") - 1) == 0) || -(strncmp(str, "vals=", sizeof("vals=") - 1) == 0) || -(strncmp(str, "values=", sizeof("values=") - 1) == 0)) { + } else if ((strncmp_const(str, "val=") == 0) || + (strncmp_const(str, "vals=") == 0) || + (strncmp_const(str, "values=") == 0)) { attrs->vals_str = kstrdup(str, GFP_KERNEL); if (!attrs->vals_str) { ret = -ENOMEM; goto out; } - } else if (strncmp(str, "sort=", sizeof("sort=") - 1) == 0) { + } else if (strncmp_const(str, "sort=") == 0) { attrs->sort_key_str = kstrdup(str, GFP_KERNEL); if (!attrs->sort_key_str) { ret = -ENOMEM; goto out; } - } else if (strncmp(str, "name=", sizeof("name=") - 1) == 0) { + } else if (strncmp_const(str, "name=") == 0) { attrs->name = kstrdup(str, GFP_KERNEL); if (!attrs->name) { ret = -ENOMEM; goto out; } - } else if (strncmp(str, "clock=", sizeof("clock=") - 1) == 0) { + } else if (strncmp_const(str, "clock=") == 0) { strsep(, "="); if (!str) { ret = -EINVAL; @@ -1939,7 +1943,7 @@ static int parse_assignment(char *str, struct hist_trigger_attrs *attrs) ret = -ENOMEM; goto out; } - } else if (strncmp(str, "size=", sizeof("size=") - 1) == 0) { + } else if (strncmp_const(str, "size=") == 0) { int map_bits = parse_map_size(str); if (map_bits < 0) { @@ -3558,7 +3562,7 @@ static struct action_data *onmax_parse(char *str) if (!onmax_fn_name || !str) goto free; - if (strncmp(onmax_fn_name, "save", sizeof("save") - 1) == 0) { + if (strncmp_const(onmax_fn_name, "save") == 0) { char *params = strsep(, ")"); if (!params) { @@ -4346,7 +4350,7 @@ static int
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 14:16 -0600, Tom Zanussi wrote: > > How's this? > > [PATCH] tracing: Introduce and use strcmp_const() for hist triggers > > Provide a new strcmp_const() macro and make use of it instead of the > longer and more error-prone strncmp(str, "str", sizeof("str") - 1). [] > diff --git a/kernel/trace/trace_events_hist.c > b/kernel/trace/trace_events_hist.c [] > @@ -22,6 +22,9 @@ > > #define STR_VAR_LEN_MAX 32 /* must be multiple of sizeof(u64) */ > > +#define strcmp_const(str, str_const) \ > + strncmp(str, str_const, sizeof(str_const) - 1) Not good as it's too easy to pass a pointer as str_const and sizeof(pointer) - 1 isn't likely the string length.
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 13:46 -0600, Tom Zanussi wrote: > Hi Steve, > > On Wed, 2018-12-19 at 14:40 -0500, Steven Rostedt wrote: > > On Tue, 18 Dec 2018 14:33:21 -0600 > > Tom Zanussi wrote: > > > > > From: Tom Zanussi > > > > > > There's no need to use strlen() for static strings when the length > > > is > > > already known, so update trace_events_hist.c with sizeof() for > > > those > > > cases. > > > > > > Signed-off-by: Tom Zanussi > > > --- > > > kernel/trace/trace_events_hist.c | 38 +++- > > > -- > > > 1 file changed, 19 insertions(+), 19 deletions(-) > > > > > > diff --git a/kernel/trace/trace_events_hist.c > > > b/kernel/trace/trace_events_hist.c > > > index d29bf8a8e1dd..25d06b3ae1f6 100644 > > > --- a/kernel/trace/trace_events_hist.c > > > +++ b/kernel/trace/trace_events_hist.c > > > @@ -507,7 +507,7 @@ static int synth_field_string_size(char *type) > > > start = strstr(type, "char["); > > > if (start == NULL) > > > return -EINVAL; > > > - start += strlen("char["); > > > + start += sizeof("char[") - 1; > > > > > > end = strchr(type, ']'); > > > if (!end || end < start) > > > @@ -1843,8 +1843,8 @@ static int parse_action(char *str, struct > > > hist_trigger_attrs *attrs) > > > if (attrs->n_actions >= HIST_ACTIONS_MAX) > > > return ret; > > > > > > - if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) || > > > - (strncmp(str, "onmax(", strlen("onmax(")) == 0)) { > > > + if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == > > > 0) || > > > + (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) { > > > attrs->action_str[attrs->n_actions] = kstrdup(str, > > > GFP_KERNEL); > > > if (!attrs->action_str[attrs->n_actions]) { > > > ret = -ENOMEM; > > > @@ -1861,34 +1861,34 @@ static int parse_assignment(char *str, > > > struct hist_trigger_attrs *attrs) > > > { > > > int ret = 0; > > > > > > - if ((strncmp(str, "key=", strlen("key=")) == 0) || > > > - (strncmp(str, "keys=", strlen("keys=")) == 0)) { > > > + if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || > > > + (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { > > > attrs->keys_str = kstrdup(str, GFP_KERNEL); > > > > I'll apply this as is, but since there's a lot of these, I wonder if > > we > > should make a marcro for this: > > > > #define strcmp_const(str, str_const) strncmp(str, str_const, > > sizeof(str_const) - 1) > > > > ? > > > > This would help prevent bugs due to typos and bad cut and paste. > > > > Yeah, I had considered it but wasn't sure it was worth it. Since > you're suggesting it is, I can send another patch on top of these, or > feel free if you want to too. ;-) I believe the 'strlen("foo") -> sizeof("foo") - 1' conversions do not change objects at all. strlen("constant") is already optimized by gcc to a constant value when fed a constant string. the strcmp_const macro does seem to make sense as the copy/paste/typo possibility is real.
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Wed, 2018-12-19 at 13:46 -0600, Tom Zanussi wrote: > Hi Steve, > > On Wed, 2018-12-19 at 14:40 -0500, Steven Rostedt wrote: > > On Tue, 18 Dec 2018 14:33:21 -0600 > > Tom Zanussi wrote: > > > > > From: Tom Zanussi > > > > > > There's no need to use strlen() for static strings when the > > > length > > > is > > > already known, so update trace_events_hist.c with sizeof() for > > > those > > > cases. > > > > > > Signed-off-by: Tom Zanussi > > > --- > > > kernel/trace/trace_events_hist.c | 38 +++--- > > > -- > > > -- > > > 1 file changed, 19 insertions(+), 19 deletions(-) > > > > > > diff --git a/kernel/trace/trace_events_hist.c > > > b/kernel/trace/trace_events_hist.c > > > index d29bf8a8e1dd..25d06b3ae1f6 100644 > > > --- a/kernel/trace/trace_events_hist.c > > > +++ b/kernel/trace/trace_events_hist.c > > > @@ -507,7 +507,7 @@ static int synth_field_string_size(char > > > *type) > > > start = strstr(type, "char["); > > > if (start == NULL) > > > return -EINVAL; > > > - start += strlen("char["); > > > + start += sizeof("char[") - 1; > > > > > > end = strchr(type, ']'); > > > if (!end || end < start) > > > @@ -1843,8 +1843,8 @@ static int parse_action(char *str, struct > > > hist_trigger_attrs *attrs) > > > if (attrs->n_actions >= HIST_ACTIONS_MAX) > > > return ret; > > > > > > - if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) > > > || > > > - (strncmp(str, "onmax(", strlen("onmax(")) == 0)) { > > > + if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == > > > 0) || > > > + (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) > > > { > > > attrs->action_str[attrs->n_actions] = > > > kstrdup(str, > > > GFP_KERNEL); > > > if (!attrs->action_str[attrs->n_actions]) { > > > ret = -ENOMEM; > > > @@ -1861,34 +1861,34 @@ static int parse_assignment(char *str, > > > struct hist_trigger_attrs *attrs) > > > { > > > int ret = 0; > > > > > > - if ((strncmp(str, "key=", strlen("key=")) == 0) || > > > - (strncmp(str, "keys=", strlen("keys=")) == 0)) { > > > + if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || > > > + (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { > > > attrs->keys_str = kstrdup(str, GFP_KERNEL); > > > > I'll apply this as is, but since there's a lot of these, I wonder > > if > > we > > should make a marcro for this: > > > > #define strcmp_const(str, str_const) strncmp(str, str_const, > > sizeof(str_const) - 1) > > > > ? > > > > This would help prevent bugs due to typos and bad cut and paste. > > > > Yeah, I had considered it but wasn't sure it was worth it. Since > you're suggesting it is, I can send another patch on top of these, or > feel free if you want to too. ;-) > How's this? [PATCH] tracing: Introduce and use strcmp_const() for hist triggers Provide a new strcmp_const() macro and make use of it instead of the longer and more error-prone strncmp(str, "str", sizeof("str") - 1). Idea-and-macro-by: Steve Rostedt Signed-off-by: Tom Zanussi --- kernel/trace/trace_events_hist.c | 31 +-- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c index c5448c103770..aaf0d2ac4aab 100644 --- a/kernel/trace/trace_events_hist.c +++ b/kernel/trace/trace_events_hist.c @@ -22,6 +22,9 @@ #define STR_VAR_LEN_MAX32 /* must be multiple of sizeof(u64) */ +#define strcmp_const(str, str_const) \ + strncmp(str, str_const, sizeof(str_const) - 1) + struct hist_field; typedef u64 (*hist_field_fn_t) (struct hist_field *field, @@ -1881,8 +1884,8 @@ static int parse_action(char *str, struct hist_trigger_attrs *attrs) if (attrs->n_actions >= HIST_ACTIONS_MAX) return ret; - if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == 0) || - (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) { + if ((strcmp_const(str, "onmatch(") == 0) || + (strcmp_const(str, "onmax(") == 0)) { attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); if (!attrs->action_str[attrs->n_actions]) { ret = -ENOMEM; @@ -1899,34 +1902,34 @@ static int parse_assignment(char *str, struct hist_trigger_attrs *attrs) { int ret = 0; - if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || - (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { + if ((strcmp_const(str, "key=") == 0) || + (strcmp_const(str, "keys=") == 0)) { attrs->keys_str = kstrdup(str, GFP_KERNEL); if (!attrs->keys_str) { ret = -ENOMEM; goto out; } - } else if ((strncmp(str, "val=", sizeof("val=") - 1) == 0) || -(strncmp(str, "vals=", sizeof("vals=") - 1) == 0) || -(strncmp(str, "values=",
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
Hi Steve, On Wed, 2018-12-19 at 14:40 -0500, Steven Rostedt wrote: > On Tue, 18 Dec 2018 14:33:21 -0600 > Tom Zanussi wrote: > > > From: Tom Zanussi > > > > There's no need to use strlen() for static strings when the length > > is > > already known, so update trace_events_hist.c with sizeof() for > > those > > cases. > > > > Signed-off-by: Tom Zanussi > > --- > > kernel/trace/trace_events_hist.c | 38 +++- > > -- > > 1 file changed, 19 insertions(+), 19 deletions(-) > > > > diff --git a/kernel/trace/trace_events_hist.c > > b/kernel/trace/trace_events_hist.c > > index d29bf8a8e1dd..25d06b3ae1f6 100644 > > --- a/kernel/trace/trace_events_hist.c > > +++ b/kernel/trace/trace_events_hist.c > > @@ -507,7 +507,7 @@ static int synth_field_string_size(char *type) > > start = strstr(type, "char["); > > if (start == NULL) > > return -EINVAL; > > - start += strlen("char["); > > + start += sizeof("char[") - 1; > > > > end = strchr(type, ']'); > > if (!end || end < start) > > @@ -1843,8 +1843,8 @@ static int parse_action(char *str, struct > > hist_trigger_attrs *attrs) > > if (attrs->n_actions >= HIST_ACTIONS_MAX) > > return ret; > > > > - if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) || > > - (strncmp(str, "onmax(", strlen("onmax(")) == 0)) { > > + if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == > > 0) || > > + (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) { > > attrs->action_str[attrs->n_actions] = kstrdup(str, > > GFP_KERNEL); > > if (!attrs->action_str[attrs->n_actions]) { > > ret = -ENOMEM; > > @@ -1861,34 +1861,34 @@ static int parse_assignment(char *str, > > struct hist_trigger_attrs *attrs) > > { > > int ret = 0; > > > > - if ((strncmp(str, "key=", strlen("key=")) == 0) || > > - (strncmp(str, "keys=", strlen("keys=")) == 0)) { > > + if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || > > + (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { > > attrs->keys_str = kstrdup(str, GFP_KERNEL); > > I'll apply this as is, but since there's a lot of these, I wonder if > we > should make a marcro for this: > > #define strcmp_const(str, str_const) strncmp(str, str_const, > sizeof(str_const) - 1) > > ? > > This would help prevent bugs due to typos and bad cut and paste. > Yeah, I had considered it but wasn't sure it was worth it. Since you're suggesting it is, I can send another patch on top of these, or feel free if you want to too. ;-) Tom
Re: [PATCH 2/7] tracing: Change strlen to sizeof for hist trigger static strings
On Tue, 18 Dec 2018 14:33:21 -0600 Tom Zanussi wrote: > From: Tom Zanussi > > There's no need to use strlen() for static strings when the length is > already known, so update trace_events_hist.c with sizeof() for those > cases. > > Signed-off-by: Tom Zanussi > --- > kernel/trace/trace_events_hist.c | 38 +++--- > 1 file changed, 19 insertions(+), 19 deletions(-) > > diff --git a/kernel/trace/trace_events_hist.c > b/kernel/trace/trace_events_hist.c > index d29bf8a8e1dd..25d06b3ae1f6 100644 > --- a/kernel/trace/trace_events_hist.c > +++ b/kernel/trace/trace_events_hist.c > @@ -507,7 +507,7 @@ static int synth_field_string_size(char *type) > start = strstr(type, "char["); > if (start == NULL) > return -EINVAL; > - start += strlen("char["); > + start += sizeof("char[") - 1; > > end = strchr(type, ']'); > if (!end || end < start) > @@ -1843,8 +1843,8 @@ static int parse_action(char *str, struct > hist_trigger_attrs *attrs) > if (attrs->n_actions >= HIST_ACTIONS_MAX) > return ret; > > - if ((strncmp(str, "onmatch(", strlen("onmatch(")) == 0) || > - (strncmp(str, "onmax(", strlen("onmax(")) == 0)) { > + if ((strncmp(str, "onmatch(", sizeof("onmatch(") - 1) == 0) || > + (strncmp(str, "onmax(", sizeof("onmax(") - 1) == 0)) { > attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); > if (!attrs->action_str[attrs->n_actions]) { > ret = -ENOMEM; > @@ -1861,34 +1861,34 @@ static int parse_assignment(char *str, struct > hist_trigger_attrs *attrs) > { > int ret = 0; > > - if ((strncmp(str, "key=", strlen("key=")) == 0) || > - (strncmp(str, "keys=", strlen("keys=")) == 0)) { > + if ((strncmp(str, "key=", sizeof("key=") - 1) == 0) || > + (strncmp(str, "keys=", sizeof("keys=") - 1) == 0)) { > attrs->keys_str = kstrdup(str, GFP_KERNEL); I'll apply this as is, but since there's a lot of these, I wonder if we should make a marcro for this: #define strcmp_const(str, str_const) strncmp(str, str_const, sizeof(str_const) - 1) ? This would help prevent bugs due to typos and bad cut and paste. -- Steve > if (!attrs->keys_str) { > ret = -ENOMEM; > goto out; > } > - } else if ((strncmp(str, "val=", strlen("val=")) == 0) || > - (strncmp(str, "vals=", strlen("vals=")) == 0) || > - (strncmp(str, "values=", strlen("values=")) == 0)) { > + } else if ((strncmp(str, "val=", sizeof("val=") - 1) == 0) || > + (strncmp(str, "vals=", sizeof("vals=") - 1) == 0) || > + (strncmp(str, "values=", sizeof("values=") - 1) == 0)) { > attrs->vals_str = kstrdup(str, GFP_KERNEL); > if (!attrs->vals_str) { > ret = -ENOMEM; > goto out; > } > - } else if (strncmp(str, "sort=", strlen("sort=")) == 0) { > + } else if (strncmp(str, "sort=", sizeof("sort=") - 1) == 0) { > attrs->sort_key_str = kstrdup(str, GFP_KERNEL); > if (!attrs->sort_key_str) { > ret = -ENOMEM; > goto out; > } > - } else if (strncmp(str, "name=", strlen("name=")) == 0) { > + } else if (strncmp(str, "name=", sizeof("name=") - 1) == 0) { > attrs->name = kstrdup(str, GFP_KERNEL); > if (!attrs->name) { > ret = -ENOMEM; > goto out; > } > - } else if (strncmp(str, "clock=", strlen("clock=")) == 0) { > + } else if (strncmp(str, "clock=", sizeof("clock=") - 1) == 0) { > strsep(, "="); > if (!str) { > ret = -EINVAL; > @@ -1901,7 +1901,7 @@ static int parse_assignment(char *str, struct > hist_trigger_attrs *attrs) > ret = -ENOMEM; > goto out; > } > - } else if (strncmp(str, "size=", strlen("size=")) == 0) { > + } else if (strncmp(str, "size=", sizeof("size=") - 1) == 0) { > int map_bits = parse_map_size(str); > > if (map_bits < 0) { > @@ -3497,7 +3497,7 @@ static struct action_data *onmax_parse(char *str) > if (!onmax_fn_name || !str) > goto free; > > - if (strncmp(onmax_fn_name, "save", strlen("save")) == 0) { > + if (strncmp(onmax_fn_name, "save", sizeof("save") - 1) == 0) { > char *params = strsep(, ")"); > > if (!params) { > @@ -4302,8 +4302,8 @@ static int parse_actions(struct hist_trigger_data > *hist_data) > for (i = 0; i < hist_data->attrs->n_actions; i++) { > str = hist_data->attrs->action_str[i]; > > - if (strncmp(str, "onmatch(", strlen("onmatch(")) == 0) { > - char *action_str =