From: Elias Naur Date: Thu, 21 Mar 2019 15:41:48 +0000 (+0100) Subject: cmd/link/internal/ld: don't leave files open in a loop X-Git-Tag: go1.13beta1~947 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=804a4024ec9b149410df6aeaa8b443e65e70ff28;p=gostls13.git cmd/link/internal/ld: don't leave files open in a loop Noticed by Ingo Oeser in his review of CL 168321. Change-Id: I2f39db675a7c22b395062a11903657a9d0d1956d Reviewed-on: https://go-review.googlesource.com/c/go/+/168560 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor Reviewed-by: Cherry Zhang Reviewed-by: Brad Fitzpatrick --- diff --git a/src/cmd/link/internal/ld/macho.go b/src/cmd/link/internal/ld/macho.go index 98359c26fc..d13857081a 100644 --- a/src/cmd/link/internal/ld/macho.go +++ b/src/cmd/link/internal/ld/macho.go @@ -694,14 +694,20 @@ func Asmbmacho(ctxt *Link) { } } } - load, err := hostobjMachoPlatform(hostobj) - if err != nil { - Exitf("%v", err) + foundLoad := false + for _, h := range hostobj { + load, err := hostobjMachoPlatform(&h) + if err != nil { + Exitf("%v", err) + } + if load != nil { + ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data))) + copy(ml.data, load.cmd.data) + foundLoad = true + break + } } - if load != nil { - ml := newMachoLoad(ctxt.Arch, load.cmd.type_, uint32(len(load.cmd.data))) - copy(ml.data, load.cmd.data) - } else if ctxt.LinkMode == LinkInternal { + if !foundLoad && ctxt.LinkMode == LinkInternal { // For lldb, must say LC_VERSION_MIN_MACOSX or else // it won't know that this Mach-O binary is from OS X // (could be iOS or WatchOS instead). @@ -1027,29 +1033,20 @@ func Machoemitreloc(ctxt *Link) { } // hostobjMachoPlatform returns the first platform load command found -// in the host objects, if any. -func hostobjMachoPlatform(hostobj []Hostobj) (*MachoPlatformLoad, error) { - for _, h := range hostobj { - f, err := os.Open(h.file) - if err != nil { - return nil, fmt.Errorf("%s: failed to open host object: %v\n", h.file, err) - } - defer f.Close() - sr := io.NewSectionReader(f, h.off, h.length) - m, err := macho.NewFile(sr) - if err != nil { - // Not a valid Mach-O file. - return nil, nil - } - load, err := peekMachoPlatform(m) - if err != nil { - return nil, err - } - if load != nil { - return load, nil - } +// in the host object, if any. +func hostobjMachoPlatform(h *Hostobj) (*MachoPlatformLoad, error) { + f, err := os.Open(h.file) + if err != nil { + return nil, fmt.Errorf("%s: failed to open host object: %v\n", h.file, err) } - return nil, nil + defer f.Close() + sr := io.NewSectionReader(f, h.off, h.length) + m, err := macho.NewFile(sr) + if err != nil { + // Not a valid Mach-O file. + return nil, nil + } + return peekMachoPlatform(m) } // peekMachoPlatform returns the first LC_VERSION_MIN_* or LC_BUILD_VERSION