Disk: don't convert NAN to integer; don't print 0B / 0B (0%)

fix ASAN warning: `runtime error: nan is outside the range of representable values of type 'unsigned char'`
This commit is contained in:
李通洲
2022-10-18 18:50:38 +08:00
parent e3ea39afaa
commit 3d5ef186ba
3 changed files with 11 additions and 12 deletions
-7
View File
@@ -19,13 +19,6 @@ const FFDiskResult* ffDetectDisks()
if(result.disks.length == 0 && result.error.length == 0)
ffStrbufAppendS(&result.error, "No disks found");
for(uint32_t i = 0; i < result.disks.length; ++i)
{
FFDisk* disk = ffListGet(&result.disks, i);
disk->bytesPercentage = (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0);
disk->filesPercentage = (uint8_t) (((long double) disk->filesUsed / (long double) disk->filesTotal) * 100.0);
}
//We need to sort the disks, so that we can detect, which disk a path resides on
// For example for /boot/efi/bootmgr we need to check /boot/efi before /boot
//Note that we sort alphabetically here for a better ordering when printing the list,
-2
View File
@@ -21,11 +21,9 @@ typedef struct FFDisk
uint64_t bytesUsed;
uint64_t bytesTotal;
uint8_t bytesPercentage;
uint32_t filesUsed;
uint32_t filesTotal;
uint8_t filesPercentage;
} FFDisk;
typedef struct FFDiskResult
+11 -3
View File
@@ -30,23 +30,31 @@ static void printDisk(FFinstance* instance, const FFDisk* disk)
ffStrbufInit(&totalPretty);
ffParseSize(disk->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
uint8_t bytesPercentage = disk->bytesTotal > 0 ? (uint8_t) (((long double) disk->bytesUsed / (long double) disk->bytesTotal) * 100.0) : 0;
if(instance->config.disk.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
printf("%s / %s (%u%%)", usedPretty.chars, totalPretty.chars, disk->bytesPercentage);
if(disk->bytesTotal > 0)
printf("%s / %s (%u%%)", usedPretty.chars, totalPretty.chars, bytesPercentage);
else
fputs("unknown", stdout);
if(disk->type == FF_DISK_TYPE_EXTERNAL)
printf(" [Removable]");
putchar('\n');
}
else
{
uint8_t filesPercentage = disk->filesTotal > 0 ? (uint8_t) (((double) disk->filesUsed / (double) disk->filesTotal) * 100.0) : 0;
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.disk.outputFormat, FF_DISK_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
{FF_FORMAT_ARG_TYPE_UINT8, &disk->bytesPercentage},
{FF_FORMAT_ARG_TYPE_UINT8, &bytesPercentage},
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesUsed},
{FF_FORMAT_ARG_TYPE_UINT, &disk->filesTotal},
{FF_FORMAT_ARG_TYPE_UINT8, &disk->filesPercentage},
{FF_FORMAT_ARG_TYPE_UINT8, &filesPercentage},
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_EXTERNAL)},
{FF_FORMAT_ARG_TYPE_BOOL, FF_FORMAT_ARG_VALUE_BOOL(disk->type == FF_DISK_TYPE_HIDDEN)},
{FF_FORMAT_ARG_TYPE_STRBUF, &disk->filesystem},