diff --git a/src/aec.c b/src/aec.c
index 44234a8011dc92c99182d75bef3815d0932a9ae8..e537327e397f1ce9f5884cfe702ec389a81cc60b 100644
--- a/src/aec.c
+++ b/src/aec.c
@@ -56,14 +56,33 @@ int get_param(unsigned int *param, int *iarg, char *argv[])
     return 0;
 }
 
+void usage(void)
+{
+    fprintf(stderr, "NAME\n\taec - encode or decode files ");
+    fprintf(stderr, "with Adaptive Entropy Coding\n\n");
+    fprintf(stderr, "SYNOPSIS\n\taec [OPTION]... SOURCE DEST\n");
+    fprintf(stderr, "\nOPTIONS\n");
+    fprintf(stderr, "\t-3\n\t\t24 bit samples are stored in 3 bytes\n");
+    fprintf(stderr, "\t-N\n\t\tdisable pre/post processing\n");
+    fprintf(stderr, "\t-b size\n\t\tinternal buffer size in bytes\n");
+    fprintf(stderr, "\t-d\n\t\tdecode SOURCE. If -d is not used: encode.\n");
+    fprintf(stderr, "\t-j samples\n\t\tblock size in samples\n");
+    fprintf(stderr, "\t-m\n\t\tsamples are MSB first. Default is LSB\n");
+    fprintf(stderr, "\t-n bits\n\t\tbits per sample\n");
+    fprintf(stderr, "\t-p\n\t\tpad RSI to byte boundary\n");
+    fprintf(stderr, "\t-r blocks\n\t\treference sample interval in blocks\n");
+    fprintf(stderr, "\t-s\n\t\tsamples are signed. Default is unsigned\n");
+    fprintf(stderr, "\t-t\n\t\tuse restricted set of code options\n\n");
+}
+
 int main(int argc, char *argv[])
 {
     struct aec_stream strm;
-    unsigned char *in;
-    unsigned char *out;
+    unsigned char *in = NULL;
+    unsigned char *out = NULL;
     size_t total_out;
     unsigned int chunk;
-    int status;
+    int status = 0;
     int input_avail, output_avail;
     char *infn, *outfn;
     FILE *infp, *outfp;
@@ -81,8 +100,10 @@ int main(int argc, char *argv[])
 
     while (iarg < argc - 2) {
         opt = argv[iarg];
-        if (opt[0] != '-')
-            goto FAIL;
+        if (opt[0] != '-') {
+            usage();
+            goto DESTRUCT;
+        }
         switch (opt[1]) {
         case '3':
             strm.flags |= AEC_DATA_3BYTE;
@@ -91,29 +112,37 @@ int main(int argc, char *argv[])
             strm.flags &= ~AEC_DATA_PREPROCESS;
             break;
         case 'b':
-            if (get_param(&chunk, &iarg, argv))
-                goto FAIL;
+            if (get_param(&chunk, &iarg, argv)) {
+                usage();
+                goto DESTRUCT;
+            }
             break;
         case 'd':
             dflag = 1;
             break;
         case 'j':
-            if (get_param(&strm.block_size, &iarg, argv))
-                goto FAIL;
+            if (get_param(&strm.block_size, &iarg, argv)) {
+                usage();
+                goto DESTRUCT;
+            }
             break;
         case 'm':
             strm.flags |= AEC_DATA_MSB;
             break;
         case 'n':
-            if (get_param(&strm.bits_per_sample, &iarg, argv))
-                goto FAIL;
+            if (get_param(&strm.bits_per_sample, &iarg, argv)) {
+                usage();
+                goto DESTRUCT;
+            }
             break;
         case 'p':
             strm.flags |= AEC_PAD_RSI;
             break;
         case 'r':
-            if (get_param(&strm.rsi, &iarg, argv))
-                goto FAIL;
+            if (get_param(&strm.rsi, &iarg, argv)) {
+                usage();
+                goto DESTRUCT;
+            }
             break;
         case 's':
             strm.flags |= AEC_DATA_SIGNED;
@@ -122,13 +151,16 @@ int main(int argc, char *argv[])
             strm.flags |= AEC_RESTRICTED;
             break;
         default:
-            goto FAIL;
+            usage();
+            goto DESTRUCT;
         }
         iarg++;
     }
 
-    if (argc - iarg < 2)
-        goto FAIL;
+    if (argc - iarg < 2) {
+        usage();
+        goto DESTRUCT;
+    }
 
     infn = argv[iarg];
     outfn = argv[iarg + 1];
@@ -145,8 +177,10 @@ int main(int argc, char *argv[])
     out = (unsigned char *)malloc(chunk);
     in = (unsigned char *)malloc(chunk);
 
-    if (in == NULL || out == NULL)
-        exit(-1);
+    if (in == NULL || out == NULL) {
+        status = 99;
+        goto DESTRUCT;
+    }
 
     total_out = 0;
     strm.avail_in = 0;
@@ -158,11 +192,13 @@ int main(int argc, char *argv[])
 
     if ((infp = fopen(infn, "rb")) == NULL) {
         fprintf(stderr, "ERROR: cannot open input file %s\n", infn);
-        return 1;
+        status = 99;
+        goto DESTRUCT;
     }
     if ((outfp = fopen(outfn, "wb")) == NULL) {
         fprintf(stderr, "ERROR: cannot open output file %s\n", infn);
-        return 1;
+        status = 99;
+        goto DESTRUCT;
     }
 
     if (dflag)
@@ -172,7 +208,7 @@ int main(int argc, char *argv[])
 
     if (status != AEC_OK) {
         fprintf(stderr, "ERROR: initialization failed (%d)\n", status);
-        return 1;
+        goto DESTRUCT;
     }
 
     while(input_avail || output_avail) {
@@ -190,7 +226,7 @@ int main(int argc, char *argv[])
 
         if (status != AEC_OK) {
             fprintf(stderr, "ERROR: %i\n", status);
-            return 1;
+            goto DESTRUCT;
         }
 
         if (strm.total_out - total_out > 0) {
@@ -210,7 +246,7 @@ int main(int argc, char *argv[])
     } else {
         if ((status = aec_encode(&strm, AEC_FLUSH)) != AEC_OK) {
             fprintf(stderr, "ERROR: while flushing output (%i)\n", status);
-            return 1;
+            goto DESTRUCT;
         }
 
         if (strm.total_out - total_out > 0)
@@ -221,25 +257,11 @@ int main(int argc, char *argv[])
 
     fclose(infp);
     fclose(outfp);
-    free(in);
-    free(out);
-    return 0;
 
-FAIL:
-    fprintf(stderr, "NAME\n\taec - encode or decode files ");
-    fprintf(stderr, "with Adaptive Entropy Coding\n\n");
-    fprintf(stderr, "SYNOPSIS\n\taec [OPTION]... SOURCE DEST\n");
-    fprintf(stderr, "\nOPTIONS\n");
-    fprintf(stderr, "\t-3\n\t\t24 bit samples are stored in 3 bytes\n");
-    fprintf(stderr, "\t-N\n\t\tdisable pre/post processing\n");
-    fprintf(stderr, "\t-b size\n\t\tinternal buffer size in bytes\n");
-    fprintf(stderr, "\t-d\n\t\tdecode SOURCE. If -d is not used: encode.\n");
-    fprintf(stderr, "\t-j samples\n\t\tblock size in samples\n");
-    fprintf(stderr, "\t-m\n\t\tsamples are MSB first. Default is LSB\n");
-    fprintf(stderr, "\t-n bits\n\t\tbits per sample\n");
-    fprintf(stderr, "\t-p\n\t\tpad RSI to byte boundary\n");
-    fprintf(stderr, "\t-r blocks\n\t\treference sample interval in blocks\n");
-    fprintf(stderr, "\t-s\n\t\tsamples are signed. Default is unsigned\n");
-    fprintf(stderr, "\t-t\n\t\tuse restricted set of code options\n\n");
-    return 1;
+DESTRUCT:
+    if (in)
+        free(in);
+    if (out)
+        free(out);
+    return status;
 }
diff --git a/tests/check_buffer_sizes.c b/tests/check_buffer_sizes.c
index 53996a0ee82a211a67780da02168d38208a5099b..c61f98d6032a65d7baa425db9b81c570129eb0f3 100644
--- a/tests/check_buffer_sizes.c
+++ b/tests/check_buffer_sizes.c
@@ -87,7 +87,8 @@ int main (void)
 
     if (!state.ubuf || !state.cbuf || !state.obuf) {
         printf("Not enough memory.\n");
-        return 99;
+        status = 99;
+        goto DESTRUCT;
     }
 
     strm.flags = AEC_DATA_PREPROCESS;
@@ -100,9 +101,12 @@ int main (void)
         goto DESTRUCT;
 
 DESTRUCT:
-    free(state.ubuf);
-    free(state.cbuf);
-    free(state.obuf);
+    if (state.ubuf)
+        free(state.ubuf);
+    if (state.cbuf)
+        free(state.cbuf);
+    if (state.obuf)
+        free(state.obuf);
 
     return status;
 }
diff --git a/tests/check_code_options.c b/tests/check_code_options.c
index dea916f01b1152e04931071d808b43ef40b603da..232d5dddaef414a22c96faff6c28dda2a0358561 100644
--- a/tests/check_code_options.c
+++ b/tests/check_code_options.c
@@ -303,7 +303,8 @@ int main(int argc, char *argv[])
 
     if (!state.ubuf || !state.cbuf || !state.obuf) {
         printf("Not enough memory.\n");
-        return 99;
+        status = 99;
+        goto DESTRUCT;
     }
 
     strm.flags = 0;
@@ -324,9 +325,12 @@ int main(int argc, char *argv[])
     status = check_byte_orderings(&state);
 
 DESTRUCT:
-    free(state.ubuf);
-    free(state.cbuf);
-    free(state.obuf);
+    if (state.ubuf)
+        free(state.ubuf);
+    if (state.cbuf)
+        free(state.cbuf);
+    if (state.obuf)
+        free(state.obuf);
 
     return status;
 }
diff --git a/tests/check_szcomp.c b/tests/check_szcomp.c
index 813375430b45de0403f223bbdb436f3a5c5265c1..3c10aef75a47d92e0a9674886c192df2d3df0ddf 100644
--- a/tests/check_szcomp.c
+++ b/tests/check_szcomp.c
@@ -11,9 +11,11 @@
 
 int main(int argc, char *argv[])
 {
-    int status;
+    int status = 0;
     SZ_com_t sz_param;
-    unsigned char *source, *dest, *dest1;
+    unsigned char *source = NULL;
+    unsigned char *dest = NULL;
+    unsigned char *dest1 = NULL;
     size_t destLen, dest1Len, sourceLen;
     FILE *fp;
 
@@ -40,27 +42,33 @@ int main(int argc, char *argv[])
     dest = (unsigned char *)malloc(destLen);
     dest1 = (unsigned char *)malloc(destLen);
 
-    if (source == NULL || dest == NULL || dest1 == NULL)
-        return 1;
+    if (source == NULL || dest == NULL || dest1 == NULL) {
+        status = 99;
+        goto DESTRUCT;
+    }
 
     sourceLen = fread(source, 1, sourceLen, fp);
 
     status = SZ_BufftoBuffCompress(dest, &destLen,
                                    source, sourceLen, &sz_param);
     if (status != SZ_OK)
-        return status;
+        goto DESTRUCT;
 
     dest1Len = sourceLen;
     status = SZ_BufftoBuffDecompress(dest1, &dest1Len,
                                      dest, destLen, &sz_param);
     if (status != SZ_OK)
-        return status;
+        goto DESTRUCT;
 
     if (memcmp(source, dest1, sourceLen) != 0)
         fprintf(stderr, "File %s Buffers differ\n", argv[2]);
 
-    free(source);
-    free(dest);
-    free(dest1);
-    return 0;
+DESTRUCT:
+    if (source)
+        free(source);
+    if (dest)
+        free(dest);
+    if (dest1)
+        free(dest1);
+    return status;
 }