Permalink
Browse files

fix: wrong data from Blob/Clob when mark/reset is used (#971)

BlobInputStream#reset() did not invalidate read buffer, thus it resulted in reading wrong data.
  • Loading branch information...
sfeigl authored and vlsi committed Mar 11, 2018
1 parent 44bb7f8 commit 61e1c300fb52237b05b7649d61603dd339fbdd9b
@@ -7,6 +7,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
### Fixed
- Avoid failure for `insert ... on conflict...update` for `reWriteBatchedInserts=true` case [PR 1130](https://github.com/pgjdbc/pgjdbc/pull/1130)
- fix: allowEncodingChanges should allow set client_encoding=... [PR 1125](https://github.com/pgjdbc/pgjdbc/pull/1125)
- Wrong data from Blob/Clob when mark/reset is used [PR 971](https://github.com/pgjdbc/pgjdbc/pull/971)
## [42.2.1] (2018-01-25)
### Known issues
@@ -41,7 +41,7 @@
/**
* The mark position
*/
private int mpos = 0;
private long mpos = 0;
/**
* The limit
@@ -156,12 +156,7 @@ public void close() throws IOException {
* @see java.io.InputStream#reset()
*/
public synchronized void mark(int readlimit) {
try {
mpos = lo.tell();
} catch (SQLException se) {
// Can't throw this because mark API doesn't allow it.
// throw new IOException(se.toString());
}
mpos = apos;
}
/**
@@ -174,7 +169,13 @@ public synchronized void mark(int readlimit) {
public synchronized void reset() throws IOException {
checkClosed();
try {
lo.seek(mpos);
if (mpos <= Integer.MAX_VALUE) {
lo.seek((int)mpos);
} else {
lo.seek64(mpos, LargeObject.SEEK_SET);
}
buffer = null;
apos = mpos;
} catch (SQLException se) {
throw new IOException(se.toString());
}
@@ -155,6 +155,31 @@ public void testUploadBlob_NATIVE() throws Exception {
assertTrue(compareBlobs());
}
@Test
public void testMarkResetStream() throws Exception {
assertTrue(uploadFile("/test-file.xml", NATIVE_STREAM) > 0);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT lo FROM testblob");
assertTrue(rs.next());
LargeObjectManager lom = ((org.postgresql.PGConnection) con).getLargeObjectAPI();
long oid = rs.getLong(1);
LargeObject blob = lom.open(oid);
InputStream bis = blob.getInputStream();
assertEquals('<', bis.read());
bis.mark(4);
assertEquals('?', bis.read());
assertEquals('x', bis.read());
assertEquals('m', bis.read());
assertEquals('l', bis.read());
bis.reset();
assertEquals('?', bis.read());
}
@Test
public void testGetBytesOffset() throws Exception {
assertTrue(uploadFile("/test-file.xml", NATIVE_STREAM) > 0);

0 comments on commit 61e1c30

Please sign in to comment.