matcha icon indicating copy to clipboard operation
matcha copied to clipboard

BUG: Unchecked file Close() error in update asset download

Open andrinoff opened this issue 7 months ago • 1 comments

Describe the bug In main.go line 3157, outFile.Close() is called but error return is ignored. If Close() fails (e.g., due to disk full), the function returns success even though file write may have failed.

To reproduce

  1. Examine main.go lines 3151-3160:
outFile, err := os.Create(assetPath)
if err != nil {
    return fmt.Errorf("could not create temp file: %w", err)
}
_, err = io.Copy(outFile, respAsset.Body)
outFile.Close()  // Error ignored
if err != nil {
    return fmt.Errorf("could not write asset to disk: %w", err)
}
  1. Notice Close() error not checked before checking io.Copy() error

Expected behavior Close() error should be checked. On many filesystems, buffered writes only fail on close, not during Copy(). Current code could succeed even with incomplete write.

Additional context

  • Good first issue: simple error handling fix
  • Common mistake: Close() can fail and must be checked for write operations
  • Best practice: use defer with explicit error check or named return value
  • Related to update/download functionality

Suggested fix (option 1 - sequential close):

_, err = io.Copy(outFile, respAsset.Body)
if err != nil {
    outFile.Close() // Cleanup but don't mask copy error
    return fmt.Errorf("could not write asset to disk: %w", err)
}
if err := outFile.Close(); err != nil {
    return fmt.Errorf("could not finalize asset file: %w", err)
}

Suggested fix (option 2 - defer with error):

outFile, err := os.Create(assetPath)
if err != nil {
    return fmt.Errorf("could not create temp file: %w", err)
}
defer func() {
    if cerr := outFile.Close(); cerr != nil && err == nil {
        err = fmt.Errorf("could not finalize asset file: %w", cerr)
    }
}()
_, err = io.Copy(outFile, respAsset.Body)
if err != nil {
    return fmt.Errorf("could not write asset to disk: %w", err)
}

OS All platforms

andrinoff avatar Apr 21 '26 08:04 andrinoff

This has had no activity for 30 days. It will be closed in 14 days unless updated. Comment or remove the stale label to keep it open.

floatpanebot avatar May 22 '26 06:05 floatpanebot