Some things I fixed, when having a look at the sources.

Just two points for code reviewing:

1.
#define NONNULL(...) __attribute__ ((nonnull(__VA_ARGS__)))
is contains a C99 feature (...), but there are also C99 long long constants 
somewhere in the code (if you mind C89 compliancy).

2.
In cast128.c I removed the wiping of t, l and r. Instead I set t=0 at the 
beginning of the loops (It seemed to be used uninitialized in F1 macro).
Please just have a short look into it - maybe the "wiping" has some 
undocumented deeper meaning !?

Regards, Tim
From f2ff94fe5f262901097e38d6f19e3438ba5c999d Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <[email protected]>
Date: Wed, 5 Sep 2012 16:54:43 +0200
Subject: [PATCH 1/4] fixed some clang analyzer issues

---
 aclocal.m4                  |    2 ++
 cast128.c                   |    7 ++-----
 examples/eratosthenes.c     |    5 +++++
 examples/io.c               |   13 ++++++++++---
 examples/nettle-benchmark.c |    2 +-
 tools/misc.h                |   13 ++-----------
 tools/nettle-hash.c         |    3 ++-
 7 files changed, 24 insertions(+), 21 deletions(-)

diff --git a/aclocal.m4 b/aclocal.m4
index a94c20d..4a56165 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -337,10 +337,12 @@ AH_BOTTOM(
 # define NORETURN __attribute__ ((__noreturn__))
 # define PRINTF_STYLE(f, a) __attribute__ ((__format__ (__printf__, f, a)))
 # define UNUSED __attribute__ ((__unused__))
+# define NONNULL(...) __attribute__ ((nonnull(__VA_ARGS__)))
 #else
 # define NORETURN
 # define PRINTF_STYLE(f, a)
 # define UNUSED
+# define NONNULL(a)
 #endif
 ])])
 
diff --git a/cast128.c b/cast128.c
index 5e53bd9..2192e98 100644
--- a/cast128.c
+++ b/cast128.c
@@ -82,6 +82,7 @@ cast128_encrypt(const struct cast128_ctx *ctx,
       /* Get inblock into l,r */
       l = READ_UINT32(src);
       r = READ_UINT32(src+4);
+      t = 0;
 
       /* Do the work */
       F1(l, r,  0);
@@ -106,8 +107,6 @@ cast128_encrypt(const struct cast128_ctx *ctx,
       /* Put l,r into outblock */
       WRITE_UINT32(dst, r);
       WRITE_UINT32(dst + 4, l);
-      /* Wipe clean */
-      t = l = r = 0;
     }
 }
 
@@ -126,6 +125,7 @@ cast128_decrypt(const struct cast128_ctx *ctx,
       /* Get inblock into l,r */
       r = READ_UINT32(src);
       l = READ_UINT32(src+4);
+      t = 0;
 
       /* Do the work */
       /* Only do full 16 rounds if key length > 80 bits */
@@ -151,9 +151,6 @@ cast128_decrypt(const struct cast128_ctx *ctx,
       /* Put l,r into outblock */
       WRITE_UINT32(dst, l);
       WRITE_UINT32(dst + 4, r);
-
-      /* Wipe clean */
-      t = l = r = 0;
     }
 }
 
diff --git a/examples/eratosthenes.c b/examples/eratosthenes.c
index 2b9d04f..0eea941 100644
--- a/examples/eratosthenes.c
+++ b/examples/eratosthenes.c
@@ -399,5 +399,10 @@ main (int argc, char **argv)
 	    printf("%lu\n", n);
 	}
     }
+
+  /* silence static analyzers */
+  free(block);
+  free(sieve);
+
   return EXIT_SUCCESS;
 }
diff --git a/examples/io.c b/examples/io.c
index 7b2289c..cda9ed3 100644
--- a/examples/io.c
+++ b/examples/io.c
@@ -125,7 +125,10 @@ read_file(const char *name, unsigned max_size, char **contents)
   fclose(f);
 
   /* NUL-terminate the data. */
-  buffer[done] = '\0';
+  if (buffer)
+    buffer[done] = '\0';
+  else
+    done = 0;
   *contents = buffer;
   
   return done;
@@ -156,7 +159,7 @@ int
 simple_random(struct yarrow256_ctx *ctx, const char *name)
 {
   unsigned length;
-  char *buffer;
+  char *buffer = NULL; /* silence static analyzer */
 
   if (name)
     length = read_file(name, 0, &buffer);
@@ -164,7 +167,11 @@ simple_random(struct yarrow256_ctx *ctx, const char *name)
     length = read_file(RANDOM_DEVICE, 20, &buffer);
   
   if (!length)
-    return 0;
+    {
+      if (buffer)
+        free(buffer);
+      return 0;
+    }
 
   yarrow256_seed(ctx, length, buffer);
 
diff --git a/examples/nettle-benchmark.c b/examples/nettle-benchmark.c
index b76a91c..48e53d0 100644
--- a/examples/nettle-benchmark.c
+++ b/examples/nettle-benchmark.c
@@ -96,7 +96,7 @@ static double frequency = 0.0;
 #define BENCH_ITERATIONS 10
 #endif
 
-static void
+static void NORETURN
 die(const char *format, ...)
 {
   va_list args;
diff --git a/tools/misc.h b/tools/misc.h
index 70c9eeb..af55998 100644
--- a/tools/misc.h
+++ b/tools/misc.h
@@ -28,19 +28,10 @@
 #endif
 
 void
-die(const char *format, ...)
-#if __GNUC___
-     __attribute__((__format__ (__printf__,1, 2)))
-     __attribute__((__noreturn__))
-#endif
-     ;
+die(const char *format, ...) PRINTF_STYLE(1,2) NORETURN;
 
 void
-werror(const char *format, ...)
-#if __GNUC___
-     __attribute__((__format__ (__printf__,1, 2)))
-#endif
-     ;
+werror(const char *format, ...) PRINTF_STYLE(1,2);
 
 void *
 xalloc(size_t size);
diff --git a/tools/nettle-hash.c b/tools/nettle-hash.c
index 5710216..842f9ff 100644
--- a/tools/nettle-hash.c
+++ b/tools/nettle-hash.c
@@ -39,6 +39,7 @@
 
 #define BUFSIZE 16384
 
+
 static void
 list_algorithms (void)
 {
@@ -51,7 +52,7 @@ list_algorithms (void)
 	    alg->name, alg->digest_size, alg->block_size);
 };
 
-static const struct nettle_hash *
+static const struct nettle_hash * NONNULL(1)
 find_algorithm (const char *name)
 {
   const struct nettle_hash *alg;
-- 
1.7.10.4

From b5bc1345dddab622ef07e09507f91a0ff54c8514 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <[email protected]>
Date: Fri, 7 Sep 2012 09:55:25 +0200
Subject: [PATCH 2/4] silence compiler warnings for example/base16enc.c

---
 examples/base16enc.c |   16 ----------------
 1 file changed, 16 deletions(-)

diff --git a/examples/base16enc.c b/examples/base16enc.c
index c3cb58f..3fc410e 100644
--- a/examples/base16enc.c
+++ b/examples/base16enc.c
@@ -47,21 +47,6 @@
 int
 main(int argc UNUSED, char **argv UNUSED)
 {
-
-  /* "buffer" will hold the bytes from disk: */
-  uint8_t * buffer = (uint8_t *) malloc (CHUNK_SIZE * sizeof(uint8_t));
-  if (buffer == NULL) {
-    fprintf (stderr, "Cannot allocate read buffer.\n");
-    return EXIT_FAILURE;
-  }
-
-  /* "result" will hold bytes before output: */
-  uint8_t * result = (uint8_t *) malloc (ENCODED_SIZE * sizeof(uint8_t));
-  if (result == NULL) {
-    fprintf (stderr, "Cannot allocate write buffer.\n");
-    return EXIT_FAILURE;
-  }
-
 #ifdef WIN32
   _setmode(0, O_BINARY);
 #endif
@@ -107,4 +92,3 @@ main(int argc UNUSED, char **argv UNUSED)
 	}
     }
 }
-
-- 
1.7.10.4

From 67186919ffa0ba6b13c57ea28e4fa29b71a0ae03 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <[email protected]>
Date: Fri, 7 Sep 2012 10:26:36 +0200
Subject: [PATCH 3/4] removed unused variables from pgp-encode.c and
 rsa2openpgp.c

---
 pgp-encode.c  |    1 -
 rsa2openpgp.c |    1 -
 2 files changed, 2 deletions(-)

diff --git a/pgp-encode.c b/pgp-encode.c
index 9a69922..f84373c 100644
--- a/pgp-encode.c
+++ b/pgp-encode.c
@@ -246,7 +246,6 @@ pgp_put_rsa_sha1_signature(struct nettle_buffer *buffer,
   unsigned hash_end;
   unsigned sub_packet_start;
   uint8_t trailer[6];
-  uint8_t digest16[2];
   mpz_t s;
   
   /* Signature packet. The packet could reasonably be both smaller and
diff --git a/rsa2openpgp.c b/rsa2openpgp.c
index c4666f3..4c62f49 100644
--- a/rsa2openpgp.c
+++ b/rsa2openpgp.c
@@ -64,7 +64,6 @@ rsa_keypair_to_openpgp(struct nettle_buffer *buffer,
   time_t now = time(NULL);
 
   unsigned key_start;
-  unsigned key_length;
   unsigned userid_start;
   
   struct sha1_ctx key_hash;
-- 
1.7.10.4

From 74eb0f0b91175f2d77074a9112b5fa6c8f9fa833 Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <[email protected]>
Date: Fri, 7 Sep 2012 10:27:51 +0200
Subject: [PATCH 4/4] use PRINTF_STYLE wherte possible

---
 desdata.c                   |    2 +-
 examples/io.h               |    6 +-----
 examples/nettle-benchmark.c |    2 +-
 3 files changed, 3 insertions(+), 7 deletions(-)

diff --git a/desdata.c b/desdata.c
index fc89c2d..6671b5d 100644
--- a/desdata.c
+++ b/desdata.c
@@ -62,7 +62,7 @@ int sorder[] = {
 	7, 5, 3, 1, 6, 4, 2, 0,
 };
 
-int printf(const char *, ...);
+int printf(const char *, ...) PRINTF_STYLE(1,2);
 
 int
 main(int argc UNUSED, char **argv UNUSED)
diff --git a/examples/io.h b/examples/io.h
index ff4a18d..e83b7eb 100644
--- a/examples/io.h
+++ b/examples/io.h
@@ -37,11 +37,7 @@ void *
 xalloc(size_t size);
 
 void
-werror(const char *format, ...)
-#if __GNUC___
-     __attribute__((__format__ (__printf__,1, 2)))
-#endif
-     ;
+werror(const char *format, ...) PRINTF_STYLE(1,2);
 
 /* If size is > 0, read at most that many bytes. If size == 0,
  * read until EOF. Allocates the buffer dynamically. */
diff --git a/examples/nettle-benchmark.c b/examples/nettle-benchmark.c
index 48e53d0..3d07868 100644
--- a/examples/nettle-benchmark.c
+++ b/examples/nettle-benchmark.c
@@ -96,7 +96,7 @@ static double frequency = 0.0;
 #define BENCH_ITERATIONS 10
 #endif
 
-static void NORETURN
+static void NORETURN PRINTF_STYLE(1,2)
 die(const char *format, ...)
 {
   va_list args;
-- 
1.7.10.4

_______________________________________________
nettle-bugs mailing list
[email protected]
http://lists.lysator.liu.se/mailman/listinfo/nettle-bugs

Reply via email to