Skip site navigation (1) Skip section navigation (2)

Bug #558: JDBC InputStream.read() from oid returns incorrect values for negative bytes and really messes up -1

From: pgsql-bugs(at)postgresql(dot)org
To: pgsql-bugs(at)postgresql(dot)org
Subject: Bug #558: JDBC InputStream.read() from oid returns incorrect values for negative bytes and really messes up -1
Date: 2002-01-09 14:52:14
Message-ID: 200201091452.g09EqE405525@postgresql.org (view raw or flat)
Thread:
Lists: pgsql-bugs
James Lynn (JamesLynn(at)mail(dot)com) reports a bug with a severity of 2
The lower the number the more severe it is.

Short Description
JDBC InputStream.read() from oid returns incorrect values for negative bytes and really messes up -1

Long Description
Using: PostgresSQL 7.1, jdbc7.1-1.2.jar

After inserting a large object (oid) and trying to select it back out, the InputStream returns incorrect values when calling read() if the input contained any negative bytes. Example: 0xFE should return 254 but incorrectly returns -2. The worst case is when you have 0xFF in the large object: since read() returns -1 rather than 255 and -1 is the signal that you have reached the end of stream so you can never tell if you have a -1.

There is also a problem with read(byte[],int,int), where the 0xFF just doesn't appear in the data. This may or may not be related.

To set up the example you must use the following create statement:
create table x (f oid);

The example code is Bug.java.

Sample Code
import java.sql.*;
import java.io.*;
import java.util.*;

public class Bug
{
 public static void main(String[] args) throws Exception
 {
 byte[] data={0,-2,-1,0};

 // Test way
  {
  OutputStream out=new FileOutputStream("bug.test");
  out.write(data);
  out.close();

  InputStream in=new FileInputStream("bug.test");
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  int ch;
  System.out.print("Correct:   ");
  while ((ch=in.read())!=-1) { System.out.print(ch);System.out.print(','); baos.write(ch);}
  System.out.print(ch);System.out.print(';'); 
  System.out.println(hex(baos.toByteArray()));
  }

 Connection c=null;
 Connection c2=null;
 Class.forName("org.postgresql.Driver");
 String url="jdbc:postgresql://dev002.oakscape.com/shipvis";
 String username="shipvis";
 String password="shipvis";
 c=DriverManager.getConnection(url, username, password);
 c.setAutoCommit(false);
 
 c.prepareStatement("delete from x").execute();
 c.commit();
 PreparedStatement insert=c.prepareStatement("insert into x (f) values (?)");

 insert.setBinaryStream(1,new ByteArrayInputStream(data),data.length);

 insert.execute();
 c.commit();

 ResultSet rs=c.prepareStatement("select * from x").executeQuery();
 rs.next();

 // One way
  {
  InputStream in=rs.getBinaryStream("f");
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  int ch;
  System.out.print("Incorrect: ");
  while ((ch=in.read())!=-1) { System.out.print(ch);System.out.print(','); baos.write(ch);}
  System.out.print(ch);System.out.print(';'); 
  System.out.println(hex(baos.toByteArray()));
  }


 // Another way
  {
  InputStream in=rs.getBinaryStream("f");
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  byte[] ch=new byte[4];
  int n;
  System.out.print("Odd:       ");
  while ((n=in.read(ch,0,ch.length))!=-1) baos.write(ch,0,n);
  System.out.println(hex(baos.toByteArray()));
  }

 // One way
  {
  InputStream in=rs.getBinaryStream("f");
  ByteArrayOutputStream baos=new ByteArrayOutputStream();
  int ch;
  System.out.print("Know len:  ");
  for (int i=0;i<4;i++) { ch=in.read(); System.out.print(ch);System.out.print(','); baos.write(ch);}
  System.out.print(';'); 
  System.out.println(hex(baos.toByteArray()));
  }

 c.close();
 }

 static String hex(byte[] data)
 {
 StringBuffer sb=new StringBuffer();
 for (int i=0;i<data.length;i++)
  {
  sb.append(hex((byte)(data[i]>>4))).append(hex(data[i]));
  }
 return sb.toString();
 }
 static char hex(byte data)
 {
 data&=0xF;
 if (data<10) return (char)('0'+data);
 return (char)('A'+data-10);
 }
}


No file was uploaded with this report


pgsql-bugs by date

Next:From: Tim NelsonDate: 2002-01-09 16:16:43
Subject: Re: ecpg "aborts" on structure reference
Previous:From: Tim NelsonDate: 2002-01-09 13:25:54
Subject: ecpg "aborts" on structure reference

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group