Skip to content

Commit fda6553

Browse files
committed
[bugfix] Do not overflow integer size bound of Journal file
1 parent 5ac7694 commit fda6553

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/org/exist/storage/journal/Journal.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ public final class Journal {
163163
*/
164164
private int currentFile = 0;
165165

166-
/**
167-
* used to keep track of the current position in the file
168-
*/
169-
private int inFilePos = 0;
170-
171166
/**
172167
* temp buffer
173168
*/
@@ -291,8 +286,18 @@ public synchronized void writeToLog(final Loggable entry) throws JournalExceptio
291286
if (required > currentBuffer.remaining()) {
292287
flushToLog(false);
293288
}
294-
currentLsn = Lsn.create(currentFile, inFilePos + currentBuffer.position() + 1);
289+
290+
try {
291+
final long offset = channel.position();
292+
if (offset > Integer.MAX_VALUE) {
293+
throw new JournalException("Journal can only write log files of less that 2GB");
294+
}
295+
currentLsn = Lsn.create(currentFile, ((int)(channel.position() &0x7FFFFFFF)) + currentBuffer.position() + 1);
296+
} catch (final IOException e) {
297+
throw new JournalException("Unable to create LSN for: " + entry.dump());
298+
}
295299
entry.setLsn(currentLsn);
300+
296301
try {
297302
currentBuffer.put(entry.getLogType());
298303
currentBuffer.putLong(entry.getTransactionId());
@@ -365,7 +370,6 @@ private void flushBuffer() {
365370
channel.write(currentBuffer);
366371
}
367372

368-
inFilePos += size;
369373
lastLsnWritten = currentLsn;
370374
}
371375
} catch (final IOException e) {
@@ -479,7 +483,6 @@ public void switchFiles() throws LogException {
479483
throw new LogException("Failed to open new journal: " + file.toAbsolutePath().toString(), e);
480484
}
481485
}
482-
inFilePos = 0;
483486
}
484487

485488
/**

src/org/exist/storage/journal/JournalReader.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ Loggable previousEntry() throws LogException {
113113
// go back two bytes and read the back-link of the last entry
114114
fc.position(fc.position() - LOG_ENTRY_BACK_LINK_LEN);
115115
header.clear().limit(LOG_ENTRY_BACK_LINK_LEN);
116-
final int bytes = fc.read(header);
117-
if (bytes < LOG_ENTRY_BACK_LINK_LEN) {
116+
final int read = fc.read(header);
117+
if (read != LOG_ENTRY_BACK_LINK_LEN) {
118118
throw new LogException("Unable to read journal entry back-link!");
119119
}
120120
header.flip();
@@ -159,17 +159,21 @@ Loggable lastEntry() throws LogException {
159159
private @Nullable
160160
Loggable readEntry() throws LogException {
161161
try {
162-
final long lsn = Lsn.create(fileNumber, (int) fc.position() + 1);
162+
final long offset = fc.position();
163+
if (offset > Integer.MAX_VALUE) {
164+
throw new LogException("Journal can only read log files of less that 2GB");
165+
}
166+
final long lsn = Lsn.create(fileNumber, ((int)(offset & 0x7FFFFFFF)) + 1);
163167

164168
// read the entry header
165169
header.clear();
166-
int bytes = fc.read(header);
167-
if (bytes <= 0) {
170+
int read = fc.read(header);
171+
if (read <= 0) {
168172
return null;
169173
}
170-
if (bytes < LOG_ENTRY_HEADER_LEN) {
174+
if (read != LOG_ENTRY_HEADER_LEN) {
171175
throw new LogException("Incomplete journal entry header found, expected "
172-
+ LOG_ENTRY_HEADER_LEN + " bytes, but found " + bytes + " bytes");
176+
+ LOG_ENTRY_HEADER_LEN + " bytes, but found " + read + " bytes");
173177
}
174178
header.flip();
175179

@@ -192,8 +196,8 @@ Loggable readEntry() throws LogException {
192196
payload = ByteBuffer.allocate(size + LOG_ENTRY_BACK_LINK_LEN);
193197
}
194198
payload.clear().limit(size + LOG_ENTRY_BACK_LINK_LEN);
195-
bytes = fc.read(payload);
196-
if (bytes < size + LOG_ENTRY_BACK_LINK_LEN) {
199+
read = fc.read(payload);
200+
if (read < size + LOG_ENTRY_BACK_LINK_LEN) {
197201
throw new LogException("Incomplete log entry found!");
198202
}
199203
payload.flip();

0 commit comments

Comments
 (0)