Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

#Bug 58062 Close IO Streams in finally statement #2

Open
wants to merge 1 commit into
base: BRANCH_1_3_ABANDONED
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions examples/tiny-webapp/Hello/src/java/wombat/HelloServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
String name = request.getParameter("name");

response.setContentType("text/html");
PrintWriter out = response.getWriter();

logger.info("About to say hello to "+name);

out.println("<HTML><BODY>");
out.println("<H2> Hello there " + name + ". How are you today?</H2>");
out.println("</BODY></HTML>");
out.close();
try{
PrintWriter out = response.getWriter();

logger.info("About to say hello to "+name);

out.println("<HTML><BODY>");
out.println("<H2> Hello there " + name + ". How are you today?</H2>");
out.println("</BODY></HTML>");
}finally{
out.close();
}
}
}
17 changes: 10 additions & 7 deletions examples/tiny-webapp/Tata/src/java/wombat/TataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ public void doPost(HttpServletRequest request, HttpServletResponse response)
String name = request.getParameter("name");

response.setContentType("text/html");
PrintWriter out = response.getWriter();

logger.info("About to say Tata to "+name);
try{
PrintWriter out = response.getWriter();

logger.info("About to say Tata to "+name);

out.println("<HTML><BODY>");
out.println("<H2>Many tata's to you " + name + ". How are you today?</H2>");
out.println("</BODY></HTML>");
out.close();
out.println("<HTML><BODY>");
out.println("<H2>Many tata's to you " + name + ". How are you today?</H2>");
out.println("</BODY></HTML>");
}finally{
out.close();
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/apache/log4j/helpers/SyslogWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,9 @@ public void close() {
flush();
} catch (IOException e) {
// should throw it ... can't change method sig. though
}finally{
ds.close();
}
ds.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,17 @@ public void begin(

try {
FileInputStream istream = new FileInputStream(fileName);
props.load(istream);
istream.close();
props.load(istream);
setProperties(ec, props);
} catch (IOException e) {
String errMsg = "Could not read properties file [" + fileName + "].";
getLogger().error(errMsg, e);
ec.addError(new ErrorItem(INVALID_ATTRIBUTES, e));
getLogger().error("Ignoring configuration file [" + fileName + "].");

}
}finally{
istream.close();
}
} else if (
!(Option.isEmpty(name) || Option.isEmpty(value))
&& Option.isEmpty(fileName)) {
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/apache/log4j/net/JMSReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,13 @@ public void activateOptions() {
if (jndiPath == null || jndiPath.equals("")) {
ctx = new InitialContext();
} else {
FileInputStream is = new FileInputStream(jndiPath);
Properties p = new Properties();
p.load(is);
is.close();
try{
FileInputStream is = new FileInputStream(jndiPath);
Properties p = new Properties();
p.load(is);
}finally{
is.close();
}
ctx = new InitialContext(p);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ public static boolean execute(
GZIPOutputStream gzos = new GZIPOutputStream(fos);
byte[] inbuf = new byte[8102];
int n;

while ((n = fis.read(inbuf)) != -1) {
gzos.write(inbuf, 0, n);
try{
while ((n = fis.read(inbuf)) != -1) {
gzos.write(inbuf, 0, n);
}
}finally{
gzos.close();
fis.close();
}

gzos.close();
fis.close();

if (deleteSource) {
if (!source.delete() && (logger != null)) {
logger.info("Unable to delete {}.", source.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,19 @@ public static boolean execute(
ZipOutputStream zos = new ZipOutputStream(fos);

ZipEntry zipEntry = new ZipEntry(source.getName());
zos.putNextEntry(zipEntry);

byte[] inbuf = new byte[8102];
int n;

while ((n = fis.read(inbuf)) != -1) {
zos.write(inbuf, 0, n);
try{
zos.putNextEntry(zipEntry);

byte[] inbuf = new byte[8102];
int n;

while ((n = fis.read(inbuf)) != -1) {
zos.write(inbuf, 0, n);
}
}finally{
zos.close();
fis.close();
}

zos.close();
fis.close();

if (deleteSource) {
if (!source.delete() && (logger != null)) {
logger.info("Unable to delete {}.", source.toString());
Expand Down
27 changes: 16 additions & 11 deletions tests/src/java/org/apache/log4j/AbstractAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,26 @@ public void activateOptions() {}

public void testNewAppender() {
// new appenders whould be inactive
AppenderSkeleton appender = getAppender();
assertFalse( appender.isActive());
assertFalse(appender.isClosed());

appender.close();
try{
AppenderSkeleton appender = getAppender();
assertFalse( appender.isActive());
assertFalse(appender.isClosed());
}finally{
appender.close();
}
assertTrue(appender.isClosed());
}

public void testConfiguredAppender() {
AppenderSkeleton appender = getConfiguredAppender();
appender.activateOptions();
assertTrue(appender.isActive());
assertFalse(appender.isClosed());

appender.close();
try{
AppenderSkeleton appender = getConfiguredAppender();
appender.activateOptions();
assertTrue(appender.isActive());
assertFalse(appender.isClosed());
}finally{
appender.close();
}

assertTrue(appender.isClosed());
}

Expand Down
11 changes: 7 additions & 4 deletions tests/src/java/org/apache/log4j/PatternLayoutTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,13 @@ public void test16() throws Exception {
PropertyConfigurator.configure("input/pattern/patternLayout16.properties");
common();
final long end = new Date().getTime();
FileReader reader = new FileReader("output/patternLayout16.log");
char chars[] = new char[50];
reader.read(chars, 0, chars.length);
reader.close();
try{
FileReader reader = new FileReader("output/patternLayout16.log");
char chars[] = new char[50];
reader.read(chars, 0, chars.length);
}finally{
reader.close();
}
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("GMT+0"));
String utcStr = new String(chars, 0, 19);
Expand Down
27 changes: 18 additions & 9 deletions tests/src/java/org/apache/log4j/PropertyConfiguratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public PropertyConfiguratorTest(final String testName) {
*/
public void testBadUnicodeEscape() throws IOException {
String fileName = "output/badescape.properties";
FileWriter writer = new FileWriter(fileName);
writer.write("log4j.rootLogger=\\uXX41");
writer.close();
try{
FileWriter writer = new FileWriter(fileName);
writer.write("log4j.rootLogger=\\uXX41");
}finally{
writer.close();
}
PropertyConfigurator.configure(fileName);
File file = new File(fileName);
assertTrue(file.delete()) ;
Expand All @@ -53,9 +56,12 @@ public void testBadUnicodeEscape() throws IOException {
*/
public void testURL() throws IOException {
File file = new File("output/unclosed.properties");
FileWriter writer = new FileWriter(file);
writer.write("log4j.rootLogger=debug");
writer.close();
try{
FileWriter writer = new FileWriter(file);
writer.write("log4j.rootLogger=debug");
}finally{
writer.close();
}
URL url = file.toURL();
PropertyConfigurator.configure(url);
assertTrue(file.delete());
Expand All @@ -70,9 +76,12 @@ public void testURL() throws IOException {
*/
public void testURLBadEscape() throws IOException {
File file = new File("output/urlbadescape.properties");
FileWriter writer = new FileWriter(file);
writer.write("log4j.rootLogger=\\uXX41");
writer.close();
try{
FileWriter writer = new FileWriter(file);
writer.write("log4j.rootLogger=\\uXX41");
}finally{
writer.close();
}
URL url = file.toURL();
PropertyConfigurator.configure(url);
assertTrue(file.delete());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,12 @@ public void testBasic() throws Exception {
}

private void readHI() throws Exception {
BufferedReader r = new BufferedReader(new FileReader(f1));
assertEquals("DEBUG - HI", r.readLine());
r.close();
try{
BufferedReader r = new BufferedReader(new FileReader(f1));
assertEquals("DEBUG - HI", r.readLine());
}finally{
r.close();
}
}

/**
Expand Down
21 changes: 12 additions & 9 deletions tests/src/java/org/apache/log4j/html/Loop.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ public static void main(String[] args) {
HTMLLayout htmlLayout = new HTMLLayout("%sn%date%thread%level%logger%m");
//htmlLayout.setInternalCSS(true);
//EnhancedHTMLLayout htmlLayout = new EnhancedHTMLLayout("%relative%thread%level%logger%m");
FileAppender appender = new FileAppender();
appender.setFile("toto.html");
appender.setAppend(false);
appender.setLayout(htmlLayout);
appender.activateOptions();
Logger root = Logger.getRootLogger();
root.addAppender(appender);
try{
FileAppender appender = new FileAppender();
appender.setFile("toto.html");
appender.setAppend(false);
appender.setLayout(htmlLayout);
appender.activateOptions();
Logger root = Logger.getRootLogger();
root.addAppender(appender);

loop(200);
appender.close();
loop(200);
}finally{
appender.close();
}
}

static void loop(int len) {
Expand Down
32 changes: 18 additions & 14 deletions tests/src/java/org/apache/log4j/net/TelnetAppenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,32 @@ public void run() {
if (c == -1)
break;
bo.write(c);
}
s.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
s.close();
}
}
}

public void testIt() throws Exception {
int oldActive = Thread.activeCount();
TelnetAppender ta = new TelnetAppender();
ta.setName("ta");
ta.setPort(port);
ta.setLayout(new PatternLayout("%p - %m"));
ta.activateOptions();
Logger l = Logger.getLogger("x");
l.addAppender(ta);
Thread t = new ReadThread();
t.start();
Thread.sleep(200);
l.info("hi");
ta.close();
try{
TelnetAppender ta = new TelnetAppender();
ta.setName("ta");
ta.setPort(port);
ta.setLayout(new PatternLayout("%p - %m"));
ta.activateOptions();
Logger l = Logger.getLogger("x");
l.addAppender(ta);
Thread t = new ReadThread();
t.start();
Thread.sleep(200);
l.info("hi");
}finally{
ta.close();
}
Thread.sleep(200);
t.interrupt();
t.join();
Expand Down
11 changes: 7 additions & 4 deletions tests/src/java/org/apache/log4j/rolling/FileOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
*/
public class FileOpener {
public static void main(String[] args) throws Exception {
InputStream is = new FileInputStream("output/test.log");
is.read();
Thread.sleep(10000);
is.close();
try{
InputStream is = new FileInputStream("output/test.log");
is.read();
Thread.sleep(10000);
}finally{
is.close();
}
System.out.println("Exiting FileOpener");
}
}
Loading