Size: 3370
Comment:
|
← Revision 14 as of 2017-12-30 01:30:00 ⇥
Size: 1584
Comment: converted to 1.6 markup
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
All code below is GPL v2 | I wrote a few tools to do a multiline (git) grep, see [[roelkluin/cvars_howto|cvars howto]]. To use these tools you first source [[roelkluin/cvars|cvars]] and then do the searches in your git directory. |
Line 9: | Line 9: |
'''Search for conditions that execute the same code in the if and else branches''' | |
Line 10: | Line 11: |
#!/bin/bash | gres -A40 "^ if \(" \ "if \(.8.\) (@branch) else \9" | less }}} |
Line 12: | 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_*" local em='!' |
'''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 26: | 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//\\$em/$em}" # Because bash bangs otherwise 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 } " } # echo a sed script to catch from multiline grep $n (where n=$#) and filter out $1 up to ${n-1}. # e.g. grep -E -n -H -B1 -A10 "foo" ~/file | sed -n -r "$(ecsed2 "foo.*bar" "foo")" # searches for lines with `foo', not followed by `bar' ecsed() { local app; if [ "${1:0:6}" = "--PrE=" ]; then app="${1:6}"; shift; fi local sp="[[:space:]]"; # space local ns="[^[:space:]]"; # no space local bol="$ns*[:-][0-9]+[:-]" # begin of line local not; while [ -n "$2" ]; do not="$not/$(bli2 "$app$1")/b;" shift; done 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 "$app$1")/p" } # gsl does something like # git grep -E -n [-opts] "[parsed $1]" | sed -n -r "[parsed ${@:2}]" gres() { local opts; local o; local append; local gq; local sq; while [ $# -ne 0 ]; do [ "${1:0:1}" != "-" ] && break; [ "${1:0:6}" != "--PrE=" ] && opts="$opts $1" || gq="${1:6}"; shift; done if [ -z "$gq" ]; then gq="$(bli2 "$1")"; shift; sq="$(ecsed2 "$@")" else gq="$(bli2 "$gq")" sq="$(ecsed2 "--PrE=$gq" "$@")"; fi git grep -E -n $opts "$gq" | sed -n -r "$sq" } }}} To use, save and source this file. '''Search for allocating of variables without test of success.''' goto your git Linux kernel directory and paste into the console: |
'''Some functions' return codes should be audited, the scripts below searches for code sections where this lacks.''' |
Line 107: | Line 30: |
z="[kv][zm]alloc" | Z="request_region kmalloc vmalloc kzalloc scsi_register create_proc_entry" for z in $Z; do echo "---[ $z ]---" |
Line 111: | Line 36: |
"@Q2(\**@w = )+(\( @V \** \) )* $z \(...\) ;" | less | "@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 |
Roel Kluin
Email: roel.kluin@gmail.com)
I wrote a few tools to do a multiline (git) grep, see cvars howto. To use these tools you first source 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