Fix infinite loop in pg_diskusage
Also, new fields added in Linux 4.18 and 5.7 are now scanned and returned. Some of the code formatting has been tweaked to match organization in Linux's genhd.c:diskstats_show code to ease comparison.
As new kernels have added fields to /proc/diskstats, the old parsing code would fail to scan. Not matching anything in the scan meant never making progress towards EOF. The result was an infinite loop until the tuplestore exhausted resources.
This is addressed several ways:
-
Scanning terminates if there are no matched fields, i.e. no progress towards EOF. Checking for EOF is redundant.
-
Because spaces and even adjacent format characters will consume newlines, a "HS" (Horizontal Space) macro is used to terminate fscanf matching should a newline character be found in-between scanned items. This is important on any system with fewer fields, such that scanning does not jump between lines as one record.
-
In event there is more data on the line, i.e. Linux has added even more fields that are otherwise un-scanned, these are consumed up to the next newline and discarded.