Skip to content

Commit

Permalink
libswap.c: Check free space with correct mnt path
Browse files Browse the repository at this point in the history
The tst_fs_has_free should check fs size of mnt point.
But current code check ".", that means check /tmp/LTP_xxx
instead of /tmp/LTP_xxx/mntpoint.

Also tst_fs_has_free's "size" parameter's type is unsigned int,
it will overflow if encounter big filesystem block size (such as Btrfs
can use 64k).

Link: https://lore.kernel.org/ltp/20240301015037.22061-1-wegao@suse.com/
Reviewed-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Wei Gao <wegao@suse.com>
[ pvorel: check for sscanf() return value ]
Signed-off-by: Petr Vorel <pvorel@suse.cz>
  • Loading branch information
coolgw authored and pevik committed Mar 3, 2024
1 parent 50626b4 commit 0f5d8c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
6 changes: 3 additions & 3 deletions include/tst_fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ enum {
* @mult: mult should be TST_KB, TST_MB or TST_GB
* the required free space is calculated by @size * @mult
*/
int tst_fs_has_free_(void (*cleanup)(void), const char *path, unsigned int size,
int tst_fs_has_free_(void (*cleanup)(void), const char *path, uint64_t size,
unsigned int mult);

/*
Expand Down Expand Up @@ -225,7 +225,7 @@ static inline long tst_fs_type(const char *path)
return tst_fs_type_(NULL, path);
}

static inline int tst_fs_has_free(const char *path, unsigned int size,
static inline int tst_fs_has_free(const char *path, uint64_t size,
unsigned int mult)
{
return tst_fs_has_free_(NULL, path, size, mult);
Expand All @@ -252,7 +252,7 @@ static inline long tst_fs_type(void (*cleanup)(void), const char *path)
}

static inline int tst_fs_has_free(void (*cleanup)(void), const char *path,
unsigned int size, unsigned int mult)
uint64_t size, unsigned int mult)
{
return tst_fs_has_free_(cleanup, path, size, mult);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tst_fs_has_free.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "tst_fs.h"

int tst_fs_has_free_(void (*cleanup)(void), const char *path,
unsigned int size, unsigned int mult)
uint64_t size, unsigned int mult)
{
struct statfs sf;

Expand Down
6 changes: 5 additions & 1 deletion libs/libltpswap/libswap.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ int make_swapfile(const char *swapfile, int blocks, int safe)
struct statvfs fs_info;
unsigned long blk_size, bs;
size_t pg_size = sysconf(_SC_PAGESIZE);
char mnt_path[100];

if (statvfs(".", &fs_info) == -1)
return -1;
Expand All @@ -149,7 +150,10 @@ int make_swapfile(const char *swapfile, int blocks, int safe)
else
bs = blk_size;

if (!tst_fs_has_free(".", bs * blocks, TST_BYTES))
if (sscanf(swapfile, "%[^/]", mnt_path) != 1)
tst_brk(TBROK, "sscanf failed");

if (!tst_fs_has_free(mnt_path, bs * blocks, TST_BYTES))
tst_brk(TCONF, "Insufficient disk space to create swap file");

/* create file */
Expand Down

0 comments on commit 0f5d8c5

Please sign in to comment.