Skip to content

Commit

Permalink
Improve packaging/var-checker.
Browse files Browse the repository at this point in the history
Make var-checker compare the variable type of the extern vars to ensure
that they are all consistent. Fix the remaining issues.
  • Loading branch information
WayneD committed Nov 14, 2024
1 parent 62bb9bb commit fa28c5d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extern int protect_args;
extern int relative_paths;
extern int sanitize_paths;
extern int curr_dir_depth;
extern int curr_dir_len;
extern unsigned int curr_dir_len;
extern int module_id;
extern int rsync_port;
extern int whole_file;
Expand Down
46 changes: 37 additions & 9 deletions packaging/var-checker
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import os, sys, re, argparse, glob

VARS_RE = re.compile(r'^(?!(?:extern|enum)\s)([a-zA-Z]\S*\s+.*);', re.M)
VARS_RE = re.compile(r'^(?!(?:extern|enum)\s)([a-zA-Z][^ \n\t:]*\s+.*);', re.M)
EXTERNS_RE = re.compile(r'^extern\s+(.*);', re.M)

types = { }
sizes = { }

def main():
Expand Down Expand Up @@ -68,19 +69,46 @@ def parse_vars(fn, lines):
for line in lines:
line = re.sub(r'\s*\{.*\}', '', line)
line = re.sub(r'\s*\(.*\)', '', line)
for item in re.split(r'\s*,\s*', line):
item = re.sub(r'\s*=.*', '', item)
m = re.search(r'(?P<var>\w+)(?P<sz>\[.*?\])?$', item)
line = re.sub(r'\s*=\s*[^,]*', '', line)
m = re.search(r'^(?:(?:static|extern)\s+)?(?P<type>[^\[,]+?)(?P<vars>\w+([\[,].+)?)$', line)
if not m:
print(f"Bogus match? ({line})")
continue
items = m['vars']
main_type = m['type'].strip()
mt_len = len(main_type)
main_type = main_type.rstrip('*')
first_stars = '*' * (mt_len - len(main_type))
if first_stars:
main_type = main_type.rstrip()
items = first_stars + items
for item in re.split(r'\s*,\s*', items):
m = re.search(r'(?P<stars>\*+\s*)?(?P<var>\w+)(?P<sz>\[.*?\])?$', item)
if not m:
print(f"Bogus match? ({item})")
continue
if m['sz']:
if m['var'] in sizes:
if sizes[m['var']] != m['sz']:
typ = main_type
if m['stars']:
typ = typ + m['stars'].strip()
chk = [
'type', typ, types,
'size', m['sz'], sizes,
]
while chk:
label = chk.pop(0)
new = chk.pop(0)
lst = chk.pop(0)
if not new:
continue
if label == 'type':
new = ' '.join(new.split()).replace(' *', '*')
if m['var'] in lst:
old = lst[m['var']]
if new != old:
var = m['var']
print(fn, f'has inconsistent size for "{var}":', m['sz'], 'vs', sizes[var])
print(fn, f'has inconsistent {label} for "{var}":', new, 'vs', old)
else:
sizes[m['var']] = m['sz']
lst[m['var']] = new
ret.append(m['var'])
return ret

Expand Down
2 changes: 1 addition & 1 deletion t_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int preallocate_files = 0;
int protect_args = 0;
int module_id = -1;
int relative_paths = 0;
int module_dirlen = 0;
unsigned int module_dirlen = 0;
int preserve_xattrs = 0;
int preserve_perms = 0;
int preserve_executability = 0;
Expand Down

0 comments on commit fa28c5d

Please sign in to comment.