Skip to content

Commit

Permalink
libswap.c: Improve calculate swap dev number
Browse files Browse the repository at this point in the history
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/[email protected]/
Reviewed-by: Petr Vorel <[email protected]>
Signed-off-by: Wei Gao <[email protected]>
  • Loading branch information
coolgw authored and pevik committed Mar 5, 2024
1 parent ee628ef commit 6f82542
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions libs/libltpswap/libswap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#define TST_NO_DEFAULT_MAIN
#define DEFAULT_MAX_SWAPFILE 32
#define BUFSIZE 200

#include "tst_test.h"
#include "libswap.h"
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6f82542

Please sign in to comment.