Skip to content

Commit 0fbd4f5

Browse files
committed
Don't call open("") (#1336)
strings.Split(s, sep) returns a slice of a single element containing s if sep is not found in s. This is true even if s is empty. As a result, every call to flagFromEnvOrFile results in an attempt to open a file with empty name. This is seen from strace as [pid 3287620] openat(AT_FDCWD, "", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) [pid 3287620] openat(AT_FDCWD, "", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory) ... To fix, check if the string is empty before calling ReadFile. This also fixes cases where filePath is non-empty but has extra commas. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com> (cherry picked from commit 3df9a3c) Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
1 parent af7fa3d commit 0fbd4f5

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

flag.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,10 @@ func flagFromFileEnv(filePath, envName string) (val string, ok bool) {
338338
}
339339
}
340340
for _, fileVar := range strings.Split(filePath, ",") {
341-
if data, err := ioutil.ReadFile(fileVar); err == nil {
342-
return string(data), true
341+
if fileVar != "" {
342+
if data, err := ioutil.ReadFile(fileVar); err == nil {
343+
return string(data), true
344+
}
343345
}
344346
}
345347
return "", false

0 commit comments

Comments
 (0)