Skip to content

Commit

Permalink
rms: Account for cases where apps want to add zero-length data
Browse files Browse the repository at this point in the history
Apotheosize (240x320, md5: 190c1d339a45bc80526cb694035b7030) fails
to save in all circumstances if it fails to set up its initial
record tables on boot, which are all zero-length. Previously FreeJ2ME
would throw a RecordStoreException in those cases, but as per the
java docs, ME apps are allowed to save zero-length data.
  • Loading branch information
AShiningRay committed Sep 16, 2024
1 parent 215b037 commit 61f6e1c
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions src/javax/microedition/rms/RecordStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,31 +194,35 @@ private void setUInt16(byte[] data, int offset, int val)
data[offset+1] = (byte)((val) & 0xFF);
}

public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreException
public int addRecord(byte[] data, int offset, int numBytes) throws RecordStoreNotOpenException, RecordStoreException, RecordStoreFullException
{
//System.out.println("> Add Record "+nextid+ " to "+name);
try
{
byte[] rec = Arrays.copyOfRange(data, offset, offset+numBytes);

byte[] rec = new byte[]{};

// Only try to copy data if there's data to begin with, as some apps may try to store a record with zero-length data
if(data != null && data.length != 0)
{
if(offset < 0 || numBytes < 0 || offset + numBytes > data.length) { throw new ArrayIndexOutOfBoundsException("Tried to access invalid record data position"); }
rec = Arrays.copyOfRange(data, offset, offset+numBytes);
}
else { records.addElement(rec); } // offset and numBytes aren't even taken into account in this case, since the data will have zero-length anyway.

records.addElement(rec);

lastModified = nextid;
nextid++;
version++;

save();

for(int i=0; i<listeners.size(); i++)
{
listeners.get(i).recordAdded(this, lastModified);
}
for(int i=0; i<listeners.size(); i++) { listeners.get(i).recordAdded(this, lastModified); }

return lastModified;
}
catch (Exception e)
{
//System.out.println("> Add Record Failed");
throw(new RecordStoreException("Can't Add RMS Record"));
}
catch (Exception e) { throw(new RecordStoreException("Can't Add RMS Record: " + e.getMessage())); }
}

public void addRecordListener(RecordListener listener)
Expand Down Expand Up @@ -407,7 +411,15 @@ public void setRecord(int recordId, byte[] newData, int offset, int numBytes) th
}
try
{
byte[] rec = Arrays.copyOfRange(newData, offset, offset+numBytes);
byte[] rec = new byte[]{};
// As for addRecord, only try to copy data if there's data to begin with
if(newData != null && newData.length != 0)
{
if(offset < 0 || numBytes < 0 || offset + numBytes > newData.length) { throw new ArrayIndexOutOfBoundsException("Tried to access invalid record data position"); }

rec = Arrays.copyOfRange(newData, offset, offset+numBytes);
}

records.set(recordId, rec);
}
catch (Exception e)
Expand Down

0 comments on commit 61f6e1c

Please sign in to comment.