Commit 3811c4d8 authored by Russ Cox's avatar Russ Cox

debug/pe: remove use of unsafe

Helps in environments with restricted support for unsafe.

LGTM=bradfitz
R=r, bradfitz
CC=dsymonds, golang-codereviews
https://golang.org/cl/156410044
parent 0edafefc
...@@ -13,7 +13,6 @@ import ( ...@@ -13,7 +13,6 @@ import (
"io" "io"
"os" "os"
"strconv" "strconv"
"unsafe"
) )
// A File represents an open PE file. // A File represents an open PE file.
...@@ -125,6 +124,11 @@ func (f *File) Close() error { ...@@ -125,6 +124,11 @@ func (f *File) Close() error {
return err return err
} }
var (
sizeofOptionalHeader32 = uintptr(binary.Size(OptionalHeader32{}))
sizeofOptionalHeader64 = uintptr(binary.Size(OptionalHeader64{}))
)
// NewFile creates a new File for accessing a PE binary in an underlying reader. // NewFile creates a new File for accessing a PE binary in an underlying reader.
func NewFile(r io.ReaderAt) (*File, error) { func NewFile(r io.ReaderAt) (*File, error) {
f := new(File) f := new(File)
...@@ -206,7 +210,7 @@ func NewFile(r io.ReaderAt) (*File, error) { ...@@ -206,7 +210,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
var oh32 OptionalHeader32 var oh32 OptionalHeader32
var oh64 OptionalHeader64 var oh64 OptionalHeader64
switch uintptr(f.FileHeader.SizeOfOptionalHeader) { switch uintptr(f.FileHeader.SizeOfOptionalHeader) {
case unsafe.Sizeof(oh32): case sizeofOptionalHeader32:
if err := binary.Read(sr, binary.LittleEndian, &oh32); err != nil { if err := binary.Read(sr, binary.LittleEndian, &oh32); err != nil {
return nil, err return nil, err
} }
...@@ -214,7 +218,7 @@ func NewFile(r io.ReaderAt) (*File, error) { ...@@ -214,7 +218,7 @@ func NewFile(r io.ReaderAt) (*File, error) {
return nil, fmt.Errorf("pe32 optional header has unexpected Magic of 0x%x", oh32.Magic) return nil, fmt.Errorf("pe32 optional header has unexpected Magic of 0x%x", oh32.Magic)
} }
f.OptionalHeader = &oh32 f.OptionalHeader = &oh32
case unsafe.Sizeof(oh64): case sizeofOptionalHeader64:
if err := binary.Read(sr, binary.LittleEndian, &oh64); err != nil { if err := binary.Read(sr, binary.LittleEndian, &oh64); err != nil {
return nil, err return nil, err
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment