Browse Source

improved source checks

Karel Miko 8 years ago
parent
commit
39425a94c5

+ 1 - 1
check_source.sh

@@ -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
 
 

+ 34 - 5
helper.pl

@@ -37,11 +37,23 @@ sub check_source {
     my $content = read_file($file);
     my $content = read_file($file);
     push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
     push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
     for my $l (split /\n/, $content) {
     for my $l (split /\n/, $content) {
-      push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
-      push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
-      push @{$troubles->{tab}}, $lineno            if $l =~ /\t/ && basename($file) !~ /^makefile/i;
-      push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
-      push @{$troubles->{cpp_comment}},    $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
+      push @{$troubles->{merge_conflict}},   $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
+      push @{$troubles->{trailing_space}},   $lineno if $l =~ / $/;
+      push @{$troubles->{tab}},              $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
+      push @{$troubles->{non_ascii_char}},   $lineno if $l =~ /[^[:ascii:]]/;
+      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;
 
 

+ 0 - 7
makefile

@@ -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
 
 

+ 1 - 1
src/encauth/ccm/ccm_memory_ex.c

@@ -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

+ 1 - 1
src/misc/crypt/crypt_constants.c

@@ -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;
         }
         }

+ 1 - 1
src/misc/crypt/crypt_sizes.c

@@ -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;
         }
         }

+ 1 - 1
src/misc/mem_neq.c

@@ -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.