dundee / gdu

Fast disk usage analyzer with console interface written in Go
MIT License
3.75k stars 137 forks source link

Division by zero in formatFileRow() causes gdu to hang #358

Closed xroberx closed 3 months ago

xroberx commented 3 months ago

Description:

gdu hangs when entering a directory in which all the files have a size of 0

Steps to reproduce the behavior:

  1. mkdir /tmp/gdu_test
  2. touch /tmp/gdu_test/empty_file.txt
  3. gdu /tmp/gdu_test

The bug is in the following code from tui/format.go:

case ui.ShowApparentSize: 
    part = int(float64(item.GetSize()) / float64(maxSize) * 100.0)   
default:   
    part = int(float64(item.GetUsage()) / float64(maxUsage) * 100.0)  

When all the items in the target directory have a size of 0, then maxSize/maxUsage = 0. Also, item.GetSize() and item.GetUsage() are equal to 0. This results in a 0/0 division, which equals NaN. Then, the casting to int results in the following number: -9223372036854775808.

If at least one file in the target directory has a size > 0, then the bug is not triggered. This is because maxSize/maxUsage are > 0, which avoids dividing by 0.

dundee commented 3 months ago

Thank you for your contribution!