commit 797face0d2e3bc2b403a05837288702c29be492a
parent c6c38c6642f1ae9b144aa6c19c98f21cbe5141bd
Author: sin <sin@2f30.org>
Date: Thu, 7 Mar 2019 13:24:36 +0000
More overflow checks
The helper doesn't help because we want control over the types
involved.
Diffstat:
3 files changed, 11 insertions(+), 19 deletions(-)
diff --git a/dedup.c b/dedup.c
@@ -143,11 +143,11 @@ grow_snap(struct snapshot *snap, uint64_t nr_blk_descs)
{
size_t size;
- if (mul_overflow(nr_blk_descs, sizeof(snap->blk_desc[0])))
+ if (nr_blk_descs > SIZE_MAX / sizeof(snap->blk_desc[0]))
errx(1, "%s: overflow", __func__);
size = nr_blk_descs * sizeof(snap->blk_desc[0]);
- if (add_overflow(size, sizeof(*snap)))
+ if (size > SIZE_MAX - sizeof(*snap))
errx(1, "%s: overflow", __func__);
size += sizeof(*snap);
@@ -160,11 +160,11 @@ grow_snap(struct snapshot *snap, uint64_t nr_blk_descs)
static void
append_snap(struct snapshot *snap)
{
- if (mul_overflow(snap->nr_blk_descs, BLK_DESC_SIZE))
+ if (snap->nr_blk_descs > UINT64_MAX / BLK_DESC_SIZE)
errx(1, "%s: overflow", __func__);
snap->size = snap->nr_blk_descs * BLK_DESC_SIZE;
- if (add_overflow(SNAPSHOT_SIZE, snap->size))
+ if (snap->size > UINT64_MAX - SNAPSHOT_SIZE)
errx(1, "%s: overflow", __func__);
snap->size += SNAPSHOT_SIZE;
@@ -172,9 +172,12 @@ append_snap(struct snapshot *snap)
write_snapshot(ifd, snap);
write_snapshot_blk_descs(ifd, snap);
- if (add_overflow(snap_hdr.size, snap->size))
+ if (snap_hdr.size > UINT64_MAX - snap->size)
errx(1, "%s: overflow", __func__);
snap_hdr.size += snap->size;
+
+ if (snap_hdr.nr_snapshots > UINT64_MAX - 1)
+ errx(1, "%s: overflow", __func__);
snap_hdr.nr_snapshots++;
}
@@ -218,6 +221,9 @@ append_blk(uint8_t *buf, struct blk_desc *blk_desc)
{
xlseek(sfd, blk_hdr.size, SEEK_SET);
xwrite(sfd, buf, blk_desc->size);
+
+ if (blk_hdr.size > UINT64_MAX - blk_desc->size)
+ errx(1, "%s: overflow", __func__);
blk_hdr.size += blk_desc->size;
}
diff --git a/dedup.h b/dedup.h
@@ -108,5 +108,3 @@ void str2bin(char *s, uint8_t *d);
off_t xlseek(int fd, off_t offset, int whence);
ssize_t xread(int fd, void *buf, size_t nbytes);
ssize_t xwrite(int fd, const void *buf, size_t nbytes);
-int mul_overflow(size_t a, size_t b);
-int add_overflow(size_t a, size_t b);
diff --git a/utils.c b/utils.c
@@ -66,15 +66,3 @@ xwrite(int fd, const void *buf, size_t nbytes)
}
return total;
}
-
-int
-mul_overflow(size_t a, size_t b)
-{
- return a > SIZE_MAX / b;
-}
-
-int
-add_overflow(size_t a, size_t b)
-{
- return a > SIZE_MAX - b;
-}