diff --git a/src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c b/src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c index e74bc777f5e..aaeae635b4a 100644 --- a/src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c +++ b/src/jdk.management/linux/native/libmanagement_ext/UnixOperatingSystem.c @@ -62,8 +62,13 @@ static struct perfbuf { #define DEC_64 "%"SCNd64 -static void next_line(FILE *f) { - while (fgetc(f) != '\n'); +static int next_line(FILE *f) { + int c; + do { + c = fgetc(f); + } while (c != '\n' && c != EOF); + + return c; } /** @@ -92,7 +97,10 @@ static int get_totalticks(int which, ticks *pticks) { &iowTicks, &irqTicks, &sirqTicks); // Move to next line - next_line(fh); + if (next_line(fh) == EOF) { + fclose(fh); + return -2; + } //find the line for requested cpu faster to just iterate linefeeds? if (which != -1) { @@ -105,7 +113,10 @@ static int get_totalticks(int which, ticks *pticks) { fclose(fh); return -2; } - next_line(fh); + if (next_line(fh) == EOF) { + fclose(fh); + return -2; + } } n = fscanf(fh, "cpu%*d " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 " " DEC_64 "\n",