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

Wei Gao wegao@suse.com
Tue Mar 5 15:10:57 CET 2024


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

Use fgets instead of fgetc can avoid above issue.

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)
-- 
2.35.3



More information about the ltp mailing list