Commit Graph

29 Commits

Author SHA1 Message Date
riastradh
76d4b81251 Clarify compile-time and run-time arithmetic safety assertions.
This is an experiment with a handful of macros for writing the
checks, most of which are compile-time:

MUL_OK(t, a, b)         Does a*b avoid overflow in type t?
ADD_OK(t, a, b)         Does a + b avoid overflow in type t?
TOOMANY(t, x, b, m)     Are there more than m b-element blocks in x in type t?
                        (I.e., does ceiling(x/b) > m?)

Addenda that might make sense but are not needed here:

MUL(t, a, b, &p)        Set p = a*b and return 0, or return ERANGE if overflow.
ADD(t, a, b, &s)        Set s = a+b and return 0, or return ERANGE if overflow.

Example:

	uint32_t a = ..., b = ..., y = ..., z = ..., x, w;

        /* input validation */
        error = MUL(size_t, a, b, &x);
        if (error)
                fail;
        if (TOOMANY(uint32_t, x, BLKSIZ, MAX_NBLK))
                fail;
        y = HOWMANY(x, BLKSIZ);
        if (z > Z_MAX)
                fail;
        ...
        /* internal computation */
        __CTASSERT(MUL_OK(uint32_t, Z_MAX, MAX_NBLK));
        w = z*y;

Obvious shortcomings:

1. Nothing checks your ctassert matches your subsequent arithmetic.
   (Maybe we could have BOUNDED_MUL(t, x, xmax, y, ymax) with a
   ctassert inside.)

2. Nothing flows the bounds needed by the arithmetic you use back
   into candidate definitions of X_MAX/Y_MAX.

But at least the reviewer's job is only to make sure that (a) the
MUL_OK matches the *, and (b) the bounds in the assertion match the
bounds on the inputs -- in particular, the reviewer need not derive
the bounds from the context, only confirm they are supported by the
paths to it.

This is not meant to be a general-purpose proof assistant, or even a
special-purpose one like gfverif <http://gfverif.cryptojedi.org/>.
Rather, it is an experiment in adding a modicum of compile-time
verification with a simple C API change.

This also is not intended to serve as trapping arithmetic on
overflow.  The goal here is to enable writing the program with
explicit checks on input and compile-time annotations on computation
to gain confident that overflow won't happen in the computation.
2017-07-29 21:04:07 +00:00
riastradh
eeefcaaf18 Omit needless XXX comment. 2017-04-17 00:02:45 +00:00
riastradh
9719b6dc16 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.)
2017-04-16 23:50:40 +00:00
christos
f4547403ad need <sys/stat.h> 2017-01-10 21:15:54 +00:00
riastradh
9783666546 Fix vndcompress restart failure fallback when input is a pipe.
Defer seeking the *input* image, or winding it forward, until we are
certain we all ready in the cloop2 output, because when the input
image is a pipe, we don't get a chance to seek back to the beginning
and start from the top instead of restarting.

If restart does fail, don't try to seek the input image back to the
beginning unless we had already tried to seek or wind it forward.

Add some automatic tests for this and related cases.

XXX pullup to netbsd-7, netbsd-6
2014-11-18 03:48:17 +00:00
riastradh
0853010545 Fix some more integer overflow/truncation issues.
Arithmetic in C is hard.  Let's go shopping!
2014-01-25 15:31:06 +00:00
christos
115b322731 CID 1164169: integer overflow 2014-01-24 17:30:18 +00:00
riastradh
f13ecd80a5 Change vndcompress to use a default window size of 512.
For vnduncompress on nonseekable input, the window size is as large
as it needs to be by default, as before.  Not clear that this is the
right choice -- by default vnduncompress on nonseekable input will
just use unbounded memory unsolicited.
2014-01-22 06:18:00 +00:00
riastradh
9aa3cfafcd Rename block size option from -s' to -b'.
Makes more sense and makes it consistent with other utilities such as
pax and pigz.  This vndcompress has never gone out in a release, so
changing the name of the option shouldn't cause too many problems...
2014-01-22 06:17:25 +00:00
riastradh
7dc3ecc300 Window size is now an option; remove XXX comment to the contrary. 2014-01-22 06:16:32 +00:00
riastradh
7c5bfcbe34 Add option -w to vnd(un)compress to specify the window size. 2014-01-22 06:15:57 +00:00
riastradh
3e40e9d7d6 Implement machinery for fixed-size windows into the offset table. 2014-01-22 06:15:22 +00:00
riastradh
519e5b52f0 Write offsets in hexadecimal, not decimal. 2014-01-22 06:15:12 +00:00
riastradh
6e96c4ea68 Move block_signals/restore_sigmask to utils.c 2014-01-22 06:15:04 +00:00
riastradh
735c239796 Abstract handling of the cloop2 offset table.
Preparation for converting it to use a fixed-size window.
2014-01-22 06:14:46 +00:00
riastradh
d99bda47ef Move vndcompress utilities to utils.c. 2014-01-22 06:14:20 +00:00
riastradh
31eb87b9b9 Make partial read/write error messages more consistent in vndcompress. 2013-05-06 22:53:24 +00:00
riastradh
4b4929c1fa Add __printflike to vsnprintf_ss. 2013-05-04 15:37:39 +00:00
riz
e3748ff50d 'unsigned long' prints with %lu, not %zu. 2013-05-04 15:27:39 +00:00
riastradh
2f16fbbee5 Fix sign-compare in compress_blocks.
Not sure why my builds didn't reveal this one -- they revealed
several others during development.
2013-05-04 14:29:48 +00:00
joerg
72bd42b73b __printflike for vwarnx_ss, __dead for err_ss and errx_ss. 2013-05-04 10:21:27 +00:00
riastradh
85bbc49a09 Rewrite vndcompress to support SIGINFO and restart after interrupt.
Make it generally more robust in the process.

No objection (or comment) on tech-userlevel.

ok christos
2013-05-03 23:28:15 +00:00
joerg
505c5e5515 Use static and __dead 2011-09-06 18:45:04 +00:00
lukem
19ffb16ab2 Fix another sign-compare issue 2009-04-14 07:36:16 +00:00
lukem
49d68c2eee fix sign-compare issues 2009-04-14 07:28:23 +00:00
dyoung
4c39af2f02 Take a small step toward making vndcompress into a host tool:
remove #include <arpa/inet.h> and use bswap32() instead of ntohl().
2008-02-18 03:34:04 +00:00
he
759fbc6504 The return type from getopt(3) is int, not char. Besides, char is
not guaranteed to be signed, so comparison with -1 will cause a
warning (turned error) for some of our ports (e.g. our arm ports).
Fix this by making the 'ch' variable an int instead of a char.
2005-07-27 09:29:02 +00:00
wiz
a654b897c8 Sync usage with man page. 2005-07-25 12:25:54 +00:00
hubertf
e98029593c Add vndcompress(1) and vnduncompress(1) to transform filesystem/disk
images from "normal" into cloop2-format compressed images and back.
Written by Florian Stoehr (netbsd@wolfnode.de) with some polishing
by me.

Compressed disk images can be used with the vnd(4) driver when compiled with
VND_COMPRESSION and "vnconfig -z".  Useful for creation of Live CDs/DVDs.
2005-07-25 12:17:59 +00:00