This is an automated email from the ASF dual-hosted git repository.

LinkinStars pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/answer.git


The following commit(s) were added to refs/heads/dev by this push:
     new e23d0949 fix(dir): report an error instead of panicking when the path 
is missing (#1660)
e23d0949 is described below

commit e23d0949d9c283c560eeb5f790655f30ce5dcb38
Author: bob <[email protected]>
AuthorDate: Tue Nov 3 16:56:21 2026 +0800

    fix(dir): report an error instead of panicking when the path is missing 
(#1660)
    
    Fixes #
    
    ## Proposed Changes
    
    - `pkg/dir.DirSize` now checks the error its `filepath.Walk` callback is
    given before it uses the `FileInfo`. `filepath.Walk` passes a nil `info`
    when the root cannot be stat-ed, so a missing path used to raise
    `invalid memory address or nil pointer dereference` instead of returning
    the error.
    - The admin dashboard's `calculateStorage`
    (`internal/service/dashboard/dashboard_service.go:334`) already logs and
    swallows an error from `DirSize`; with the panic that branch was
    unreachable, so a missing or unmounted upload directory turned the
    dashboard into a crash rather than an empty size.
    - Adds `pkg/dir/dir_test.go`: the happy path plus three missing-path
    cases that must return an error and must not panic.
    
    ## Additional Info
    
    Reproducer on `main` (`6744295`): `dir.DirSize("/no/such/dir")` panics
    with SIGSEGV at
    `pkg/dir/dir.go:45`. After the change it returns the `lstat …: no such
    file or directory`
    error. No new dependency; `golangci-lint v2.6.2` (`make lint`) and `go
    mod tidy` are clean.
---
 pkg/dir/dir.go      |  7 +++++-
 pkg/dir/dir_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+), 1 deletion(-)

diff --git a/pkg/dir/dir.go b/pkg/dir/dir.go
index 09591f6c..bfd1b110 100644
--- a/pkg/dir/dir.go
+++ b/pkg/dir/dir.go
@@ -42,10 +42,15 @@ func CheckFileExist(path string) bool {
 func DirSize(path string) (int64, error) {
        var size int64
        err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) 
error {
+               // filepath.Walk passes a nil info when it cannot stat the 
root, so the
+               // error has to be handled before info is dereferenced.
+               if err != nil {
+                       return err
+               }
                if !info.IsDir() {
                        size += info.Size()
                }
-               return err
+               return nil
        })
        return size, err
 }
diff --git a/pkg/dir/dir_test.go b/pkg/dir/dir_test.go
new file mode 100644
index 00000000..80241427
--- /dev/null
+++ b/pkg/dir/dir_test.go
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package dir
+
+import (
+       "os"
+       "path/filepath"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+       "github.com/stretchr/testify/require"
+)
+
+func TestDirSize(t *testing.T) {
+       dir := t.TempDir()
+       require.NoError(t, os.WriteFile(filepath.Join(dir, "a.txt"), 
[]byte("12345"), 0o600))
+       require.NoError(t, os.WriteFile(filepath.Join(dir, "b.txt"), 
[]byte("1234567890"), 0o600))
+       require.NoError(t, os.MkdirAll(filepath.Join(dir, "sub"), 0o700))
+       require.NoError(t, os.WriteFile(filepath.Join(dir, "sub", "c.txt"), 
[]byte("1234567"), 0o600))
+
+       size, err := DirSize(dir)
+       require.NoError(t, err)
+       assert.Equal(t, int64(22), size)
+}
+
+// A path that does not exist must be reported as an error. Before the fix
+// filepath.Walk hands the callback a nil FileInfo for the root, and
+// `info.IsDir()` panicked with a nil pointer dereference instead.
+func TestDirSizeMissingPath(t *testing.T) {
+       missing := filepath.Join(t.TempDir(), "no-such-dir")
+
+       size, err := DirSize(missing)
+       require.Error(t, err)
+       assert.Equal(t, int64(0), size)
+}
+
+// Same for a path whose parent is missing, and for a relative path that
+// cannot be resolved from the working directory.
+func TestDirSizeMissingPathsDoNotPanic(t *testing.T) {
+       for _, missing := range []string{"no/such/relative/dir", 
"/no/such/absolute/dir"} {
+               t.Run(missing, func(t *testing.T) {
+                       size, err := DirSize(missing)
+                       require.Error(t, err)
+                       assert.Equal(t, int64(0), size)
+               })
+       }
+}

Reply via email to