Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/vndcompress Justify the last unjustified assertion h...



details:   https://anonhg.NetBSD.org/src/rev/d8db36f20bca
branches:  trunk
changeset: 352995:d8db36f20bca
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Sun Apr 16 23:50:40 2017 +0000

description:
Justify the last unjustified assertion here.

Sprinkle a few more assertions to help along the way.

(Actually, it was justified; I just hadn't made explicit the relation
to the value of fdpos that all two callers specify.)

diffstat:

 usr.bin/vndcompress/offtab.c        |  36 +++++++++++++++++++++++++++---------
 usr.bin/vndcompress/offtab.h        |   8 +++++++-
 usr.bin/vndcompress/vndcompress.c   |   8 ++++++--
 usr.bin/vndcompress/vnduncompress.c |   5 +++--
 4 files changed, 43 insertions(+), 14 deletions(-)

diffs (182 lines):

diff -r d1509a5a608f -r d8db36f20bca usr.bin/vndcompress/offtab.c
--- a/usr.bin/vndcompress/offtab.c      Sun Apr 16 23:43:57 2017 +0000
+++ b/usr.bin/vndcompress/offtab.c      Sun Apr 16 23:50:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: offtab.c,v 1.13 2014/01/25 16:38:15 riastradh Exp $    */
+/*     $NetBSD: offtab.c,v 1.14 2017/04/16 23:50:40 riastradh Exp $    */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: offtab.c,v 1.13 2014/01/25 16:38:15 riastradh Exp $");
+__RCSID("$NetBSD: offtab.c,v 1.14 2017/04/16 23:50:40 riastradh Exp $");
 
 #include <sys/types.h>
 #include <sys/endian.h>
@@ -103,7 +103,9 @@
        const off_t window_offset = ((off_t)window_start *
            (off_t)sizeof(uint64_t));
 
-       /* XXX This assertion is not justified.  */
+       assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
+       __CTASSERT(OFFTAB_MAX_FDPOS <=
+           (OFF_MAX - (off_t)MAX_N_OFFSETS*sizeof(uint64_t)));
        assert(offtab->ot_fdpos <= (OFF_MAX - window_offset));
        *pos = (offtab->ot_fdpos + window_offset);
 }
@@ -209,6 +211,7 @@
        assert(0 < n_offsets);
        assert(0 <= fd);
        assert(0 <= fdpos);
+       assert(fdpos <= OFFTAB_MAX_FDPOS);
 
        offtab->ot_n_offsets = n_offsets;
        if ((window_size == 0) || (n_offsets < window_size))
@@ -293,6 +296,9 @@
                __CTASSERT(MAX_N_OFFSETS <= (OFF_MAX / sizeof(uint64_t)));
                const off_t offtab_bytes = ((off_t)offtab->ot_n_offsets *
                    (off_t)sizeof(uint64_t));
+               assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
+               __CTASSERT(OFFTAB_MAX_FDPOS <=
+                   (OFF_MAX - (off_t)MAX_N_OFFSETS*sizeof(uint64_t)));
                assert(offtab->ot_fdpos <= (OFF_MAX - offtab_bytes));
                const off_t first_offset = (offtab->ot_fdpos + offtab_bytes);
                if (lseek(offtab->ot_fd, first_offset, SEEK_SET) == -1) {
@@ -367,9 +373,11 @@
        __CTASSERT(MAX_N_OFFSETS <= UINT32_MAX);
        assert(offtab->ot_n_offsets > 0);
 
+       /* Initialize window of all ones.  */
        for (i = 0; i < offtab->ot_window_size; i++)
                offtab->ot_window[i] = ~(uint64_t)0;
 
+       /* Write the window to every position in the table.  */
        const uint32_t n_windows =
            howmany(offtab->ot_n_offsets, offtab->ot_window_size);
        for (i = 1; i < n_windows; i++) {
@@ -378,15 +386,25 @@
                offtab_write_window(offtab);
        }
 
-       offtab->ot_window_start = 0;
-       __CTASSERT(MAX_N_OFFSETS <=
-           (MIN(OFF_MAX, UINT64_MAX) / sizeof(uint64_t)));
+       /* Compute the number of bytes in the offset table.  */
+       __CTASSERT(MAX_N_OFFSETS <= OFF_MAX/sizeof(uint64_t));
        const off_t offtab_bytes = ((off_t)offtab->ot_n_offsets *
            sizeof(uint64_t));
-       assert(offtab->ot_fdpos <=
-           ((off_t)MIN(OFF_MAX, UINT64_MAX) - offtab_bytes));
+
+       /* Compute the offset of the first block.  */
+       assert(offtab->ot_fdpos <= OFFTAB_MAX_FDPOS);
+       __CTASSERT(OFFTAB_MAX_FDPOS <=
+           (OFF_MAX - (off_t)MAX_N_OFFSETS*sizeof(uint64_t)));
+       assert(offtab->ot_fdpos <= (OFF_MAX - offtab_bytes));
        const off_t first_offset = (offtab->ot_fdpos + offtab_bytes);
-       assert(first_offset <= (off_t)MIN(OFF_MAX, UINT64_MAX));
+
+       /* Assert that it fits in 64 bits.  */
+       __CTASSERT(MAX_N_OFFSETS <= UINT64_MAX/sizeof(uint64_t));
+       __CTASSERT(OFFTAB_MAX_FDPOS <=
+           (UINT64_MAX - (uint64_t)MAX_N_OFFSETS*sizeof(uint64_t)));
+
+       /* Write out the first window with the first offset.  */
+       offtab->ot_window_start = 0;
        offtab->ot_window[0] = htobe64((uint64_t)first_offset);
        offtab_write_window(offtab);
 
diff -r d1509a5a608f -r d8db36f20bca usr.bin/vndcompress/offtab.h
--- a/usr.bin/vndcompress/offtab.h      Sun Apr 16 23:43:57 2017 +0000
+++ b/usr.bin/vndcompress/offtab.h      Sun Apr 16 23:50:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: offtab.h,v 1.2 2014/01/22 06:15:22 riastradh Exp $     */
+/*     $NetBSD: offtab.h,v 1.3 2017/04/16 23:50:40 riastradh Exp $     */
 
 /*-
  * Copyright (c) 2014 The NetBSD Foundation, Inc.
@@ -37,6 +37,8 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+#include "common.h"
+
 struct offtab {
        uint32_t        ot_n_offsets;
        uint32_t        ot_window_size;
@@ -54,6 +56,10 @@
        }               ot_mode;
 };
 
+#define        OFFTAB_MAX_FDPOS                                                      \
+       ((off_t)(MIN(OFF_MAX, UINT64_MAX) -                                   \
+           (off_t)MAX_N_OFFSETS*sizeof(uint64_t)))
+
 void           offtab_init(struct offtab *, uint32_t, uint32_t, int, off_t);
 void           offtab_destroy(struct offtab *);
 
diff -r d1509a5a608f -r d8db36f20bca usr.bin/vndcompress/vndcompress.c
--- a/usr.bin/vndcompress/vndcompress.c Sun Apr 16 23:43:57 2017 +0000
+++ b/usr.bin/vndcompress/vndcompress.c Sun Apr 16 23:50:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vndcompress.c,v 1.26 2017/01/10 21:15:54 christos Exp $        */
+/*     $NetBSD: vndcompress.c,v 1.27 2017/04/16 23:50:40 riastradh Exp $       */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: vndcompress.c,v 1.26 2017/01/10 21:15:54 christos Exp $");
+__RCSID("$NetBSD: vndcompress.c,v 1.27 2017/04/16 23:50:40 riastradh Exp $");
 
 #include <sys/endian.h>
 #include <sys/stat.h>
@@ -485,6 +485,7 @@
        S->n_offsets = (S->n_blocks + 1);
        __CTASSERT(MAX_N_OFFSETS == (MAX_N_BLOCKS + 1));
        __CTASSERT(MAX_N_OFFSETS <= (SIZE_MAX / sizeof(uint64_t)));
+       __CTASSERT(CLOOP2_OFFSET_TABLE_OFFSET <= OFFTAB_MAX_FDPOS);
        offtab_init(&S->offtab, S->n_offsets, window_size, S->cloop2_fd,
            CLOOP2_OFFSET_TABLE_OFFSET);
 
@@ -606,6 +607,9 @@
        if (!offtab_prepare_get(&S->offtab, 0))
                return false;
        const uint64_t first_offset = offtab_get(&S->offtab, 0);
+       __CTASSERT(MAX_N_OFFSETS <= UINT64_MAX/sizeof(uint64_t));
+       __CTASSERT(sizeof(struct cloop2_header) <=
+           (UINT64_MAX - MAX_N_OFFSETS*sizeof(uint64_t)));
        const uint64_t expected = sizeof(struct cloop2_header) + 
            ((uint64_t)S->n_offsets * sizeof(uint64_t));
        if (first_offset != expected) {
diff -r d1509a5a608f -r d8db36f20bca usr.bin/vndcompress/vnduncompress.c
--- a/usr.bin/vndcompress/vnduncompress.c       Sun Apr 16 23:43:57 2017 +0000
+++ b/usr.bin/vndcompress/vnduncompress.c       Sun Apr 16 23:50:40 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: vnduncompress.c,v 1.11 2014/01/25 15:31:06 riastradh Exp $     */
+/*     $NetBSD: vnduncompress.c,v 1.12 2017/04/16 23:50:40 riastradh Exp $     */
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: vnduncompress.c,v 1.11 2014/01/25 15:31:06 riastradh Exp $");
+__RCSID("$NetBSD: vnduncompress.c,v 1.12 2017/04/16 23:50:40 riastradh Exp $");
 
 #include <sys/endian.h>
 
@@ -135,6 +135,7 @@
        }
 
        /* Initialize the offset table and start reading it in.  */
+       __CTASSERT(CLOOP2_OFFSET_TABLE_OFFSET <= OFFTAB_MAX_FDPOS);
        offtab_init(&offtab, n_offsets, window_size, cloop2_fd,
            CLOOP2_OFFSET_TABLE_OFFSET);
        offtab_reset_read(&offtab, &err1, &errx1);



Home | Main Index | Thread Index | Old Index