[LTP] [PATCH v2] libswap.c: Improve calculate swap dev number

Petr Vorel pvorel@suse.cz
Tue Mar 5 15:45:20 CET 2024


> I encounter a dead loop on following code in our test on ppc64 machine:
> while ((c = fgetc(fp)) != EOF)

FYI it fails also on aarch64 and s390x, thus I'll add it to the commit message
before merge. I also try to have look why it's broken.

> Use fgets instead of fgetc can avoid above issue.

LGTM. I probably merge it tonight.

Reviewed-by: Petr Vorel <pvorel@suse.cz>

Kind regards,
Petr

> Signed-off-by: Wei Gao <wegao@suse.com>
> ---
>  libs/libltpswap/libswap.c | 9 ++++-----
>  1 file changed, 4 insertions(+), 5 deletions(-)

> diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
> index c10db91c0..a26ea25e4 100644
> --- a/libs/libltpswap/libswap.c
> +++ b/libs/libltpswap/libswap.c
> @@ -13,6 +13,7 @@

>  #define TST_NO_DEFAULT_MAIN
>  #define DEFAULT_MAX_SWAPFILE 32
> +#define BUFSIZE 200

>  #include "tst_test.h"
>  #include "libswap.h"
> @@ -279,16 +280,14 @@ int tst_count_swaps(void)
>  {
>  	FILE *fp;
>  	int used = -1;
> -	char c;
> +	char buf[BUFSIZE];

>  	fp = SAFE_FOPEN("/proc/swaps", "r");
>  	if (fp == NULL)
>  		return -1;

> -	while ((c = fgetc(fp)) != EOF) {
> -		if (c == '\n')
> -			used++;
> -	}
> +	while (fgets(buf, BUFSIZE, fp) != NULL)
> +		used++;

>  	SAFE_FCLOSE(fp);
>  	if (used < 0)


More information about the ltp mailing list