]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb1_transaction.c
tdb2: Avoid a malloc/memcpy in _tdb1_store
[ccan] / ccan / tdb2 / tdb1_transaction.c
index 6e39de33f4c95269d818b0d8150f68b2735b6c2b..c4a2b10b20759685112524f6a101e69964d3ec9f 100644 (file)
@@ -376,9 +376,9 @@ static void transaction1_next_hash_chain(struct tdb_context *tdb, uint32_t *chai
 /*
   out of bounds check during a transaction
 */
-static int transaction1_oob(struct tdb_context *tdb, tdb1_off_t len, int probe)
+static int transaction1_oob(struct tdb_context *tdb, tdb1_off_t off, tdb1_off_t len, int probe)
 {
-       if (len <= tdb->file->map_size) {
+       if (off + len >= off && off + len <= tdb->file->map_size) {
                return 0;
        }
        tdb->last_error = TDB_ERR_IO;
@@ -520,7 +520,7 @@ static int _tdb1_transaction_start(struct tdb_context *tdb)
 
        /* make sure we know about any file expansions already done by
           anyone else */
-       tdb->tdb1.io->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
+       tdb->tdb1.io->tdb1_oob(tdb, tdb->file->map_size, 1, 1);
        tdb->tdb1.transaction->old_map_size = tdb->file->map_size;
 
        /* finally hook the io methods, replacing them with
@@ -761,7 +761,7 @@ static int tdb1_recovery_allocate(struct tdb_context *tdb,
        tdb->stats.transaction_expand_file++;
 
        /* remap the file (if using mmap) */
-       methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
+       methods->tdb1_oob(tdb, tdb->file->map_size, 1, 1);
 
        /* we have to reset the old map size so that we don't try to expand the file
           again in the transaction commit, which would destroy the recovery area */
@@ -1022,7 +1022,7 @@ static int _tdb1_transaction_prepare_commit(struct tdb_context *tdb)
                }
                tdb->stats.transaction_expand_file++;
                tdb->file->map_size = tdb->tdb1.transaction->old_map_size;
-               methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
+               methods->tdb1_oob(tdb, tdb->file->map_size, 1, 1);
        }
 
        /* Keep the open lock until the actual commit */
@@ -1174,7 +1174,8 @@ int tdb1_transaction_commit(struct tdb_context *tdb)
        _tdb1_transaction_cancel(tdb);
 
        if (need_repack) {
-               return tdb_repack(tdb);
+               if (tdb_repack(tdb) != 0)
+                       return -1;
        }
 
        return 0;
@@ -1317,7 +1318,7 @@ tdb_bool_err tdb1_needs_recovery(struct tdb_context *tdb)
 
        /* find the recovery area */
        if (tdb1_ofs_read(tdb, TDB1_RECOVERY_HEAD, &recovery_head) == -1) {
-               return tdb->last_error;
+               return TDB_ERR_TO_OFF(tdb->last_error);
        }
 
        if (recovery_head == 0) {
@@ -1328,7 +1329,7 @@ tdb_bool_err tdb1_needs_recovery(struct tdb_context *tdb)
        /* read the recovery record */
        if (tdb->tdb1.io->tdb1_read(tdb, recovery_head, &rec,
                                   sizeof(rec), TDB1_DOCONV()) == -1) {
-               return tdb->last_error;
+               return TDB_ERR_TO_OFF(tdb->last_error);
        }
 
        return (rec.magic == TDB1_RECOVERY_MAGIC);