Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sbin/restore Add wrapper functions around hash algorithm ope...



details:   https://anonhg.NetBSD.org/src/rev/e83996ecbb82
branches:  trunk
changeset: 768038:e83996ecbb82
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sat Aug 06 20:46:42 2011 +0000

description:
Add wrapper functions around hash algorithm operations to avoid
undefined behavior arising from illegal function casts. As a side
effect, no longer need -Wno-pointer-sign either.

diffstat:

 sbin/restore/Makefile  |   6 +---
 sbin/restore/restore.h |   6 ++-
 sbin/restore/tape.c    |  70 +++++++++++++++++++++++++++++++++++--------------
 3 files changed, 55 insertions(+), 27 deletions(-)

diffs (141 lines):

diff -r e7d7807c7bec -r e83996ecbb82 sbin/restore/Makefile
--- a/sbin/restore/Makefile     Sat Aug 06 20:42:43 2011 +0000
+++ b/sbin/restore/Makefile     Sat Aug 06 20:46:42 2011 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile,v 1.25 2011/06/20 07:44:00 mrg Exp $
+#      $NetBSD: Makefile,v 1.26 2011/08/06 20:46:42 dholland Exp $
 #      @(#)Makefile    8.1 (Berkeley) 6/5/93
 
 .include <bsd.own.mk>
@@ -12,8 +12,4 @@
 MLINKS+=restore.8 rrestore.8
 .PATH: ${NETBSDSRCDIR}/sbin/dump
 
-.if defined(HAVE_GCC) || defined(HAVE_PCC)
-COPTS.tape.c+= -Wno-pointer-sign
-.endif
-
 .include <bsd.prog.mk>
diff -r e7d7807c7bec -r e83996ecbb82 sbin/restore/restore.h
--- a/sbin/restore/restore.h    Sat Aug 06 20:42:43 2011 +0000
+++ b/sbin/restore/restore.h    Sat Aug 06 20:46:42 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: restore.h,v 1.19 2011/08/06 17:01:06 dholland Exp $    */
+/*     $NetBSD: restore.h,v 1.20 2011/08/06 20:46:42 dholland Exp $    */
 
 /*
  * Copyright (c) 1983, 1993
@@ -69,10 +69,12 @@
 extern int     Bcvt;           /* need byte swapping on inodes and dirs */
 extern FILE    *Mtreefile;     /* file descriptor for the mtree file */
 
+union digest_context;
+
 struct digest_desc {
        const char *dd_name;
        void (*dd_init)(void *);
-       void (*dd_update)(void *, const u_char *, u_int);
+       void (*dd_update)(union digest_context *, const void *, u_int);
        char *(*dd_end)(void *, char *);
 };
 extern const struct digest_desc *ddesc;
diff -r e7d7807c7bec -r e83996ecbb82 sbin/restore/tape.c
--- a/sbin/restore/tape.c       Sat Aug 06 20:42:43 2011 +0000
+++ b/sbin/restore/tape.c       Sat Aug 06 20:46:42 2011 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: tape.c,v 1.64 2011/08/06 17:01:06 dholland Exp $       */
+/*     $NetBSD: tape.c,v 1.65 2011/08/06 20:46:42 dholland Exp $       */
 
 /*
  * Copyright (c) 1983, 1993
@@ -39,7 +39,7 @@
 #if 0
 static char sccsid[] = "@(#)tape.c     8.9 (Berkeley) 5/1/95";
 #else
-__RCSID("$NetBSD: tape.c,v 1.64 2011/08/06 17:01:06 dholland Exp $");
+__RCSID("$NetBSD: tape.c,v 1.65 2011/08/06 20:46:42 dholland Exp $");
 #endif
 #endif /* not lint */
 
@@ -94,26 +94,11 @@
 int            Bcvt;           /* Swap Bytes (for CCI or sun) */
 
 const struct digest_desc *ddesc;
-const struct digest_desc digest_descs[] = {
-       { "MD5",
-         (void (*)(void *))MD5Init,
-         (void (*)(void *, const u_char *, u_int))MD5Update,
-         (char *(*)(void *, char *))MD5End, },
-       { "SHA1",
-         (void (*)(void *))SHA1Init,
-         (void (*)(void *, const u_char *, u_int))SHA1Update,
-         (char *(*)(void *, char *))SHA1End, },
-       { "RMD160",
-         (void (*)(void *))RMD160Init,
-         (void (*)(void *, const u_char *, u_int))RMD160Update,
-         (char *(*)(void *, char *))RMD160End, },
-       { .dd_name = NULL },
-};
 
 static union digest_context {
-       MD5_CTX dc_md5;
-       SHA1_CTX dc_sha1;
-       RMD160_CTX dc_rmd160;
+       MD5_CTX dc_MD5;
+       SHA1_CTX dc_SHA1;
+       RMD160_CTX dc_RMD160;
 } dcontext;
 
 /*
@@ -168,6 +153,51 @@
 static void     swap_header(struct s_spcl *);
 static void     swap_old_header(struct s_ospcl *);
 
+////////////////////////////////////////////////////////////
+// thunks for type correctness
+
+#define WRAP(alg) \
+       static void                                                     \
+       do_##alg##Init(void *ctx)                                       \
+       {                                                               \
+               alg##Init(ctx);                                         \
+       }                                                               \
+                                                                       \
+       static void                                                     \
+       do_##alg##Update(union digest_context *ctx,                     \
+               const void *buf, unsigned len)                          \
+       {                                                               \
+               alg##Update(&ctx->dc_##alg, buf, len);                  \
+       }                                                               \
+                                                                       \
+       static char *                                                   \
+       do_##alg##End(void *ctx, char *str)                             \
+       {                                                               \
+               return alg##End(ctx, str);                              \
+       }
+
+WRAP(MD5);
+WRAP(SHA1);
+WRAP(RMD160);
+
+static const struct digest_desc digest_descs[] = {
+       { "MD5",
+         do_MD5Init,
+         do_MD5Update,
+         do_MD5End, },
+       { "SHA1",
+         do_SHA1Init,
+         do_SHA1Update,
+         do_SHA1End, },
+       { "RMD160",
+         do_RMD160Init,
+         do_RMD160Update,
+         do_RMD160End, },
+       { .dd_name = NULL },
+};
+
+////////////////////////////////////////////////////////////
+
 const struct digest_desc *
 digest_lookup(const char *name)
 {



Home | Main Index | Thread Index | Old Index