From 3513ba0ac450901bdeb852cb81f3df09a8466aec Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Fri, 25 Apr 2014 13:36:53 +0400 Subject: [PATCH] test_librbd_fsx: use posix_memalign() to allocate aligned buffers 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 --- src/test/librbd/fsx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/test/librbd/fsx.c b/src/test/librbd/fsx.c index f28d82023241..f5a76939fca4 100644 --- a/src/test/librbd/fsx.c +++ b/src/test/librbd/fsx.c @@ -34,6 +34,7 @@ #include #include +#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; -- 2.47.3