summaryrefslogtreecommitdiff
path: root/lib/tdb/common
diff options
context:
space:
mode:
authorMichael Adam <obnox@samba.org>2014-02-10 01:39:15 +0100
committerVolker Lendecke <vl@samba.org>2014-06-26 10:00:11 +0200
commit87ac4ac523b68776c98fb1656d89d1eb28419fd4 (patch)
tree24ed62991c61e971d5c3f3f8166138b614ee7717 /lib/tdb/common
parentf5a777a36ca1767df70087dfd084e86d4e97d242 (diff)
downloadsamba-87ac4ac523b68776c98fb1656d89d1eb28419fd4.tar.gz
tdb: increase readability of read_record_on_left()
by using early returns and better variable names, and reducing indentation. Signed-off-by: Michael Adam <obnox@samba.org> Reviewed-by: Volker Lendecke <vl@samba.org>
Diffstat (limited to 'lib/tdb/common')
-rw-r--r--lib/tdb/common/freelist.c68
1 files changed, 41 insertions, 27 deletions
diff --git a/lib/tdb/common/freelist.c b/lib/tdb/common/freelist.c
index e6f7db511f6..4a48c3d6704 100644
--- a/lib/tdb/common/freelist.c
+++ b/lib/tdb/common/freelist.c
@@ -101,45 +101,59 @@ static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
* Read the record directly on the left.
* Fail if there is no record on the left.
*/
-static int read_record_on_left(struct tdb_context *tdb, tdb_off_t offset,
+static int read_record_on_left(struct tdb_context *tdb, tdb_off_t rec_ptr,
tdb_off_t *left_p,
struct tdb_record *left_r)
{
- tdb_off_t left = offset - sizeof(tdb_off_t);
- struct tdb_record l;
- tdb_off_t leftsize;
+ tdb_off_t left_ptr;
+ tdb_off_t left_size;
+ struct tdb_record left_rec;
+ int ret;
- if (offset - sizeof(tdb_off_t) > TDB_DATA_START(tdb->hash_size)) {
+ left_ptr = rec_ptr - sizeof(tdb_off_t);
- /* Read in tailer and jump back to header */
- if (tdb_ofs_read(tdb, left, &leftsize) == -1) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left offset read failed at %u\n", left));
- return -1;
- }
+ if (left_ptr <= TDB_DATA_START(tdb->hash_size)) {
+ /* no record on the left */
+ return -1;
+ }
- /* it could be uninitialised data */
- if (leftsize == 0 || leftsize == TDB_PAD_U32) {
- return -1;
- }
+ /* Read in tailer and jump back to header */
+ ret = tdb_ofs_read(tdb, left_ptr, &left_size);
+ if (ret == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,
+ "tdb_free: left offset read failed at %u\n", left_ptr));
+ return -1;
+ }
- left = offset - leftsize;
+ /* it could be uninitialised data */
+ if (left_size == 0 || left_size == TDB_PAD_U32) {
+ return -1;
+ }
- if (leftsize > offset ||
- left < TDB_DATA_START(tdb->hash_size)) {
- return -1;
- }
+ if (left_size > rec_ptr) {
+ return -1;
+ }
- /* Now read in the left record */
- if (tdb->methods->tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
- return -1;
- }
+ left_ptr = rec_ptr - left_size;
+
+ if (left_ptr < TDB_DATA_START(tdb->hash_size)) {
+ return -1;
+ }
- *left_p = left;
- *left_r = l;
+ /* Now read in the left record */
+ ret = tdb->methods->tdb_read(tdb, left_ptr, &left_rec,
+ sizeof(left_rec), DOCONV());
+ if (ret == -1) {
+ TDB_LOG((tdb, TDB_DEBUG_FATAL,
+ "tdb_free: left read failed at %u (%u)\n",
+ left_ptr, left_size));
+ return -1;
}
- return -1;
+ *left_p = left_ptr;
+ *left_r = left_rec;
+
+ return 0;
}
/* Add an element into the freelist. Merge adjacent records if