improved source checks

This commit is contained in:
Karel Miko 2017-03-15 21:29:09 +01:00
parent 9babf374ee
commit 39425a94c5
7 changed files with 39 additions and 17 deletions

View File

@ -6,7 +6,7 @@ bash printinfo.sh
make clean > /dev/null make clean > /dev/null
echo "checking..." echo "checking..."
./helper.pl --check-source --check-makefiles || exit 1 ./helper.pl --check-source --check-makefiles --check-defines|| exit 1
exit 0 exit 0

View File

@ -42,6 +42,18 @@ sub check_source {
push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i; push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/; push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/); push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
# in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmalloc\s*\(/;
push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\brealloc\s*\(/;
push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bcalloc\s*\(/;
push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bfree\s*\(/;
push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemset\s*\(/;
push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcmp\s*\(/;
push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcmp\s*\(/;
push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bclock\s*\(/;
push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bqsort\s*\(/;
$lineno++; $lineno++;
} }
for my $k (sort keys %$troubles) { for my $k (sort keys %$troubles) {
@ -54,6 +66,21 @@ sub check_source {
return $fails; return $fails;
} }
sub check_defines {
my $fails = 0;
my $cust_h = read_file("src/headers/tomcrypt_custom.h");
my $cryp_c = read_file("src/misc/crypt/crypt.c");
$cust_h =~ s|/\*.*?\*/||sg; # remove comments
$cryp_c =~ s|/\*.*?\*/||sg; # remove comments
my %def = map { $_ => 1 } map { $_ =~ s/^\s*#define\s+(LTC_\S+).*$/$1/; $_ } grep { /^\s*#define\s+LTC_\S+/ } split /\n/, $cust_h;
for my $d (sort keys %def) {
next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q$d\E/;
}
warn( $fails > 0 ? "check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
return $fails;
}
sub prepare_variable { sub prepare_variable {
my ($varname, @list) = @_; my ($varname, @list) = @_;
my $output = "$varname="; my $output = "$varname=";
@ -236,6 +263,7 @@ MARKER
} }
GetOptions( "check-source" => \my $check_source, GetOptions( "check-source" => \my $check_source,
"check-defines" => \my $check_defines,
"check-makefiles" => \my $check_makefiles, "check-makefiles" => \my $check_makefiles,
"update-makefiles" => \my $update_makefiles, "update-makefiles" => \my $update_makefiles,
"help" => \my $help "help" => \my $help
@ -243,6 +271,7 @@ GetOptions( "check-source" => \my $check_source,
my $failure; my $failure;
$failure ||= check_source() if $check_source; $failure ||= check_source() if $check_source;
$failure ||= check_defines() if $check_defines;
$failure ||= process_makefiles(0) if $check_makefiles; $failure ||= process_makefiles(0) if $check_makefiles;
$failure ||= process_makefiles(1) if $update_makefiles; $failure ||= process_makefiles(1) if $update_makefiles;

View File

@ -397,13 +397,6 @@ zipup: no_oops docs
gpg -b -a crypt-$(VERSION).tar.bz2 ; gpg -b -a crypt-$(VERSION).zip ; \ gpg -b -a crypt-$(VERSION).tar.bz2 ; gpg -b -a crypt-$(VERSION).zip ; \
mv -fv crypt* ~ ; rm -rf libtomcrypt-$(VERSION) mv -fv crypt* ~ ; rm -rf libtomcrypt-$(VERSION)
check_defines:
${silent} cat src/headers/tomcrypt_custom.h | grep '\#define[ \t]*LTC_' | sed -e 's@/\*@@g' -e 's@\*/@@g' -e 's@^[ \t]*@@g' \
| cut -d' ' -f 2 | sed -e 's@(x)@@g' | sort | uniq \
| grep -v -e 'LTC_ECC[0-9]*' -e 'LTC_DH[0-9]*' -e 'LTC_NO_' -e 'LTC_MUTEX' -e 'LTC_MPI' \
| xargs -I '{}' sh -c 'grep -q -m 1 -o {} src/misc/crypt/crypt.c || echo {} not found'
perlcritic: perlcritic:
perlcritic *.pl perlcritic *.pl

View File

@ -360,7 +360,7 @@ if (CTR != NULL) {
ctrcopy[z] = (ctrcopy[z] + 1) & 255; ctrcopy[z] = (ctrcopy[z] + 1) & 255;
if (ctrcopy[z]) break; if (ctrcopy[z]) break;
} }
memcpy(CTR, ctrcopy, 16); XMEMCPY(CTR, ctrcopy, 16);
} }
#ifdef LTC_CLEAN_STACK #ifdef LTC_CLEAN_STACK

View File

@ -161,7 +161,7 @@ int crypt_get_constant(const char* namein, int *valueout) {
int i; int i;
int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]); int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
for (i=0; i<_crypt_constants_len; i++) { for (i=0; i<_crypt_constants_len; i++) {
if (strcmp(_crypt_constants[i].name, namein) == 0) { if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
*valueout = _crypt_constants[i].value; *valueout = _crypt_constants[i].value;
return 0; return 0;
} }

View File

@ -248,7 +248,7 @@ int crypt_get_size(const char* namein, unsigned int *sizeout) {
int i; int i;
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]); int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
if (strcmp(_crypt_sizes[i].name, namein) == 0) { if (XSTRCMP(_crypt_sizes[i].name, namein) == 0) {
*sizeout = _crypt_sizes[i].size; *sizeout = _crypt_sizes[i].size;
return 0; return 0;
} }

View File

@ -19,7 +19,7 @@
/** /**
Compare two blocks of memory for inequality. Compare two blocks of memory for inequality.
The usage is similar to that of standard memcmp(), but you can only test The usage is similar to that of standard memcmp, but you can only test
if the memory is equal or not - you can not determine by how much the if the memory is equal or not - you can not determine by how much the
first different byte differs. first different byte differs.