Skip to content

Commit

Permalink
refactor(pkg, tests): clean potential file inclusion via variable (#577)
Browse files Browse the repository at this point in the history
Signed-off-by: aSquare14 <[email protected]>
  • Loading branch information
asquare14 authored and kmova committed May 10, 2021
1 parent dfe4a13 commit bed93eb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/ndm_daemonset/probe/usedbyprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package probe
import (
"errors"
"os"
"path/filepath"
"strings"
"syscall"

Expand Down Expand Up @@ -200,7 +201,7 @@ func getBlockDeviceZFSPartition(bd blockdevice.BlockDevice) (string, bool) {
// isBlockDeviceInUseByKernel tries to open the device exclusively to check if the device is
// being held by some process. eg: If kernel zfs uses the disk, the open will fail
func isBlockDeviceInUseByKernel(path string) (bool, error) {
f, err := os.OpenFile(path, os.O_EXCL, 0444)
f, err := os.OpenFile(filepath.Clean(path), os.O_EXCL, 0444)

if errors.Is(err, syscall.EBUSY) {
return true, nil
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/utils/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

// GetYAMLString gets the yaml-string from the given YAML file
func GetYAMLString(fileName string) (string, error) {
fileBytes, err := ioutil.ReadFile(fileName)
fileBytes, err := ioutil.ReadFile(filepath.Clean(fileName))
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/sysfs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,23 @@ package sysfs

import (
"io/ioutil"
"path/filepath"
"strconv"
"strings"
)

// readSysFSFileAsInt64 reads a file and
// converts that content into int64
func readSysFSFileAsInt64(sysFilePath string) (int64, error) {
b, err := ioutil.ReadFile(sysFilePath)
b, err := ioutil.ReadFile(filepath.Clean(sysFilePath))
if err != nil {
return 0, err
}
return strconv.ParseInt(strings.TrimSuffix(string(b), "\n"), 10, 64)
}

func readSysFSFileAsString(sysFilePath string) (string, error) {
b, err := ioutil.ReadFile(sysFilePath)
b, err := ioutil.ReadFile(filepath.Clean(sysFilePath))
if err != nil {
return "", err
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/udev/mockdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ package udev
import "C"
import (
"bufio"
bd "github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/pkg/mount"
"github.com/openebs/node-disk-manager/pkg/sysfs"
"io/ioutil"
"os"
"path/filepath"
"strings"

bd "github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/pkg/mount"
"github.com/openebs/node-disk-manager/pkg/sysfs"
)

// MockOsDiskDetails struct contain different attribute of os disk.
Expand Down Expand Up @@ -140,7 +142,7 @@ func OsDiskName() (string, string, error) {

// getSyspathOfOsDisk returns syspath of os disk in success
func getSyspathOfOsDisk(osDiskName string) (string, error) {
data, err := ioutil.ReadFile("/sys/class/block/" + osDiskName + "/dev")
data, err := ioutil.ReadFile(filepath.Clean("/sys/class/block/" + osDiskName + "/dev"))
if err != nil {
return "", err
}
Expand All @@ -149,7 +151,7 @@ func getSyspathOfOsDisk(osDiskName string) (string, error) {

// getOsDiskSize returns size of os disk in success
func getOsDiskSize(osDiskName string) (string, error) {
sizeByte, err := ioutil.ReadFile("/sys/class/block/" + osDiskName + "/size")
sizeByte, err := ioutil.ReadFile(filepath.Clean("/sys/class/block/" + osDiskName + "/size"))
if err != nil {
return "", err
}
Expand Down

0 comments on commit bed93eb

Please sign in to comment.