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.)
This commit is contained in:
riastradh 2017-04-16 23:50:40 +00:00
parent 9b8e93bfa9
commit 9719b6dc16
4 changed files with 43 additions and 14 deletions

View File

@ -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 @@ offtab_compute_window_position(struct offtab *offtab, uint32_t window_start,
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 @@ offtab_init(struct offtab *offtab, uint32_t n_offsets, uint32_t window_size,
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 @@ offtab_reset_read(struct offtab *offtab,
__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 @@ offtab_reset_write(struct offtab *offtab)
__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_reset_write(struct offtab *offtab)
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);

View File

@ -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 @@ struct offtab {
} 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 *);

View File

@ -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 @@ compress_init(int argc, char **argv, const struct options *O,
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 @@ compress_restart(struct compress_state *S)
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) {

View File

@ -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 @@ vnduncompress(int argc, char **argv, const struct options *O __unused)
}
/* 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);