From f2ff94fe5f262901097e38d6f19e3438ba5c999d Mon Sep 17 00:00:00 2001
From: Tim Ruehsen <tim.ruehsen@gmx.de>
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

