Fix lru/lba compare functions: possible overflow
authorgkostka <kostka.grzegorz@gmail.com>
Mon, 23 Nov 2015 17:12:26 +0000 (18:12 +0100)
committergkostka <kostka.grzegorz@gmail.com>
Mon, 23 Nov 2015 17:35:09 +0000 (18:35 +0100)
Functions will not work for lba values:
 - 0x100000001ULL - 0x000000001UL

It won't work also for 16 int architectures. So it is better to
implement this functions using if/else if statements.

lwext4/ext4_bcache.c

index 51fb2d630ca5101d3a68eec42546652b2c2a3b5a..b3800c890c9880926b13216d32081c051347935a 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 
-static int
-ext4_bcache_lba_compare(struct ext4_buf *a,
-                       struct ext4_buf *b)
+static int ext4_bcache_lba_compare(struct ext4_buf *a, struct ext4_buf *b)
 {
-       return a->lba - b->lba;
+        if (a->lba > b->lba)
+                return 1;
+        else if (a->lba < b->lba)
+                return -1;
+        return 0;
 }
 
-static int
-ext4_bcache_lru_compare(struct ext4_buf *a,
-                       struct ext4_buf *b)
+static int ext4_bcache_lru_compare(struct ext4_buf *a, struct ext4_buf *b)
 {
-       return a->lru_id - b->lru_id;
+       if (a->lru_id > b->lru_id)
+               return 1;
+       else if (a->lru_id < b->lru_id)
+               return -1;
+       return 0;
 }
 
 RB_GENERATE_INTERNAL(ext4_buf_lba, ext4_buf, lba_node,