From 80ce15901faa3a069b817ae42a2abe95e5cc50ea Mon Sep 17 00:00:00 2001 From: sonnemusk Date: Thu, 23 Jul 2026 18:35:23 +0800 Subject: [PATCH] fix: reject empty filename in LoadFile Trim path and error clearly when the filename is blank. --- empty_file_test.go | 12 ++++++++++++ load.go | 4 ++++ 2 files changed, 16 insertions(+) create mode 100644 empty_file_test.go diff --git a/empty_file_test.go b/empty_file_test.go new file mode 100644 index 0000000..9aeb739 --- /dev/null +++ b/empty_file_test.go @@ -0,0 +1,12 @@ +package properties + +import "testing" + +func TestLoadFileEmptyPath(t *testing.T) { + if _, err := LoadFile(" ", UTF8); err == nil { + t.Fatal("expected error") + } + if _, err := LoadFile("", UTF8); err == nil { + t.Fatal("expected error") + } +} diff --git a/load.go b/load.go index 3d5573f..57337aa 100644 --- a/load.go +++ b/load.go @@ -195,6 +195,10 @@ func LoadMap(m map[string]string) *Properties { // LoadFile reads a file into a Properties struct. func LoadFile(filename string, enc Encoding) (*Properties, error) { + filename = strings.TrimSpace(filename) + if filename == "" { + return nil, fmt.Errorf("properties: empty filename") + } l := &Loader{Encoding: enc} return l.LoadAll([]string{filename}) }