From 6d8cb5218ef717b43b6b994456467c1decb805c8 Mon Sep 17 00:00:00 2001 From: Shengjing Zhu Date: Thu, 16 Jan 2020 22:30:22 +0800 Subject: [PATCH] fix slice size overflow on mipsle Fix following errors on mipsle: src/github.com/containerd/btrfs/btrfs.go:133:15: type [2147483647]byte larger than address space src/github.com/containerd/btrfs/btrfs.go:274:15: type [2147483647]byte larger than address space src/github.com/containerd/btrfs/btrfs.go:310:15: type [2147483647]byte larger than address space src/github.com/containerd/btrfs/btrfs.go:369:15: type [2147483647]byte larger than address space src/github.com/containerd/btrfs/helpers.go:53:9: type [2147483647]byte larger than address space Also fix some trivial document typos. Signed-off-by: Shengjing Zhu --- README.md | 2 +- btrfs.go | 32 ++++++++++---------------------- doc.go | 15 --------------- helpers.go | 17 +---------------- info.go | 15 --------------- ioctl.go | 15 --------------- 6 files changed, 12 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 1fcce78..a05299d 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ This package may not cover all the use cases for btrfs. If something you need is missing, please don't hesitate to submit a PR. Note that due to struct alignment issues, this isn't yet fully native. -Preferrably, this could be resolved, so contributions in this direction are +Preferably, this could be resolved, so contributions in this direction are greatly appreciated. ## Applying License Header to New Files diff --git a/btrfs.go b/btrfs.go index 27b7bc4..a055890 100644 --- a/btrfs.go +++ b/btrfs.go @@ -14,25 +14,8 @@ limitations under the License. */ -/* - Copyright The containerd Authors - - Licensed 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 btrfs -import "sort" - /* #include #include @@ -47,12 +30,17 @@ import "C" import ( "os" "path/filepath" + "sort" "syscall" "unsafe" "github.com/pkg/errors" ) +// maxByteSliceSize is the smallest size that Go supports on various platforms. +// On mipsle, 1<<31-1 overflows the address space. +const maxByteSliceSize = 1 << 30 + // IsSubvolume returns nil if the path is a valid subvolume. An error is // returned if the path does not exist or the path is not a valid subvolume. func IsSubvolume(path string) error { @@ -146,7 +134,7 @@ func subvolMap(path string) (map[uint64]*Info, error) { var ( sh C.struct_btrfs_ioctl_search_header shSize = unsafe.Sizeof(sh) - buf = (*[1<<31 - 1]byte)(unsafe.Pointer(&args.buf[0]))[:C.BTRFS_SEARCH_ARGS_BUFSIZE] + buf = (*[maxByteSliceSize]byte)(unsafe.Pointer(&args.buf[0]))[:C.BTRFS_SEARCH_ARGS_BUFSIZE] ) for i := 0; i < int(args.key.nr_items); i++ { @@ -287,7 +275,7 @@ func SubvolCreate(path string) error { if len(name) > C.BTRFS_PATH_NAME_MAX { return errors.Errorf("%q too long for subvolume", name) } - nameptr := (*[1<<31 - 1]byte)(unsafe.Pointer(&args.name[0])) + nameptr := (*[maxByteSliceSize]byte)(unsafe.Pointer(&args.name[0])) copy(nameptr[:C.BTRFS_PATH_NAME_MAX], []byte(name)) if err := ioctl(fp.Fd(), C.BTRFS_IOC_SUBVOL_CREATE, uintptr(unsafe.Pointer(&args))); err != nil { @@ -304,7 +292,7 @@ func SubvolSnapshot(dst, src string, readonly bool) error { dstfp, err := openSubvolDir(dstdir) if err != nil { - return errors.Wrapf(err, "opening snapshot desination subvolume failed") + return errors.Wrapf(err, "opening snapshot destination subvolume failed") } defer dstfp.Close() @@ -323,7 +311,7 @@ func SubvolSnapshot(dst, src string, readonly bool) error { return errors.Errorf("%q too long for subvolume", dstname) } - nameptr := (*[1<<31 - 1]byte)(unsafe.Pointer(name)) + nameptr := (*[maxByteSliceSize]byte)(unsafe.Pointer(name)) copy(nameptr[:C.BTRFS_SUBVOL_NAME_MAX], []byte(dstname)) if readonly { @@ -382,7 +370,7 @@ func SubvolDelete(path string) error { return errors.Errorf("%q too long for subvolume", name) } - nameptr := (*[1<<31 - 1]byte)(unsafe.Pointer(&args.name[0])) + nameptr := (*[maxByteSliceSize]byte)(unsafe.Pointer(&args.name[0])) copy(nameptr[:C.BTRFS_SUBVOL_NAME_MAX], []byte(name)) if err := ioctl(fp.Fd(), C.BTRFS_IOC_SNAP_DESTROY, uintptr(unsafe.Pointer(&args))); err != nil { diff --git a/doc.go b/doc.go index 11ee64f..6aaf2d0 100644 --- a/doc.go +++ b/doc.go @@ -14,20 +14,5 @@ limitations under the License. */ -/* - Copyright The containerd Authors - - Licensed 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 btrfs provides bindings for working with btrfs partitions from Go. package btrfs diff --git a/helpers.go b/helpers.go index c06f953..475f1c6 100644 --- a/helpers.go +++ b/helpers.go @@ -14,21 +14,6 @@ limitations under the License. */ -/* - Copyright The containerd Authors - - Licensed 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 btrfs /* @@ -66,7 +51,7 @@ var ( ) func uuidString(uuid *[C.BTRFS_UUID_SIZE]C.u8) string { - b := (*[1<<31 - 1]byte)(unsafe.Pointer(uuid))[:C.BTRFS_UUID_SIZE] + b := (*[maxByteSliceSize]byte)(unsafe.Pointer(uuid))[:C.BTRFS_UUID_SIZE] if bytes.Equal(b, zeros) { return "" diff --git a/info.go b/info.go index 83278a7..0f96be6 100644 --- a/info.go +++ b/info.go @@ -14,21 +14,6 @@ limitations under the License. */ -/* - Copyright The containerd Authors - - Licensed 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 btrfs // Info describes metadata about a btrfs subvolume. diff --git a/ioctl.go b/ioctl.go index 29354db..bac1dbd 100644 --- a/ioctl.go +++ b/ioctl.go @@ -14,21 +14,6 @@ limitations under the License. */ -/* - Copyright The containerd Authors - - Licensed 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 btrfs import "syscall"