Size: 2595
Comment:
|
Size: 1590
Comment: a few more examples
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
I wrote a few tools to do a multiline (git) grep, see [wiki:roelkluin/cvars_howto cvars howto]. To use these tools you first source [wiki:roelkluin/cvars cvars] and then do the searches in your git directory. '''Search for conditions that execute the same code in the if and else branches''' |
|
Line 8: | Line 11: |
#!/bin/bash | gres -A40 "^ if \(" \ "if \(.8.\) (@branch) else \9" | less }}} |
Line 10: | Line 17: |
# blank_it: replaces spaces with bracket expressions to match # optional/obligatory spaces for usage with egrep. bli2() { local int="[[:digit:]]" local hex="[[:xdigit:]]" local up_="[[:upper:]_]" local al_="[[:alpha:]_]" local AN_="[[:upper:][:digit:]_]" local an_="[[:alnum:]_]" local sp="[[:space:]]" local V="$al_+$an_*" |
'''Search for switch cases that execute the same code.''' {{{ gres -A40 "^ case@S[^:]*: $" \ "case@S[^:\n]*: \n (([^\n]*[^:{[:space:]] \n|\{.8.\}){2,}) (case@S[^:\n]*: ([^\n]*[^:{[:space:]] \n|\{.8.\})*)* case@S[^:\n]*: \1 (case|default)@S[^:\n]*:" | less }}} |
Line 23: | Line 28: |
local l="$1" l="${l//@V/$V}" # variable/function/struct name l="${l//@Q2/[^[:alnum:]_>.]}" # 1 char left of variable/function/struct name l="${l//@Q/[^[:alnum:]_]}" # 1 char right of variable/function/struct name l="${l//@d/($int+[uUlLfF]?|$int+[uU]?[lL][lL]?|0x$hex+|$int+[lL][lL][uU]|$int*\.$int+[fF]?)}" # a number l="${l//@K/$up_+$AN_*}" # an definition or macro name (uppercase) l="${l//@s/$sp*}" # optional space l="${l//@S/$sp+}" # obligatory space l="${l//@w/($V|\.|->|\[$sp*[^][]+$sp*\]|\($sp*[^)(]*$sp*\))+}" # a variable/array/member/unnested function or macro l="${l//\\\(...\\\)/\([^()]*(\([^()]*(\([^()]*\)[^()]*)*\)[^()]*)*\)}" # nested parentheses, 2 backrefs l="$(echo "$l" | sed -r "s/\\\\\{\.\.\.\\\\\}/\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*(\\\\\{\[^\}\{\]*\\\\\}\[^\}\{\]*)*\\\\\}\[^\}\{\]*)*\\\\\}/g")" # nested curly brackets, 2 backrefs echo "$l" | sed -r " :a s/($an_)$sp+($an_)/\1$sp+\2/g s/(\[\[:space:\]\]\*)*$sp+/$sp*/g $!{ N ba } " } |
'''Some functions' return codes should be audited, the scripts below searches for code sections where this lacks.''' {{{ Z="request_region kmalloc vmalloc kzalloc scsi_register create_proc_entry" for z in $Z; do echo "---[ $z ]---" gres -B1 -A20 "^((.* (@w) =)? (\( @V \** \) )*)? $z \(" \ "@Q2(\**@w) = (\**@w = )*(\( @V \** \) )* $z \(...\) ;.* (return|[&|(]) (\1 [=\!]= NULL|\!? \1|NULL [=\!]= \1) [&|)?;]" \ "@Q2(\**@w = )+(\( @V \** \) )* $z \(...\) ;" done | less |
Line 45: | Line 39: |
# echo a sed script to catch $1 from multiline grep and filter out $2. # e.g. grep -E -n -H -B1 -A10 "foo" ~/file | sed -n -r "$(ecsed "foo" "foo.*bar")" ecsed() { local sp="[[:space:]]"; # space local ns="[^[:space:]]"; # no space local bol="$ns*[:-][0-9]+[:-]" # begin of line [ -n "$2" ] && not="/$(bli2 "$2")/b" echo ":start /(--|$bol}$s)$/!{ N b start } s/$sp*\/\*+([^*]*|\*[^\/])*\*+\// /g # remove C style comment s/(\/\/[^\n]*)?\n$bol/\n/g # remove C99 comment $not s/^($ns*)[:-]([0-9]+)[:-]/---[ vi \1 +\2 ]---\n/ s/\n--$// /$(bli2 "$1")/p" } |
Z="request_irq register_netdev misc_register create_proc_profile kernel_thread do_fork" for z in $Z; do echo "---[ $z ]---" gres -B1 -A20 "^((.* (@w) =)? (\( @V \** \) )*)? $z \(" \ "@Q2(\**@w) = (\**@w = )*(\( @V \** \) )* $z \(...\) ;.* (return|[&|(]) (\1 (<|[\!>=]=) 0|\!? \1|0 (>|[\!<=]=) \1) [)&|?;]" \ "@Q2(\**@w = )+(\( @V \** \) )* $z \(...\) ;" done | less |
Line 66: | Line 48: |
To use, save and source this file. An example script below: search for kmallocing of variables without test of kmalloc success. goto your git Linux kernel directory and paste into the console: {{{ em='!' z="kmalloc" git grep -E -n -B3 -A20 "$(bli2 "@Q2$z \(")" | sed -n -r "$(ecsed \ "@Q2(@w) = (\(...\) )* $z \(...\) ;" \ "@Q2(@w) = (@w = )*(\(...\) )* $z \(...\) ;([^\n]*\n)* [^\n]*([&|(]|return) (\1 [=$em]= NULL|$em? \1|NULL [=$em]= \1) [&|)?]" \ )" | less em='!' }}} |
Roel Kluin
Email: roel.kluin@gmail.com)
I wrote a few tools to do a multiline (git) grep, see [wiki:roelkluin/cvars_howto cvars howto]. To use these tools you first source [wiki:roelkluin/cvars cvars] and then do the searches in your git directory.
Search for conditions that execute the same code in the if and else branches
gres -A40 "^ if \(" \ "if \(.8.\) (@branch) else \9" | less
Search for switch cases that execute the same code.
gres -A40 "^ case@S[^:]*: $" \ "case@S[^:\n]*: \n (([^\n]*[^:{[:space:]] \n|\{.8.\}){2,}) (case@S[^:\n]*: ([^\n]*[^:{[:space:]] \n|\{.8.\})*)* case@S[^:\n]*: \1 (case|default)@S[^:\n]*:" | less
Some functions' return codes should be audited, the scripts below searches for code sections where this lacks.
Z="request_region kmalloc vmalloc kzalloc scsi_register create_proc_entry" for z in $Z; do echo "---[ $z ]---" gres -B1 -A20 "^((.* (@w) =)? (\( @V \** \) )*)? $z \(" \ "@Q2(\**@w) = (\**@w = )*(\( @V \** \) )* $z \(...\) ;.* (return|[&|(]) (\1 [=\!]= NULL|\!? \1|NULL [=\!]= \1) [&|)?;]" \ "@Q2(\**@w = )+(\( @V \** \) )* $z \(...\) ;" done | less Z="request_irq register_netdev misc_register create_proc_profile kernel_thread do_fork" for z in $Z; do echo "---[ $z ]---" gres -B1 -A20 "^((.* (@w) =)? (\( @V \** \) )*)? $z \(" \ "@Q2(\**@w) = (\**@w = )*(\( @V \** \) )* $z \(...\) ;.* (return|[&|(]) (\1 (<|[\!>=]=) 0|\!? \1|0 (>|[\!<=]=) \1) [)&|?;]" \ "@Q2(\**@w = )+(\( @V \** \) )* $z \(...\) ;" done | less