Bug #6198 Buffer.readString(String encoding) does not return the correct value
Submitted: 21 Oct 2004 14:26 Modified: 21 Oct 2004 19:44
Reporter: Henning Moll Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version:3.0.15 OS:Any (all)
Assigned to: Mark Matthews CPU Architecture:Any

[21 Oct 2004 14:26] Henning Moll
Description:
There's a simple, but effective bug in the function 

final String readString(String encoding) throws SQLException

of class

com.mysql.jdbc.Buffer

The function will not return the correct buffer content, but a string behind(!)  the end_of_content pointer. That area is filled with random stuff from prior usages of that buffer.
You can easily see the bug if you compare this function to function 

final String readString() throws SQLException 

(without parameter) of the same class, because that function is correct.

How to repeat:
I wonder how this bug i still alive. Maybe it's due to the fact, that it only occours, if i do an 

LOAD DATA LOCAL INFILE <filename> INTO TABLE <tablename>

But this bug is trivial, so look at the suggested fix. It's self explanatory.

Suggested fix:
*** mysql-connector-java-3.0.15-ga/com/mysql/jdbc/Buffer.java   Sat Sep  4 02:14:52 2004
--- mysql-connector-java-3.0.15-ga-fixed/com/mysql/jdbc/Buffer.java     Thu Oct 21 16:21:45 2004
***************
*** 370,383 ****
                        i++;
                }
  
!               this.position += (len + 1); // update cursor
                
                try {
!                       return new String(this.byteBuffer, this.position, len, encoding);
                        
                } catch (UnsupportedEncodingException uEE) {
                        throw new SQLException("Unsupported character encoding '" + encoding + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
                }
      }
  
      final int readnBytes() {
--- 370,390 ----
                        i++;
                }
  
! 
!               String tmp;
                
                try {
!                       tmp = new String(this.byteBuffer, this.position, len, encoding);
                        
                } catch (UnsupportedEncodingException uEE) {
                        throw new SQLException("Unsupported character encoding '" + encoding + "'", SQLError.SQL_STATE_ILLEGAL_ARGUMENT);
                }
+               finally
+               {
+                       this.position += (len + 1); // update cursor. *hgmoll* That has to be done AFTER creating the String object as it is done in readString()
+               }
+ 
+               return tmp;
      }
  
      final int readnBytes() {
[21 Oct 2004 19:44] Mark Matthews
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html