iOliverNguyen / ujson

µjson - A fast and minimal JSON parser and transformer that works on unstructured JSON
MIT License
76 stars 10 forks source link

panic: runtime error: index out of range [0] with length 0 #3

Open dwertent opened 7 months ago

dwertent commented 7 months ago

Here is the function I implemented:

func loadMetadataFromPath(appFs afero.Fs, rootPath string) (*metav1.ObjectMeta, error) {
    input, err := afero.ReadFile(appFs, rootPath)
    if err != nil {
        return nil, fmt.Errorf("failed to read file %s: %w", rootPath, err)
    }
    data := metav1.ObjectMeta{
        Annotations: map[string]string{},
        Labels:      map[string]string{},
    }
    // ujson parsing
    var parent string
    err = ujson.Walk(input, func(level int, key, value []byte) bool {
        switch level {
        case 1:
            // read name
            if bytes.EqualFold(key, []byte(`"name"`)) {
                data.Name = unquote(value)
            }
            // read namespace
            if bytes.EqualFold(key, []byte(`"namespace"`)) {
                data.Namespace = unquote(value)
            }
            // record parent for level 3
            parent = unquote(key)
        case 2:
            // read annotations
            if parent == "annotations" {
                data.Annotations[unquote(key)] = unquote(value)
            }
            // read labels
            if parent == "labels" {
                data.Labels[unquote(key)] = unquote(value)
            }
        }
        return true
    })
    if err != nil {
        return nil, fmt.Errorf("failed to parse file %s: %w", rootPath, err)
    }
    return &data, nil
}

func unquote(value []byte) string {
    buf, err := ujson.Unquote(value)
    if err != nil {
        return string(value)
    }
    return string(buf)
}

And here is the panic:

panic: runtime error: index out of range [0] with length 0

goroutine 107 [running]:
github.com/olvrng/ujson.Walk({0xc000222400, 0x44bb880?, 0x200}, 0xc0025738d8)
    /go/pkg/mod/github.com/olvrng/ujson@v1.1.0/µjson.go:87 +0x990
github.com/kubescape/storage/pkg/cleanup.loadMetadataFromPath({0x2db8240?, 0x44bb880?}, {0xc0033b3380, 0x76})
    /work/pkg/cleanup/cleanup.go:144 +0x1fc
github.com/kubescape/storage/pkg/cleanup.(*ResourcesCleanupHandler).StartCleanupTask.func1({0xc0033b3380, 0x76}, {0x2da8a68?, 0xc0033d36c0?}, {0x0?, 0x0?})
    /work/pkg/cleanup/cleanup.go:96 +0xfa
github.com/spf13/afero.walk({0x2db8240, 0x44bb880}, {0xc0033b3380, 0x76}, {0x2da8a68, 0xc0033d36c0}, 0xc002573ed0)
    /go/pkg/mod/github.com/spf13/afero@v1.11.0/path.go:44 +0x6c
github.com/spf13/afero.walk({0x2db8240, 0x44bb880}, {0xc002fe9ef0, 0x4c}, {0x2da8a68, 0xc0033d3520}, 0xc002573ed0)
    /go/pkg/mod/github.com/spf13/afero@v1.11.0/path.go:69 +0x2f4
github.com/spf13/afero.walk({0x2db8240, 0x44bb880}, {0xc001d0bb80, 0x3f}, {0x2da8a68, 0xc0031ea680}, 0xc002573ed0)
    /go/pkg/mod/github.com/spf13/afero@v1.11.0/path.go:69 +0x2f4
github.com/spf13/afero.Walk({0x2db8240, 0x44bb880}, {0xc001d0bb80, 0x3f}, 0xc0028f9ed0)
    /go/pkg/mod/github.com/spf13/afero@v1.11.0/path.go:105 +0x7a
github.com/kubescape/storage/pkg/cleanup.(*ResourcesCleanupHandler).StartCleanupTask(0xc000a8e3f0)
    /work/pkg/cleanup/cleanup.go:86 +0x58f
created by main.main in goroutine 1
    /work/main.go:84 +0x6ed
Stream closed EOF for kubescape/storage-c845d4659-8lkcg (apiserver)

I don't understand how we get this "out of range". Is this a known issue?

iOliverNguyen commented 3 months ago

Hi @dwertent, could you try with the latest version? If the issue is still there, could you share the json?