Jelajahi Sumber

windows/registry: do not generate unaligned loads

Byte slices aren't necessarily aligned, which means casting them to
integer types and dereferencing may result in an unaligned load. This
is mostly fine on Intel but isn't necessarily fine on other platforms.
Any good compiler will generate optimal code for the platform using the
pattern of this commit.

Change-Id: I6dd8debad1cb850b8562ee96ae0f366d1f822a6f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/176857
Run-TryBot: Jason Donenfeld <Jason@zx2c4.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Jason A. Donenfeld 6 tahun lalu
induk
melakukan
c46e0d965b
1 mengubah file dengan 6 tambahan dan 3 penghapusan
  1. 6 3
      windows/registry/value.go

+ 6 - 3
windows/registry/value.go

@@ -68,7 +68,7 @@ func (k Key) GetValue(name string, buf []byte) (n int, valtype uint32, err error
 	return int(l), valtype, nil
 }
 
-func (k Key) getValue(name string, buf []byte) (date []byte, valtype uint32, err error) {
+func (k Key) getValue(name string, buf []byte) (data []byte, valtype uint32, err error) {
 	p, err := syscall.UTF16PtrFromString(name)
 	if err != nil {
 		return nil, 0, err
@@ -241,12 +241,15 @@ func (k Key) GetIntegerValue(name string) (val uint64, valtype uint32, err error
 		if len(data) != 4 {
 			return 0, typ, errors.New("DWORD value is not 4 bytes long")
 		}
-		return uint64(*(*uint32)(unsafe.Pointer(&data[0]))), DWORD, nil
+		var val32 uint32
+		copy((*[4]byte)(unsafe.Pointer(&val32))[:], data)
+		return uint64(val32), DWORD, nil
 	case QWORD:
 		if len(data) != 8 {
 			return 0, typ, errors.New("QWORD value is not 8 bytes long")
 		}
-		return uint64(*(*uint64)(unsafe.Pointer(&data[0]))), QWORD, nil
+		copy((*[8]byte)(unsafe.Pointer(&val))[:], data)
+		return val, QWORD, nil
 	default:
 		return 0, typ, ErrUnexpectedType
 	}