diff mbox

[ovs-dev] checkpatch: Check for stdlib usage.

Message ID 20170522225139.3961-1-joe@ovn.org
State Changes Requested
Headers show

Commit Message

Joe Stringer May 22, 2017, 10:51 p.m. UTC
Many standard library functions are wrapped in OVS, so check for usage
of the original versions and suggest that authors replace them with the
OVS versions.

Signed-off-by: Joe Stringer <joe@ovn.org>
---
 utilities/checkpatch.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

Gregory Rose May 23, 2017, 12:30 a.m. UTC | #1
On Mon, 2017-05-22 at 15:51 -0700, Joe Stringer wrote:
> Many standard library functions are wrapped in OVS, so check for usage
> of the original versions and suggest that authors replace them with the
> OVS versions.
> 
> Signed-off-by: Joe Stringer <joe@ovn.org>
> ---
>  utilities/checkpatch.py | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
> index d486de81c8ff..4e2f5a42817f 100755
> --- a/utilities/checkpatch.py
> +++ b/utilities/checkpatch.py
> @@ -210,6 +210,37 @@ checks = [
>  ]
>  
> 
> +def regex_function_factory(func_name):
> +    regex = re.compile('[^x]%s\([^)]*\)' % func_name)
> +    return lambda x: regex.search(x) is not None
> +
> +
> +std_functions = [
> +        ('malloc', 'Use xmalloc() in place of malloc()'),
> +        ('calloc', 'Use xcmalloc() in place of calloc()'),
> +        ('zalloc', 'Use xzmalloc() in place of zalloc()'),
> +        ('realloc', 'Use xrealloc() in place of realloc()'),
> +        ('memdup', 'Use xmemdup() in place of memdup()'),
> +        ('memdup0', 'Use xmemdup0() in place of memdup0()'),
> +        ('strdup', 'Use xstrdup() in place of strdup()'),
> +        ('asprintf', 'Use xasprintf() in place of asprintf()'),
> +        ('vasprintf', 'Use xvasprintf() in place of vasprintf()'),
> +        ('2nrealloc', 'Use x2nrealloc() in place of 2nrealloc()'),
> +        ('strlcpy', 'Use ovs_strlcpy() in place of strlcpy()'),
> +        ('strzcpy', 'Use ovs_strzcpy() in place of strzcpy()'),
> +        ('strerror', 'Use ovs_strerror() in place of strerror()'),
> +        ('sleep', 'Use xsleep() in place of sleep()'),
> +        ('abort', 'Use xabort() in place of abort()'),
> +        ('error', 'Use xerror() in place of error()'),
> +]
> +checks += [
> +    {'regex': '(.c|.h)(.in)?$',
> +     'match_name': None,
> +     'check': regex_function_factory(function_name),
> +     'print': lambda: print_error(description)}
> +for function_name, description in std_functions]
> +
> +
>  def get_file_type_checks(filename):
>      """Returns the list of checks for a file based on matching the filename
>         against regex."""

Good idea.

thanks!

Acked-by: Greg Rose <gvrose8192@gmail.com>
Ben Pfaff May 23, 2017, 12:53 a.m. UTC | #2
On Mon, May 22, 2017 at 03:51:39PM -0700, Joe Stringer wrote:
> Many standard library functions are wrapped in OVS, so check for usage
> of the original versions and suggest that authors replace them with the
> OVS versions.
> 
> Signed-off-by: Joe Stringer <joe@ovn.org>

Thanks for working to improve the patch checks!

> +def regex_function_factory(func_name):
> +    regex = re.compile('[^x]%s\([^)]*\)' % func_name)
> +    return lambda x: regex.search(x) is not None
> +
> +
> +std_functions = [
> +        ('malloc', 'Use xmalloc() in place of malloc()'),
> +        ('calloc', 'Use xcmalloc() in place of calloc()'),

xcalloc, not xcmalloc

> +        ('zalloc', 'Use xzmalloc() in place of zalloc()'),

I don't think there's a zalloc function.

> +        ('realloc', 'Use xrealloc() in place of realloc()'),
> +        ('memdup', 'Use xmemdup() in place of memdup()'),
> +        ('memdup0', 'Use xmemdup0() in place of memdup0()'),

I don't think there's a memdup0 function.

> +        ('strdup', 'Use xstrdup() in place of strdup()'),
> +        ('asprintf', 'Use xasprintf() in place of asprintf()'),
> +        ('vasprintf', 'Use xvasprintf() in place of vasprintf()'),
> +        ('2nrealloc', 'Use x2nrealloc() in place of 2nrealloc()'),

I don't think there's a 2nrealloc function.

> +        ('strlcpy', 'Use ovs_strlcpy() in place of strlcpy()'),
> +        ('strzcpy', 'Use ovs_strzcpy() in place of strzcpy()'),

I don't think there's a strzcpy function, but ovs_strzcpy() might be a
substitute for strncpy.

It can often be a good idea to use ovs_strlcpy() in place of plain
strcpy().

> +        ('strerror', 'Use ovs_strerror() in place of strerror()'),
> +        ('sleep', 'Use xsleep() in place of sleep()'),
> +        ('abort', 'Use xabort() in place of abort()'),

I don't think we have an xabort function.

> +        ('error', 'Use xerror() in place of error()'),

I don't know of either error *or* xerror functions.

Thanks,

Ben.
Joe Stringer May 23, 2017, 1:18 a.m. UTC | #3
On 22 May 2017 at 17:53, Ben Pfaff <blp@ovn.org> wrote:
> On Mon, May 22, 2017 at 03:51:39PM -0700, Joe Stringer wrote:
>> Many standard library functions are wrapped in OVS, so check for usage
>> of the original versions and suggest that authors replace them with the
>> OVS versions.
>>
>> Signed-off-by: Joe Stringer <joe@ovn.org>
>
> Thanks for working to improve the patch checks!
>
>> +def regex_function_factory(func_name):
>> +    regex = re.compile('[^x]%s\([^)]*\)' % func_name)
>> +    return lambda x: regex.search(x) is not None
>> +
>> +
>> +std_functions = [
>> +        ('malloc', 'Use xmalloc() in place of malloc()'),
>> +        ('calloc', 'Use xcmalloc() in place of calloc()'),
>
> xcalloc, not xcmalloc
>
>> +        ('zalloc', 'Use xzmalloc() in place of zalloc()'),
>
> I don't think there's a zalloc function.
>
>> +        ('realloc', 'Use xrealloc() in place of realloc()'),
>> +        ('memdup', 'Use xmemdup() in place of memdup()'),
>> +        ('memdup0', 'Use xmemdup0() in place of memdup0()'),
>
> I don't think there's a memdup0 function.
>
>> +        ('strdup', 'Use xstrdup() in place of strdup()'),
>> +        ('asprintf', 'Use xasprintf() in place of asprintf()'),
>> +        ('vasprintf', 'Use xvasprintf() in place of vasprintf()'),
>> +        ('2nrealloc', 'Use x2nrealloc() in place of 2nrealloc()'),
>
> I don't think there's a 2nrealloc function.
>
>> +        ('strlcpy', 'Use ovs_strlcpy() in place of strlcpy()'),
>> +        ('strzcpy', 'Use ovs_strzcpy() in place of strzcpy()'),
>
> I don't think there's a strzcpy function, but ovs_strzcpy() might be a
> substitute for strncpy.
>
> It can often be a good idea to use ovs_strlcpy() in place of plain
> strcpy().
>
>> +        ('strerror', 'Use ovs_strerror() in place of strerror()'),
>> +        ('sleep', 'Use xsleep() in place of sleep()'),
>> +        ('abort', 'Use xabort() in place of abort()'),
>
> I don't think we have an xabort function.
>
>> +        ('error', 'Use xerror() in place of error()'),
>
> I don't know of either error *or* xerror functions.

:-) Thanks for looking through. I started from lib/util.h and assumed
that anything prefaced with 'x' replaces a common library function.

For xabort/xerror I think I was looking at ovs_abort() and
ovs_error(). The former is probably a useful substitute but perhaps
not the latter.

I'll send a v2.
diff mbox

Patch

diff --git a/utilities/checkpatch.py b/utilities/checkpatch.py
index d486de81c8ff..4e2f5a42817f 100755
--- a/utilities/checkpatch.py
+++ b/utilities/checkpatch.py
@@ -210,6 +210,37 @@  checks = [
 ]
 
 
+def regex_function_factory(func_name):
+    regex = re.compile('[^x]%s\([^)]*\)' % func_name)
+    return lambda x: regex.search(x) is not None
+
+
+std_functions = [
+        ('malloc', 'Use xmalloc() in place of malloc()'),
+        ('calloc', 'Use xcmalloc() in place of calloc()'),
+        ('zalloc', 'Use xzmalloc() in place of zalloc()'),
+        ('realloc', 'Use xrealloc() in place of realloc()'),
+        ('memdup', 'Use xmemdup() in place of memdup()'),
+        ('memdup0', 'Use xmemdup0() in place of memdup0()'),
+        ('strdup', 'Use xstrdup() in place of strdup()'),
+        ('asprintf', 'Use xasprintf() in place of asprintf()'),
+        ('vasprintf', 'Use xvasprintf() in place of vasprintf()'),
+        ('2nrealloc', 'Use x2nrealloc() in place of 2nrealloc()'),
+        ('strlcpy', 'Use ovs_strlcpy() in place of strlcpy()'),
+        ('strzcpy', 'Use ovs_strzcpy() in place of strzcpy()'),
+        ('strerror', 'Use ovs_strerror() in place of strerror()'),
+        ('sleep', 'Use xsleep() in place of sleep()'),
+        ('abort', 'Use xabort() in place of abort()'),
+        ('error', 'Use xerror() in place of error()'),
+]
+checks += [
+    {'regex': '(.c|.h)(.in)?$',
+     'match_name': None,
+     'check': regex_function_factory(function_name),
+     'print': lambda: print_error(description)}
+for function_name, description in std_functions]
+
+
 def get_file_type_checks(filename):
     """Returns the list of checks for a file based on matching the filename
        against regex."""