• Bug
  • Status: Closed
  • Resolution: Fixed
  • drb
  • Reporter: sourceforgetracker
  • September 21, 2009
  • 0
  • Watchers: 0
  • September 22, 2009
  • September 22, 2009

Description

Hi got a possible bug in net.sf.ehcache.store.DiskStore.

The ObjectInputStream may be ‘null’ but the ‘FileInputStream ‘ not

original:

private synchronized void readIndex() throws IOException { ObjectInputStream objectInputStream = null; if (indexFile.exists()) { try { FileInputStream fin = new FileInputStream(indexFile); objectInputStream = new ObjectInputStream(fin); diskElements = (HashMap) objectInputStream.readObject(); freeSpace = (ArrayList) objectInputStream.readObject(); } catch (StreamCorruptedException e) { LOG.error(“Corrupt index file. Creating new index. “); createNewIndexFile(); } catch (IOException e) { LOG.error(“IOException reading index. Creating new index.”); createNewIndexFile(); } catch (ClassNotFoundException e) { LOG.error(“Class loading problem reading index. Creating new index.”, e); createNewIndexFile(); } finally { if (objectInputStream != null) { objectInputStream.close(); } //Zero out file. If there is a dirty shutdown, the file will be empty. createNewIndexFile(); } } else { createNewIndexFile(); } }

should probably look like this

private synchronized void readIndex() throws IOException \{
    ObjectInputStream objectInputStream = null;
    FileInputStream fin = null;
    if (indexFile.exists()) {
        try {
            fin = new FileInputStream(indexFile);
            objectInputStream = new ObjectInputStream(fin);
            diskElements = (HashMap) objectInputStream.readObject();
            freeSpace = (ArrayList) objectInputStream.readObject();
        } catch (StreamCorruptedException e) {
            LOG.error("Corrupt index file. Creating new index. ");
        } catch (IOException e) {
            LOG.error("IOException reading index. Creating new index.");
        } catch (ClassNotFoundException e) {
            LOG.error("Class loading problem reading index. Creating new index.", e);
        } finally {
            if (objectInputStream != null) {
                objectInputStream.close();
            }
            else if(null != fin) {
                fin.close();
            }
            
            //Zero out file. If there is a dirty shutdown, the file will be empty.
            createNewIndexFile();
        }
    } else {
        createNewIndexFile();
    }
} Sourceforge Ticket ID: 1063908 - Opened By: nobody - 10 Nov 2004 15:58 UTC

Comments

Sourceforge Tracker 2009-09-21

Logged In: YES user_id=693320

Hi

I have added a check for fin and close that too if it is not null where OOS is null. I added a try/catch around the lot to make sure we always create the new index.

My index corruption test passes both before and after the change. Can you give me a test case that will fail on the old code, or failing that a scenario where it can happen so I can add a test for it. Thanks Comment by: gregluck - 23 Nov 2004 05:14 UTC

Fiona OShea 2009-09-22

Re-opening so that I can properly close out these issues and have correct Resolution status in Jira