]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
test_librbd_fsx: use posix_memalign() to allocate aligned buffers
authorIlya Dryomov <ilya.dryomov@inktank.com>
Fri, 25 Apr 2014 09:36:53 +0000 (13:36 +0400)
committerIlya Dryomov <ilya.dryomov@inktank.com>
Wed, 7 May 2014 13:30:05 +0000 (17:30 +0400)
Use posix_memalign() to allocate good_buf and temp_buf, which must be
writebdy and readbdy aligned respectively.  Using round_ptr_up() the
way it is used makes fsx crash on free()s at the end of main(), because
the pointer returned by malloc() is overwritten by the aligned pointer.

Drop round_ptr_up() as it is no longer used.

Signed-off-by: Ilya Dryomov <ilya.dryomov@inktank.com>
src/test/librbd/fsx.c

index f28d82023241f67da59b97ccfc542cdad7f7188e..f5a76939fca48790bc0d1b60ccb1a0f84fbfac8f 100644 (file)
@@ -34,6 +34,7 @@
 #include <errno.h>
 #include <math.h>
 
+#include "include/intarith.h"
 #include "include/rados/librados.h"
 #include "include/rbd/librbd.h"
 
@@ -173,15 +174,6 @@ FILE *     fsxlogf = NULL;
 int badoff = -1;
 int closeopen = 0;
 
-static void *round_ptr_up(void *ptr, unsigned long align, unsigned long offset)
-{
-       unsigned long ret = (unsigned long)ptr;
-
-       ret = ((ret + align - 1) & ~(align - 1));
-       ret += offset;
-       return (void *)ret;
-}
-
 void
 vwarnc(int code, const char *fmt, va_list ap) {
   fprintf(stderr, "fsx: ");
@@ -1530,12 +1522,29 @@ main(int argc, char **argv)
        original_buf = (char *) malloc(maxfilelen);
        for (i = 0; i < (int)maxfilelen; i++)
                original_buf[i] = random() % 256;
-       good_buf = (char *) malloc(maxfilelen + writebdy);
-       good_buf = round_ptr_up(good_buf, writebdy, 0);
+
+       ret = posix_memalign((void **)&good_buf, MAX(writebdy, sizeof(void *)),
+                            maxfilelen);
+       if (ret > 0) {
+               if (ret == EINVAL)
+                       prt("writebdy is not a suitable power of two\n");
+               else
+                       prterrcode("main: posix_memalign(good_buf)", -ret);
+               exit(94);
+       }
        memset(good_buf, '\0', maxfilelen);
-       temp_buf = (char *) malloc(maxfilelen + readbdy);
-       temp_buf = round_ptr_up(temp_buf, readbdy, 0);
+
+       ret = posix_memalign((void **)&temp_buf, MAX(readbdy, sizeof(void *)),
+                            maxfilelen);
+       if (ret > 0) {
+               if (ret == EINVAL)
+                       prt("readbdy is not a suitable power of two\n");
+               else
+                       prterrcode("main: posix_memalign(temp_buf)", -ret);
+               exit(95);
+       }
        memset(temp_buf, '\0', maxfilelen);
+
        if (lite) {     /* zero entire existing file */
                ssize_t written;