From 4483bf143ddfee9ec07aed8f124559b00f757d9a Mon Sep 17 00:00:00 2001 From: Volker Lendecke Date: Thu, 30 May 2013 14:52:59 +0200 Subject: [PATCH] tdb: Add overflow-checking tdb_add_off_t Signed-off-by: Volker Lendecke Reviewed-by: Rusty Russell --- lib/tdb/common/tdb.c | 11 +++++++++++ lib/tdb/common/tdb_private.h | 1 + 2 files changed, 12 insertions(+) diff --git a/lib/tdb/common/tdb.c b/lib/tdb/common/tdb.c index a2ae187f577..6256a05d04e 100644 --- a/lib/tdb/common/tdb.c +++ b/lib/tdb/common/tdb.c @@ -1000,6 +1000,17 @@ bool tdb_write_all(int fd, const void *buf, size_t count) return true; } +bool tdb_add_off_t(tdb_off_t a, tdb_off_t b, tdb_off_t *pret) +{ + tdb_off_t ret = a + b; + + if ((ret < a) || (ret < b)) { + return false; + } + *pret = ret; + return true; +} + #ifdef TDB_TRACE static void tdb_trace_write(struct tdb_context *tdb, const char *str) { diff --git a/lib/tdb/common/tdb_private.h b/lib/tdb/common/tdb_private.h index 406fc5f7f20..c37246f1503 100644 --- a/lib/tdb/common/tdb_private.h +++ b/lib/tdb/common/tdb_private.h @@ -282,4 +282,5 @@ void tdb_header_hash(struct tdb_context *tdb, uint32_t *magic1_hash, uint32_t *magic2_hash); unsigned int tdb_old_hash(TDB_DATA *key); size_t tdb_dead_space(struct tdb_context *tdb, tdb_off_t off); +bool tdb_add_off_t(tdb_off_t a, tdb_off_t b, tdb_off_t *pret); #endif /* TDB_PRIVATE_H */ -- 2.34.1