1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-23 08:55:25 +02:00

Bug 553674: Make Binary parsers and related classes Autocloseable

And prepare to make it an error in CDT to not have properly handled
an Autocloseable which means a number of fixes to make sure handles
are closed.

Change-Id: I36cd46017bbce6ece1703d688d7754e523eca68f
This commit is contained in:
Jonah Graham 2020-08-31 21:32:32 -04:00
parent 7818f6e494
commit 1562080a3c
35 changed files with 261 additions and 216 deletions

View file

@ -698,8 +698,7 @@ public class ManagedBuildTestHelper {
private static ArrayList<String> getContents(IPath fullPath) {
ArrayList<String> lines = new ArrayList<>();
try {
BufferedReader in = new BufferedReader(new FileReader(fullPath.toFile()));
try (BufferedReader in = new BufferedReader(new FileReader(fullPath.toFile()))) {
String line;
do {
line = in.readLine();
@ -876,7 +875,8 @@ public class ManagedBuildTestHelper {
return buff;
}
static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files) {
static public IPath copyFilesToTempDir(IPath srcDir, IPath tmpRootDir, IPath tmpSubDir, IPath[] files)
throws IOException {
IPath tmpSrcDir = null;
tmpSrcDir = tmpRootDir.append(tmpSubDir);
if (tmpRootDir.toString().equalsIgnoreCase(tmpSrcDir.toString())) {
@ -898,46 +898,31 @@ public class ManagedBuildTestHelper {
for (int i = 0; i < files.length; i++) {
IPath file = files[i];
IPath srcFile = srcDir.append(file);
FileReader srcReader = null;
try {
srcReader = new FileReader(srcFile.toFile());
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be read.");
return null;
}
if (file.segmentCount() > 1) {
IPath newDir = tmpSrcDir;
do {
IPath dir = file.uptoSegment(1);
newDir = newDir.append(dir);
file = file.removeFirstSegments(1);
newDir.toFile().mkdir();
if (!newDir.toFile().exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
} while (file.segmentCount() > 1);
}
IPath destFile = tmpSrcDir.append(files[i]);
FileWriter writer = null;
try {
writer = new FileWriter(destFile.toFile());
} catch (Exception e) {
Assert.fail("File " + files[i].toString() + " could not be written.");
return null;
}
try {
int c;
do {
c = srcReader.read();
if (c == -1)
break;
writer.write(c);
} while (c != -1);
srcReader.close();
writer.close();
} catch (Exception e) {
Assert.fail("File " + file.toString() + " could not be copied.");
try (FileReader srcReader = new FileReader(srcFile.toFile())) {
if (file.segmentCount() > 1) {
IPath newDir = tmpSrcDir;
do {
IPath dir = file.uptoSegment(1);
newDir = newDir.append(dir);
file = file.removeFirstSegments(1);
newDir.toFile().mkdir();
if (!newDir.toFile().exists()) {
Assert.fail("Can't create temporary directory " + tmpSrcDirFile.toString());
}
} while (file.segmentCount() > 1);
}
IPath destFile = tmpSrcDir.append(files[i]);
try (FileWriter writer = new FileWriter(destFile.toFile())) {
int c;
do {
c = srcReader.read();
if (c == -1)
break;
writer.write(c);
} while (c != -1);
}
}
}
}

View file

@ -409,11 +409,12 @@ public class MultiVersionSupportTests extends TestCase {
String expectedContent = "Converter for CDT 2.0 Project is invoked"; //$NON-NLS-1$
BufferedReader data = new BufferedReader(new FileReader(inputFile));
String actualContent;
try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) {
String actualContent;
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
}
}
} catch (IOException e) {
@ -434,11 +435,12 @@ public class MultiVersionSupportTests extends TestCase {
String expectedContent = "Converter for CDT 2.1 Project is invoked"; //$NON-NLS-1$
BufferedReader data = new BufferedReader(new FileReader(inputFile));
String actualContent;
try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) {
String actualContent;
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
}
}
} catch (IOException e) {
@ -459,11 +461,12 @@ public class MultiVersionSupportTests extends TestCase {
String expectedContent = "The converter for the projectType testProject_1.0.0 is invoked"; //$NON-NLS-1$
BufferedReader data = new BufferedReader(new FileReader(inputFile));
String actualContent;
try (BufferedReader data = new BufferedReader(new FileReader(inputFile))) {
String actualContent;
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
if ((actualContent = data.readLine()) != null) {
assertEquals(actualContent, expectedContent);
}
}
} catch (IOException e) {

View file

@ -28,8 +28,7 @@ public class ProjectConverter implements IConvertManagedBuildObject {
String tmpDir = System.getProperty("java.io.tmpdir");
File outputFile = new File(tmpDir + "/converterOutput.txt");
try {
FileWriter out = new FileWriter(outputFile);
try (FileWriter out = new FileWriter(outputFile)) {
out.write("---------- Start-------");
out.write("Converter for the build object : '" + buildObj.getName() + "' is invoked.");
out.write("From Id : " + fromId);

View file

@ -3442,18 +3442,18 @@ public class GnuMakefileGenerator implements IManagedBuilderMakefileGenerator2 {
return false;
// Get the contents of the dependency file
StringBuffer inBuffer;
InputStream contentStream = makefile.getContents(false);
Reader in = new InputStreamReader(contentStream);
StringBuffer inBuffer = null;
int chunkSize = contentStream.available();
inBuffer = new StringBuffer(chunkSize);
char[] readBuffer = new char[chunkSize];
int n = in.read(readBuffer);
while (n > 0) {
inBuffer.append(readBuffer);
n = in.read(readBuffer);
try (Reader in = new InputStreamReader(contentStream)) {
int chunkSize = contentStream.available();
inBuffer = new StringBuffer(chunkSize);
char[] readBuffer = new char[chunkSize];
int n = in.read(readBuffer);
while (n > 0) {
inBuffer.append(readBuffer);
n = in.read(readBuffer);
}
}
contentStream.close();
// The rest of this operation is equally expensive, so
// if we are doing an incremental build, only update the
@ -3613,6 +3613,7 @@ public class GnuMakefileGenerator implements IManagedBuilderMakefileGenerator2 {
save(outBuffer, makefile);
return true;
}
return false;
}

View file

@ -474,8 +474,7 @@ public class Binary extends Openable implements IBinary {
IBinaryObject bin = getBinaryObject();
if (bin != null) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader stream = new BufferedReader(new InputStreamReader(bin.getContents()));
try (BufferedReader stream = new BufferedReader(new InputStreamReader(bin.getContents()))) {
char[] buf = new char[512];
int len;
while ((len = stream.read(buf, 0, buf.length)) != -1) {

View file

@ -30,13 +30,18 @@ import org.eclipse.cdt.core.CCorePlugin;
* class operations.
* @see ARHeader
*/
public class AR {
public class AR implements AutoCloseable {
protected String filename;
protected ERandomAccessFile efile;
protected long strtbl_pos = -1;
private ARHeader[] headers;
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (efile != null) {
@ -197,11 +202,10 @@ public class AR {
efile.seek(obj_offset);
efile.read(temp);
} else {
efile = new ERandomAccessFile(filename, "r"); //$NON-NLS-1$
efile.seek(obj_offset);
efile.read(temp);
efile.close();
efile = null;
try (ERandomAccessFile tempfile = new ERandomAccessFile(filename, "r")) { //$NON-NLS-1$
tempfile.seek(obj_offset);
tempfile.read(temp);
}
}
return temp;
}

View file

@ -18,11 +18,12 @@ import java.io.RandomAccessFile;
import org.eclipse.cdt.core.CCorePlugin;
public class Exe {
public class Exe implements AutoCloseable {
public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
protected RandomAccessFile rfile;
ExeHeader ehdr;
private String file;
static public class ExeHeader {
@ -160,12 +161,13 @@ public class Exe {
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
buffer.append(rfile).append(NL);
buffer.append(file).append(NL);
buffer.append(ehdr);
return buffer.toString();
}
public Exe(String file) throws IOException {
this.file = file;
rfile = new RandomAccessFile(file, "r"); //$NON-NLS-1$
try {
ehdr = new ExeHeader(rfile);
@ -176,6 +178,15 @@ public class Exe {
}
}
@Override
public void close() throws Exception {
if (rfile != null) {
rfile.close();
rfile = null;
ehdr = null;
}
}
public static void main(String[] args) {
try {
Exe exe = new Exe(args[0]);

View file

@ -73,7 +73,7 @@ import org.eclipse.cdt.utils.debug.stabs.StabsReader;
* This class is planned for removal in next major release.
*/
@Deprecated
public class PE {
public class PE implements AutoCloseable {
public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
RandomAccessFile rfile;
@ -573,10 +573,14 @@ public class PE {
}
public static Attribute getAttribute(String file) throws IOException {
PE pe = new PE(file);
Attribute attrib = pe.getAttribute();
pe.dispose();
return attrib;
try (PE pe = new PE(file)) {
return pe.getAttribute();
}
}
@Override
public void close() throws IOException {
dispose();
}
public void dispose() throws IOException {

View file

@ -68,7 +68,7 @@ import org.eclipse.cdt.utils.debug.stabs.StabsReader;
* </pre>
* @since 6.9
*/
public class PE64 {
public class PE64 implements AutoCloseable {
public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
RandomAccessFile rfile;
@ -667,10 +667,14 @@ public class PE64 {
}
public static Attribute getAttribute(String file) throws IOException {
PE64 pe = new PE64(file);
Attribute attrib = pe.getAttribute();
pe.dispose();
return attrib;
try (PE64 pe = new PE64(file)) {
return pe.getAttribute();
}
}
@Override
public void close() throws IOException {
dispose();
}
public void dispose() throws IOException {

View file

@ -30,13 +30,18 @@ import org.eclipse.cdt.core.CCorePlugin;
* @see ARHeader
*/
@Deprecated
public class PEArchive {
public class PEArchive implements AutoCloseable {
protected String filename;
protected RandomAccessFile rfile;
protected long strtbl_pos = -1;
private ARHeader[] headers;
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (rfile != null) {

View file

@ -35,7 +35,9 @@ public class PEBinaryArchive extends BinaryFile implements IBinaryArchive {
public PEBinaryArchive(PEParser parser, IPath path) throws IOException {
super(parser, path, IBinaryFile.ARCHIVE);
new AR(path.toOSString()).dispose(); // check file type
try (AR ar = new AR(path.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -34,7 +34,9 @@ public class PEBinaryArchive64 extends BinaryFile implements IBinaryArchive {
public PEBinaryArchive64(PEParser64 parser, IPath path) throws IOException {
super(parser, path, IBinaryFile.ARCHIVE);
new AR(path.toOSString()).dispose(); // check file type
try (AR ar = new AR(path.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -123,33 +123,21 @@ public class PEBinaryObject extends BinaryObjectAdapter {
}
protected void loadAll() throws IOException {
PE pe = null;
try {
pe = getPE();
try (PE pe = getPE()) {
loadInfo(pe);
loadSymbols(pe);
} finally {
if (pe != null) {
pe.dispose();
}
}
}
protected void loadInfo() throws IOException {
PE pe = null;
try {
pe = getPE();
try (PE pe = getPE()) {
loadInfo(pe);
} finally {
if (pe != null) {
pe.dispose();
}
}
}
protected void loadInfo(PE pe) throws IOException {
info = new BinaryObjectInfo();
PE.Attribute attribute = getPE().getAttribute();
PE.Attribute attribute = pe.getAttribute();
info.isLittleEndian = attribute.isLittleEndian();
info.hasDebug = attribute.hasDebug();
info.cpu = attribute.getCPU();

View file

@ -125,33 +125,21 @@ public class PEBinaryObject64 extends BinaryObjectAdapter {
}
protected void loadAll() throws IOException {
PE64 pe = null;
try {
pe = getPE();
try (PE64 pe = getPE()) {
loadInfo(pe);
loadSymbols(pe);
} finally {
if (pe != null) {
pe.dispose();
}
}
}
protected void loadInfo() throws IOException {
PE64 pe = null;
try {
pe = getPE();
try (PE64 pe = getPE()) {
loadInfo(pe);
} finally {
if (pe != null) {
pe.dispose();
}
}
}
protected void loadInfo(PE64 pe) throws IOException {
info = new BinaryObjectInfo();
PE64.Attribute attribute = getPE().getAttribute();
PE64.Attribute attribute = pe.getAttribute();
info.isLittleEndian = attribute.isLittleEndian();
info.hasDebug = attribute.hasDebug();
info.cpu = attribute.getCPU();

View file

@ -186,9 +186,9 @@ public class Dwarf {
boolean printEnabled = true;
public Dwarf(String file) throws IOException {
Elf exe = new Elf(file);
init(exe);
exe.dispose();
try (Elf exe = new Elf(file)) {
init(exe);
}
}
public Dwarf(Elf exe) throws IOException {
@ -238,8 +238,9 @@ public class Dwarf {
}
if (debugFile.exists()) {
// if the debug file exists from above, open it and get the section info from it
Elf debugInfo = new Elf(debugFile.getCanonicalPath());
sections = debugInfo.getSections();
try (Elf debugInfo = new Elf(debugFile.getCanonicalPath())) {
sections = debugInfo.getSections();
}
debugInfoPath = new Path(debugFile.getCanonicalPath());
}
}
@ -264,17 +265,17 @@ public class Dwarf {
}
File altFile = altPath.toFile();
if (altFile.exists()) {
Elf altInfo = new Elf(altFile.getCanonicalPath());
Elf.Section[] altSections = altInfo.getSections();
for (Section altSection : altSections) {
String altName = altSection.toString();
for (String element : DWARF_ALT_SCNNAMES) {
if (altName.equals(element)) {
try {
dwarfAltSections.put(element, altSection.mapSectionData());
} catch (Exception e) {
e.printStackTrace();
CCorePlugin.log(e);
try (Elf altInfo = new Elf(altFile.getCanonicalPath())) {
Elf.Section[] altSections = altInfo.getSections();
for (Section altSection : altSections) {
String altName = altSection.toString();
for (String element : DWARF_ALT_SCNNAMES) {
if (altName.equals(element)) {
try {
dwarfAltSections.put(element, altSection.mapSectionData());
} catch (Exception e) {
CCorePlugin.log(e);
}
}
}
}

View file

@ -134,8 +134,9 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions
File buildIdFile = buildIdPath.toFile();
if (buildIdFile.exists()) {
// if the debug file exists from above, open it and get the section info from it
Elf debugInfo = new Elf(buildIdFile.getCanonicalPath());
sections = debugInfo.getSections();
try (Elf debugInfo = new Elf(buildIdFile.getCanonicalPath())) {
sections = debugInfo.getSections();
}
have_build_id = true;
debugInfoPath = new Path(buildIdFile.getCanonicalPath()).removeLastSegments(1);
break;
@ -185,8 +186,9 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions
}
if (debugFile.exists()) {
// if the debug file exists from above, open it and get the section info from it
Elf debugInfo = new Elf(debugFile.getCanonicalPath());
sections = debugInfo.getSections();
try (Elf debugInfo = new Elf(debugFile.getCanonicalPath())) {
sections = debugInfo.getSections();
}
debugInfoPath = new Path(debugFile.getCanonicalPath()).removeLastSegments(1);
}
}
@ -215,17 +217,17 @@ public class DwarfReader extends Dwarf implements ISymbolReader, ICompileOptions
}
File altFile = altPath.toFile();
if (altFile.exists()) {
Elf altInfo = new Elf(altFile.getCanonicalPath());
Elf.Section[] altSections = altInfo.getSections();
for (Section altSection : altSections) {
String altName = altSection.toString();
for (String element : DWARF_ALT_SectionsToParse) {
if (altName.equals(element)) {
try {
dwarfAltSections.put(element, altSection.mapSectionData());
} catch (Exception e) {
e.printStackTrace();
CCorePlugin.log(e);
try (Elf altInfo = new Elf(altFile.getCanonicalPath())) {
Elf.Section[] altSections = altInfo.getSections();
for (Section altSection : altSections) {
String altName = altSection.toString();
for (String element : DWARF_ALT_SectionsToParse) {
if (altName.equals(element)) {
try {
dwarfAltSections.put(element, altSection.mapSectionData());
} catch (Exception e) {
CCorePlugin.log(e);
}
}
}
}

View file

@ -68,14 +68,12 @@ public class Stabs {
// we support Elf and PE executable formats. try Elf
// and then PE.
try {
Elf exe = new Elf(file);
try (Elf exe = new Elf(file)) {
init(exe);
exe.dispose();
} catch (IOException e) {
PE exe = new PE(file);
init(exe);
exe.dispose();
try (PE exe = new PE(file)) {
init(exe);
}
}
}

View file

@ -31,9 +31,9 @@ public class DebugAddr2line {
DebugSymsRequestor symreq;
public DebugAddr2line(String file) throws IOException {
Elf elf = new Elf(file);
init(elf);
elf.dispose();
try (Elf elf = new Elf(file)) {
init(elf);
}
}
public DebugAddr2line(Elf elf) throws IOException {

View file

@ -46,9 +46,9 @@ public class DebugDump implements IDebugEntryRequestor {
}
void parse(String file) throws IOException {
Elf elf = new Elf(file);
parse(elf);
elf.dispose();
try (Elf elf = new Elf(file)) {
parse(elf);
}
}
void parse(Elf elf) throws IOException {

View file

@ -31,13 +31,18 @@ import org.eclipse.cdt.utils.ERandomAccessFile;
* @see ARHeader
*/
@Deprecated
public class AR {
public class AR implements AutoCloseable {
protected String filename;
protected ERandomAccessFile efile;
protected long strtbl_pos = -1;
private ARHeader[] headers;
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (efile != null) {
@ -207,11 +212,10 @@ public class AR {
efile.seek(elf_offset);
efile.read(temp);
} else {
efile = new ERandomAccessFile(filename, "r"); //$NON-NLS-1$
efile.seek(elf_offset);
efile.read(temp);
efile.close();
efile = null;
try (ERandomAccessFile tempfile = new ERandomAccessFile(filename, "r")) { //$NON-NLS-1$
tempfile.seek(elf_offset);
tempfile.read(temp);
}
}
return temp;
}
@ -237,6 +241,7 @@ public class AR {
String hdr = efile.readLine();
if (hdr == null || hdr.compareTo("!<arch>") != 0) { //$NON-NLS-1$
efile.close();
efile = null;
throw new IOException(CCorePlugin.getResourceString("Util.exception.invalidArchive")); //$NON-NLS-1$
}
}

View file

@ -41,7 +41,7 @@ import org.eclipse.cdt.utils.Addr64Factory;
import org.eclipse.cdt.utils.ERandomAccessFile;
import org.eclipse.cdt.utils.debug.dwarf.DwarfReader;
public class Elf {
public class Elf implements AutoCloseable {
public static final int ELF32_ADDR_SIZE = 4;
public static final int ELF32_OFF_SIZE = 4;
public static final int ELF64_ADDR_SIZE = 8;
@ -956,21 +956,20 @@ public class Elf {
}
public static Attribute getAttributes(String file) throws IOException {
Elf elf = new Elf(file);
Attribute attrib = elf.getAttributes();
elf.dispose();
return attrib;
try (Elf elf = new Elf(file)) {
Attribute attrib = elf.getAttributes();
return attrib;
}
}
public static Attribute getAttributes(byte[] array) throws IOException {
try (Elf emptyElf = new Elf()) {
emptyElf.ehdr = emptyElf.new ELFhdr(array);
emptyElf.sections = new Elf.Section[0];
Attribute attrib = emptyElf.getAttributes();
Elf emptyElf = new Elf();
emptyElf.ehdr = emptyElf.new ELFhdr(array);
emptyElf.sections = new Elf.Section[0];
Attribute attrib = emptyElf.getAttributes();
emptyElf.dispose();
return attrib;
return attrib;
}
}
public static boolean isElfHeader(byte[] e_ident) {
@ -978,6 +977,11 @@ public class Elf {
&& e_ident[ELFhdr.EI_MAG1] == 'E' && e_ident[ELFhdr.EI_MAG2] == 'L' && e_ident[ELFhdr.EI_MAG3] == 'F';
}
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (efile != null) {

View file

@ -26,7 +26,7 @@ import org.eclipse.cdt.utils.elf.Elf.Symbol;
*
* @see Elf
*/
public class ElfHelper {
public class ElfHelper implements AutoCloseable {
private Elf elf;
private Elf.Symbol[] dynsyms;
@ -34,6 +34,11 @@ public class ElfHelper {
private Elf.Section[] sections;
private Elf.Dynamic[] dynamics;
@Override
public void close() {
dispose();
}
public void dispose() {
if (elf != null) {
elf.dispose();

View file

@ -32,9 +32,11 @@ public class ElfBinaryArchive extends BinaryFile implements IBinaryArchive {
private ArrayList<IBinaryObject> children;
public ElfBinaryArchive(IBinaryParser parser, IPath p) throws IOException {
super(parser, p, IBinaryFile.ARCHIVE);
new AR(p.toOSString()).dispose(); // check file type
public ElfBinaryArchive(IBinaryParser parser, IPath path) throws IOException {
super(parser, path, IBinaryFile.ARCHIVE);
try (AR ar = new AR(path.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -111,27 +111,15 @@ public class ElfBinaryObject extends BinaryObjectAdapter {
}
protected void loadAll() throws IOException {
ElfHelper helper = null;
try {
helper = getElfHelper();
try (ElfHelper helper = getElfHelper()) {
loadInfo(helper);
loadSymbols(helper);
} finally {
if (helper != null) {
helper.dispose();
}
}
}
protected void loadInfo() throws IOException {
ElfHelper helper = null;
try {
helper = getElfHelper();
try (ElfHelper helper = getElfHelper()) {
loadInfo(helper);
} finally {
if (helper != null) {
helper.dispose();
}
}
}

View file

@ -184,11 +184,8 @@ public class ElfParser extends AbstractCExtension implements IBinaryParser {
}
private static PHdr[] getPHdrs(IPath path) throws IOException {
Elf elf = new Elf(path.toOSString());
try {
try (Elf elf = new Elf(path.toOSString())) {
return elf.getPHdrs();
} finally {
elf.dispose();
}
}
}

View file

@ -29,13 +29,18 @@ import org.eclipse.cdt.core.CCorePlugin;
* class operations.
* @see ARHeader
*/
public class AR {
public class AR implements AutoCloseable {
protected String filename;
protected ERandomAccessFile efile;
protected long strtbl_pos = -1;
private ARHeader[] headers;
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (efile != null) {

View file

@ -35,7 +35,9 @@ public class MachOBinaryArchive extends BinaryFile implements IBinaryArchive {
public MachOBinaryArchive(IBinaryParser parser, IPath p) throws IOException {
super(parser, p, IBinaryFile.ARCHIVE);
new AR(p.toOSString()).dispose(); // check file type
try (AR ar = new AR(p.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -33,7 +33,10 @@ public class MachOBinaryArchive64 extends BinaryFile implements IBinaryArchive {
public MachOBinaryArchive64(IBinaryParser parser, IPath p) throws IOException {
super(parser, p, IBinaryFile.ARCHIVE);
new AR(p.toOSString()).dispose(); // check file type
try (AR ar = new AR(p.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -26,7 +26,7 @@ import org.eclipse.cdt.utils.coff.ReadMemoryAccess;
*
* @author vhirsl
*/
public class AR {
public class AR implements AutoCloseable {
public static final String NL = System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
protected String filename;
@ -190,6 +190,11 @@ public class AR {
lstHeader = new LSTHeader();
}
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (file != null) {

View file

@ -39,7 +39,9 @@ public class SOMBinaryArchive extends BinaryFile implements IBinaryArchive {
*/
public SOMBinaryArchive(IBinaryParser parser, IPath path) throws IOException {
super(parser, path, IBinaryFile.ARCHIVE);
new AR(path.toOSString()).dispose(); // check file type
try (AR ar = new AR(path.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -30,7 +30,7 @@ import org.eclipse.cdt.core.CCorePlugin;
*
* @author vhirsl
*/
public class AR {
public class AR implements AutoCloseable {
protected String filename;
private RandomAccessFile file;
private ARHeader header;
@ -105,6 +105,11 @@ public class AR {
}
}
@Override
public void close() {
dispose();
}
public void dispose() {
try {
if (file != null) {
@ -365,4 +370,5 @@ public class AR {
e.printStackTrace();
}
}
}

View file

@ -39,7 +39,9 @@ public class XCOFFBinaryArchive extends BinaryFile implements IBinaryArchive {
*/
public XCOFFBinaryArchive(IBinaryParser parser, IPath path) throws IOException {
super(parser, path, IBinaryFile.ARCHIVE);
new AR(path.toOSString()).dispose(); // check file type
try (AR ar = new AR(path.toOSString())) {
// create the object just to check file type
}
children = new ArrayList<>(5);
}

View file

@ -46,6 +46,7 @@
<li><a href="#terminal">TM Terminal has major changes to support new color and preference functionality.</a></li>
<li><a href="#casesensitive">Environment Variables are always case sensitive in CDT.</a></li>
<li><a href="#escaping">Environment variables no longer support \${ to avoid expanding.</a></li>
<li><a href="#autocloseable">The binary parsers are now implement Autocloseable</a></li>
</ol>
<p>
Planned Removals after June 2022
@ -303,6 +304,27 @@
<li>org.eclipse.cdt.core.envvar.IEnvironmentVariableManager.isVariableCaseSensitive() removed.</li></li>
</ul>
<h3>15. <a name="autocloseable">The binary parsers are now implement AutoCloseable</a></h3>
<p>
The binary parsers part of CDT core now implement the AutoCloseable interface and can be used
in try-with-resources blocks. See list below for all the classes that are now AutoCloseable. See <a
href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=553674" target="_blank">Bug 553674</a>.
</p>
<ul>
<li>org.eclipse.cdt.utils.coff.Exe</li>
<li>org.eclipse.cdt.utils.coff.PE</li>
<li>org.eclipse.cdt.utils.coff.PE64</li>
<li>org.eclipse.cdt.utils.coff.PEArchive</li>
<li>org.eclipse.cdt.utils.elf.Elf</li>
<li>org.eclipse.cdt.utils.elf.ElfHelper</li>
<li>org.eclipse.cdt.utils.AR</li>
<li>org.eclipse.cdt.utils.elf.AR</li>
<li>org.eclipse.cdt.utils.som.AR</li>
<li>org.eclipse.cdt.utils.xcoff.AR</li>
<li>org.eclipse.cdt.utils.macho.AR</li>
</ul>
<hr>
<h2>Future Deletions</h2>

View file

@ -151,17 +151,19 @@ public class PDABackend extends AbstractDsfService {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
Socket socket = new Socket("localhost", fRequestPort);
fRequestOutputStream = socket.getOutputStream();
fRequestInputStream = socket.getInputStream();
try (Socket socket = new Socket("localhost", fRequestPort)) {
fRequestOutputStream = socket.getOutputStream();
fRequestInputStream = socket.getInputStream();
}
// give interpreter a chance to open next socket
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
socket = new Socket("localhost", fEventPort);
fEventInputStream = socket.getInputStream();
try (Socket socket = new Socket("localhost", fEventPort)) {
fEventInputStream = socket.getInputStream();
}
} catch (UnknownHostException e) {
rm.setStatus(new Status(IStatus.ERROR, PDAPlugin.PLUGIN_ID, REQUEST_FAILED,

View file

@ -398,8 +398,7 @@ public class CEnvironmentTab extends CLaunchConfigurationTab {
}
//Iterate through each key/value property we discover
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line, key, value;
while ((line = reader.readLine()) != null) {