From 6f82542fcb30b83fa3cb39efc941e202b3536209 Mon Sep 17 00:00:00 2001 From: Wei Gao Date: Tue, 5 Mar 2024 09:10:57 -0500 Subject: [PATCH] libswap.c: Improve calculate swap dev number I encounter a dead loop on following code in our test on some non-intel archrs (aarch64, ppc64le, s390x): while ((c = fgetc(fp)) != EOF) Use fgets() instead of fgetc() can avoid the above issue. Link: https://lore.kernel.org/ltp/20240305141057.8754-1-wegao@suse.com/ Reviewed-by: Petr Vorel Signed-off-by: Wei Gao --- 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 c10db91c0e5..a26ea25e4b5 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)