Program: 15800
Project: 15800
Buggy source:
/** 
 * Write a value.
 * @param v the value
 */
public void writeValue(Value v) throws IOException {
  int type=v.getType();
  writeInt(type);
switch (type) {
case Value.NULL:
    break;
case Value.BYTES:
case Value.JAVA_OBJECT:
  writeBytes(v.getBytesNoCopy());
break;
case Value.UUID:
{
ValueUuid uuid=(ValueUuid)v;
writeLong(uuid.getHigh());
writeLong(uuid.getLow());
break;
}
case Value.BOOLEAN:
writeBoolean(v.getBoolean().booleanValue());
break;
case Value.BYTE:
writeByte(v.getByte());
break;
case Value.TIME:
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
writeLong(((ValueTime)v).getNanos());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
writeLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
}
 else {
writeLong(v.getTime().getTime());
}
break;
case Value.DATE:
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
writeLong(((ValueDate)v).getDateValue());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
writeLong(DateTimeUtils.getTimeLocalWithoutDst(v.getDate()));
}
 else {
writeLong(v.getDate().getTime());
}
break;
case Value.TIMESTAMP:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
ValueTimestamp ts=(ValueTimestamp)v;
writeLong(ts.getDateValue());
writeLong(ts.getTimeNanos());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
Timestamp ts=v.getTimestamp();
writeLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
writeInt(ts.getNanos());
}
 else {
Timestamp ts=v.getTimestamp();
writeLong(ts.getTime());
writeInt(ts.getNanos());
}
break;
}
case Value.DECIMAL:
writeString(v.getString());
break;
case Value.DOUBLE:
writeDouble(v.getDouble());
break;
case Value.FLOAT:
writeFloat(v.getFloat());
break;
case Value.INT:
writeInt(v.getInt());
break;
case Value.LONG:
writeLong(v.getLong());
break;
case Value.SHORT:
writeInt(v.getShort());
break;
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
writeString(v.getString());
break;
case Value.BLOB:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) {
ValueLobDb lob=(ValueLobDb)v;
if (lob.isStored()) {
writeLong(-1);
writeInt(lob.getTableId());
writeLong(lob.getLobId());
if (version >= Constants.TCP_PROTOCOL_VERSION_12) {
writeBytes(calculateLobMac(lob.getLobId()));
}
writeLong(lob.getPrecision());
break;
}
}
}
long length=v.getPrecision();
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length);
}
writeLong(length);
long written=IOUtils.copyAndCloseInput(v.getInputStream(),out);
if (written != length) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length + "String_Node_Str"+ written);
}
writeInt(LOB_MAGIC);
break;
}
case Value.CLOB:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) {
ValueLobDb lob=(ValueLobDb)v;
if (lob.isStored()) {
writeLong(-1);
writeInt(lob.getTableId());
writeLong(lob.getLobId());
if (version >= Constants.TCP_PROTOCOL_VERSION_12) {
writeBytes(calculateLobMac(lob.getLobId()));
}
writeLong(lob.getPrecision());
break;
}
}
}
long length=v.getPrecision();
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length);
}
writeLong(length);
Reader reader=v.getReader();
Data.copyString(reader,out);
writeInt(LOB_MAGIC);
break;
}
case Value.ARRAY:
{
ValueArray va=(ValueArray)v;
Value[] list=va.getList();
int len=list.length;
Class<?> componentType=va.getComponentType();
if (componentType == Object.class) {
writeInt(len);
}
 else {
writeInt(-(len + 1));
writeString(componentType.getName());
}
for (Value value : list) {
writeValue(value);
}
break;
}
case Value.RESULT_SET:
{
try {
ResultSet rs=((ValueResultSet)v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta=rs.getMetaData();
int columnCount=meta.getColumnCount();
writeInt(columnCount);
for (int i=0; i < columnCount; i++) {
writeString(meta.getColumnName(i + 1));
writeInt(meta.getColumnType(i + 1));
writeInt(meta.getPrecision(i + 1));
writeInt(meta.getScale(i + 1));
}
while (rs.next()) {
writeBoolean(true);
for (int i=0; i < columnCount; i++) {
int t=DataType.getValueTypeFromResultSet(meta,i + 1);
Value val=DataType.readValue(session,rs,i + 1,t);
writeValue(val);
}
}
writeBoolean(false);
rs.beforeFirst();
}
 catch (SQLException e) {
throw DbException.convertToIOException(e);
}
break;
}
case Value.GEOMETRY:
if (version >= Constants.TCP_PROTOCOL_VERSION_14) {
writeBytes(v.getBytesNoCopy());
}
 else {
writeString(v.getString());
}
break;
default :
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + type);
}
}

Fixed version (ground truth):
/** 
 * Write a value.
 * @param v the value
 */
public void writeValue(Value v) throws IOException {
  int type=v.getType();
  writeInt(type);
switch (type) {
case Value.NULL:
    break;
case Value.BYTES:
case Value.JAVA_OBJECT:
  writeBytes(v.getBytesNoCopy());
break;
case Value.UUID:
{
ValueUuid uuid=(ValueUuid)v;
writeLong(uuid.getHigh());
writeLong(uuid.getLow());
break;
}
case Value.BOOLEAN:
writeBoolean(v.getBoolean().booleanValue());
break;
case Value.BYTE:
writeByte(v.getByte());
break;
case Value.TIME:
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
writeLong(((ValueTime)v).getNanos());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
writeLong(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
}
 else {
writeLong(v.getTime().getTime());
}
break;
case Value.DATE:
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
writeLong(((ValueDate)v).getDateValue());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
writeLong(DateTimeUtils.getTimeLocalWithoutDst(v.getDate()));
}
 else {
writeLong(v.getDate().getTime());
}
break;
case Value.TIMESTAMP:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_9) {
ValueTimestamp ts=(ValueTimestamp)v;
writeLong(ts.getDateValue());
writeLong(ts.getTimeNanos());
}
 else if (version >= Constants.TCP_PROTOCOL_VERSION_7) {
Timestamp ts=v.getTimestamp();
writeLong(DateTimeUtils.getTimeLocalWithoutDst(ts));
writeInt(ts.getNanos() % 1000000);
}
 else {
Timestamp ts=v.getTimestamp();
writeLong(ts.getTime());
writeInt(ts.getNanos() % 1000000);
}
break;
}
case Value.DECIMAL:
writeString(v.getString());
break;
case Value.DOUBLE:
writeDouble(v.getDouble());
break;
case Value.FLOAT:
writeFloat(v.getFloat());
break;
case Value.INT:
writeInt(v.getInt());
break;
case Value.LONG:
writeLong(v.getLong());
break;
case Value.SHORT:
writeInt(v.getShort());
break;
case Value.STRING:
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
writeString(v.getString());
break;
case Value.BLOB:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) {
ValueLobDb lob=(ValueLobDb)v;
if (lob.isStored()) {
writeLong(-1);
writeInt(lob.getTableId());
writeLong(lob.getLobId());
if (version >= Constants.TCP_PROTOCOL_VERSION_12) {
writeBytes(calculateLobMac(lob.getLobId()));
}
writeLong(lob.getPrecision());
break;
}
}
}
long length=v.getPrecision();
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length);
}
writeLong(length);
long written=IOUtils.copyAndCloseInput(v.getInputStream(),out);
if (written != length) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length + "String_Node_Str"+ written);
}
writeInt(LOB_MAGIC);
break;
}
case Value.CLOB:
{
if (version >= Constants.TCP_PROTOCOL_VERSION_11) {
if (v instanceof ValueLobDb) {
ValueLobDb lob=(ValueLobDb)v;
if (lob.isStored()) {
writeLong(-1);
writeInt(lob.getTableId());
writeLong(lob.getLobId());
if (version >= Constants.TCP_PROTOCOL_VERSION_12) {
writeBytes(calculateLobMac(lob.getLobId()));
}
writeLong(lob.getPrecision());
break;
}
}
}
long length=v.getPrecision();
if (length < 0) {
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + length);
}
writeLong(length);
Reader reader=v.getReader();
Data.copyString(reader,out);
writeInt(LOB_MAGIC);
break;
}
case Value.ARRAY:
{
ValueArray va=(ValueArray)v;
Value[] list=va.getList();
int len=list.length;
Class<?> componentType=va.getComponentType();
if (componentType == Object.class) {
writeInt(len);
}
 else {
writeInt(-(len + 1));
writeString(componentType.getName());
}
for (Value value : list) {
writeValue(value);
}
break;
}
case Value.RESULT_SET:
{
try {
ResultSet rs=((ValueResultSet)v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta=rs.getMetaData();
int columnCount=meta.getColumnCount();
writeInt(columnCount);
for (int i=0; i < columnCount; i++) {
writeString(meta.getColumnName(i + 1));
writeInt(meta.getColumnType(i + 1));
writeInt(meta.getPrecision(i + 1));
writeInt(meta.getScale(i + 1));
}
while (rs.next()) {
writeBoolean(true);
for (int i=0; i < columnCount; i++) {
int t=DataType.getValueTypeFromResultSet(meta,i + 1);
Value val=DataType.readValue(session,rs,i + 1,t);
writeValue(val);
}
}
writeBoolean(false);
rs.beforeFirst();
}
 catch (SQLException e) {
throw DbException.convertToIOException(e);
}
break;
}
case Value.GEOMETRY:
if (version >= Constants.TCP_PROTOCOL_VERSION_14) {
writeBytes(v.getBytesNoCopy());
}
 else {
writeString(v.getString());
}
break;
default :
throw DbException.get(ErrorCode.CONNECTION_BROKEN_1,"String_Node_Str" + type);
}
}