]> mj.ucw.cz Git - libucw.git/commitdiff
Fixed a nasty bug in partmap_map(). When called for an item spanning window
authorMartin Mares <mj@ucw.cz>
Sat, 8 Jan 2005 21:34:22 +0000 (21:34 +0000)
committerMartin Mares <mj@ucw.cz>
Sat, 8 Jan 2005 21:34:22 +0000 (21:34 +0000)
boundary, it miscalculated the memory address and returned the start of the
new window instead.

lib/partmap.c

index c0c2a960174b93c079e6f6b9b900f424925f9f87..04e0891243aeaa79882736d5695409abedce73cf 100644 (file)
@@ -69,19 +69,18 @@ partmap_map(struct partmap *p, sh_off_t start, uns size)
     {
       if (p->start_map)
        munmap(p->start_map, p->end_off - p->start_off);
-      uns win = PARTMAP_WINDOW;
-      ASSERT(win >= size);
       sh_off_t end = start + size;
-      start = start/PAGE_SIZE * PAGE_SIZE;
-      if (start+win > p->file_size)
-       win = p->file_size - start;
-      if (start+win < end)
-       die("cannot mmap, the window is too small");
-      p->start_map = sh_mmap(NULL, win, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, start);
+      sh_off_t win_start = start/PAGE_SIZE * PAGE_SIZE;
+      uns win_len = PARTMAP_WINDOW;
+      if (win_start+win_len > p->file_size)
+       win_len = ALIGN(p->file_size - win_start, PAGE_SIZE);
+      if (win_start+win_len < end)
+       die("partmap_map: Window is too small for mapping %d bytes", size);
+      p->start_map = sh_mmap(NULL, win_len, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, win_start);
       if (p->start_map == MAP_FAILED)
-       die("mmap failed at position %Ld: %m", (long long)start);
-      p->start_off = start;
-      p->end_off = start+win;
+       die("mmap failed at position %Ld: %m", (long long)win_start);
+      p->start_off = win_start;
+      p->end_off = win_start+win_len;
     }
   return p->start_map + (start - p->start_off);
 }