Program: 15802
Project: 15802
Buggy source:
/** 
 * Calculate the number of bytes required to encode the given value.
 * @param v the value
 * @param handler the data handler for lobs
 * @return the number of bytes required to store this value
 */
public static int getValueLen(Value v,DataHandler handler){
  if (v == ValueNull.INSTANCE) {
    return 1;
  }
switch (v.getType()) {
case Value.BOOLEAN:
    return 1;
case Value.BYTE:
  return 2;
case Value.SHORT:
return 3;
case Value.INT:
{
int x=v.getInt();
if (x < 0) {
  return 1 + getVarIntLen(-x);
}
 else if (x < 16) {
  return 1;
}
 else {
  return 1 + getVarIntLen(x);
}
}
case Value.LONG:
{
long x=v.getLong();
if (x < 0) {
return 1 + getVarLongLen(-x);
}
 else if (x < 8) {
return 1;
}
 else {
return 1 + getVarLongLen(x);
}
}
case Value.DOUBLE:
{
double x=v.getDouble();
if (x == 1.0d) {
return 1;
}
long d=Double.doubleToLongBits(x);
if (d == ValueDouble.ZERO_BITS) {
return 1;
}
return 1 + getVarLongLen(Long.reverse(d));
}
case Value.FLOAT:
{
float x=v.getFloat();
if (x == 1.0f) {
return 1;
}
int f=Float.floatToIntBits(x);
if (f == ValueFloat.ZERO_BITS) {
return 1;
}
return 1 + getVarIntLen(Integer.reverse(f));
}
case Value.STRING:
{
String s=v.getString();
int len=s.length();
if (len < 32) {
return 1 + getStringWithoutLengthLen(s,len);
}
return 1 + getStringLen(s);
}
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
return 1 + getStringLen(v.getString());
case Value.DECIMAL:
{
BigDecimal x=v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
return 1;
}
 else if (BigDecimal.ONE.equals(x)) {
return 1;
}
int scale=x.scale();
BigInteger b=x.unscaledValue();
int bits=b.bitLength();
if (bits <= 63) {
if (scale == 0) {
return 1 + getVarLongLen(b.longValue());
}
return 1 + getVarIntLen(scale) + getVarLongLen(b.longValue());
}
byte[] bytes=b.toByteArray();
return 1 + getVarIntLen(scale) + getVarIntLen(bytes.length)+ bytes.length;
}
case Value.TIME:
if (SysProperties.STORE_LOCAL_TIME) {
long nanos=((ValueTime)v).getNanos();
long millis=nanos / 1000000;
nanos-=millis * 1000000;
return 1 + getVarLongLen(millis) + getVarLongLen(nanos);
}
return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
case Value.DATE:
{
if (SysProperties.STORE_LOCAL_TIME) {
long dateValue=((ValueDate)v).getDateValue();
return 1 + getVarLongLen(dateValue);
}
long x=DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
return 1 + getVarLongLen(x / MILLIS_PER_MINUTE);
}
case Value.TIMESTAMP:
{
if (SysProperties.STORE_LOCAL_TIME) {
ValueTimestamp ts=(ValueTimestamp)v;
long dateValue=ts.getDateValue();
long nanos=ts.getTimeNanos();
long millis=nanos / 1000000;
nanos-=millis * 1000000;
return 1 + getVarLongLen(dateValue) + getVarLongLen(millis)+ getVarLongLen(nanos);
}
Timestamp ts=v.getTimestamp();
return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(ts)) + getVarIntLen(ts.getNanos());
}
case Value.GEOMETRY:
case Value.JAVA_OBJECT:
{
byte[] b=v.getBytesNoCopy();
return 1 + getVarIntLen(b.length) + b.length;
}
case Value.BYTES:
{
byte[] b=v.getBytesNoCopy();
int len=b.length;
if (len < 32) {
return 1 + b.length;
}
return 1 + getVarIntLen(b.length) + b.length;
}
case Value.UUID:
return 1 + LENGTH_LONG + LENGTH_LONG;
case Value.BLOB:
case Value.CLOB:
{
int len=1;
if (v instanceof ValueLob) {
ValueLob lob=(ValueLob)v;
lob.convertToFileIfRequired(handler);
byte[] small=lob.getSmall();
if (small == null) {
int t=-1;
if (!lob.isLinked()) {
t=-2;
}
len+=getVarIntLen(t);
len+=getVarIntLen(lob.getTableId());
len+=getVarIntLen(lob.getObjectId());
len+=getVarLongLen(lob.getPrecision());
len+=1;
if (t == -2) {
len+=getStringLen(lob.getFileName());
}
}
 else {
len+=getVarIntLen(small.length);
len+=small.length;
}
}
 else {
ValueLobDb lob=(ValueLobDb)v;
byte[] small=lob.getSmall();
if (small == null) {
len+=getVarIntLen(-3);
len+=getVarIntLen(lob.getTableId());
len+=getVarLongLen(lob.getLobId());
len+=getVarLongLen(lob.getPrecision());
}
 else {
len+=getVarIntLen(small.length);
len+=small.length;
}
}
return len;
}
case Value.ARRAY:
{
Value[] list=((ValueArray)v).getList();
int len=1 + getVarIntLen(list.length);
for (Value x : list) {
len+=getValueLen(x,handler);
}
return len;
}
case Value.RESULT_SET:
{
int len=1;
try {
ResultSet rs=((ValueResultSet)v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta=rs.getMetaData();
int columnCount=meta.getColumnCount();
len+=getVarIntLen(columnCount);
for (int i=0; i < columnCount; i++) {
len+=getStringLen(meta.getColumnName(i + 1));
len+=getVarIntLen(meta.getColumnType(i + 1));
len+=getVarIntLen(meta.getPrecision(i + 1));
len+=getVarIntLen(meta.getScale(i + 1));
}
while (rs.next()) {
len++;
for (int i=0; i < columnCount; i++) {
int t=DataType.getValueTypeFromResultSet(meta,i + 1);
Value val=DataType.readValue(null,rs,i + 1,t);
len+=getValueLen(val,handler);
}
}
len++;
rs.beforeFirst();
}
 catch (SQLException e) {
throw DbException.convert(e);
}
return len;
}
default :
throw DbException.throwInternalError("String_Node_Str" + v.getType());
}
}

Fixed version (ground truth):
/** 
 * Calculate the number of bytes required to encode the given value.
 * @param v the value
 * @param handler the data handler for lobs
 * @return the number of bytes required to store this value
 */
public static int getValueLen(Value v,DataHandler handler){
  if (v == ValueNull.INSTANCE) {
    return 1;
  }
switch (v.getType()) {
case Value.BOOLEAN:
    return 1;
case Value.BYTE:
  return 2;
case Value.SHORT:
return 3;
case Value.INT:
{
int x=v.getInt();
if (x < 0) {
  return 1 + getVarIntLen(-x);
}
 else if (x < 16) {
  return 1;
}
 else {
  return 1 + getVarIntLen(x);
}
}
case Value.LONG:
{
long x=v.getLong();
if (x < 0) {
return 1 + getVarLongLen(-x);
}
 else if (x < 8) {
return 1;
}
 else {
return 1 + getVarLongLen(x);
}
}
case Value.DOUBLE:
{
double x=v.getDouble();
if (x == 1.0d) {
return 1;
}
long d=Double.doubleToLongBits(x);
if (d == ValueDouble.ZERO_BITS) {
return 1;
}
return 1 + getVarLongLen(Long.reverse(d));
}
case Value.FLOAT:
{
float x=v.getFloat();
if (x == 1.0f) {
return 1;
}
int f=Float.floatToIntBits(x);
if (f == ValueFloat.ZERO_BITS) {
return 1;
}
return 1 + getVarIntLen(Integer.reverse(f));
}
case Value.STRING:
{
String s=v.getString();
int len=s.length();
if (len < 32) {
return 1 + getStringWithoutLengthLen(s,len);
}
return 1 + getStringLen(s);
}
case Value.STRING_IGNORECASE:
case Value.STRING_FIXED:
return 1 + getStringLen(v.getString());
case Value.DECIMAL:
{
BigDecimal x=v.getBigDecimal();
if (BigDecimal.ZERO.equals(x)) {
return 1;
}
 else if (BigDecimal.ONE.equals(x)) {
return 1;
}
int scale=x.scale();
BigInteger b=x.unscaledValue();
int bits=b.bitLength();
if (bits <= 63) {
if (scale == 0) {
return 1 + getVarLongLen(b.longValue());
}
return 1 + getVarIntLen(scale) + getVarLongLen(b.longValue());
}
byte[] bytes=b.toByteArray();
return 1 + getVarIntLen(scale) + getVarIntLen(bytes.length)+ bytes.length;
}
case Value.TIME:
if (SysProperties.STORE_LOCAL_TIME) {
long nanos=((ValueTime)v).getNanos();
long millis=nanos / 1000000;
nanos-=millis * 1000000;
return 1 + getVarLongLen(millis) + getVarLongLen(nanos);
}
return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(v.getTime()));
case Value.DATE:
{
if (SysProperties.STORE_LOCAL_TIME) {
long dateValue=((ValueDate)v).getDateValue();
return 1 + getVarLongLen(dateValue);
}
long x=DateTimeUtils.getTimeLocalWithoutDst(v.getDate());
return 1 + getVarLongLen(x / MILLIS_PER_MINUTE);
}
case Value.TIMESTAMP:
{
if (SysProperties.STORE_LOCAL_TIME) {
ValueTimestamp ts=(ValueTimestamp)v;
long dateValue=ts.getDateValue();
long nanos=ts.getTimeNanos();
long millis=nanos / 1000000;
nanos-=millis * 1000000;
return 1 + getVarLongLen(dateValue) + getVarLongLen(millis)+ getVarLongLen(nanos);
}
Timestamp ts=v.getTimestamp();
return 1 + getVarLongLen(DateTimeUtils.getTimeLocalWithoutDst(ts)) + getVarIntLen(ts.getNanos() % 1000000);
}
case Value.GEOMETRY:
case Value.JAVA_OBJECT:
{
byte[] b=v.getBytesNoCopy();
return 1 + getVarIntLen(b.length) + b.length;
}
case Value.BYTES:
{
byte[] b=v.getBytesNoCopy();
int len=b.length;
if (len < 32) {
return 1 + b.length;
}
return 1 + getVarIntLen(b.length) + b.length;
}
case Value.UUID:
return 1 + LENGTH_LONG + LENGTH_LONG;
case Value.BLOB:
case Value.CLOB:
{
int len=1;
if (v instanceof ValueLob) {
ValueLob lob=(ValueLob)v;
lob.convertToFileIfRequired(handler);
byte[] small=lob.getSmall();
if (small == null) {
int t=-1;
if (!lob.isLinked()) {
t=-2;
}
len+=getVarIntLen(t);
len+=getVarIntLen(lob.getTableId());
len+=getVarIntLen(lob.getObjectId());
len+=getVarLongLen(lob.getPrecision());
len+=1;
if (t == -2) {
len+=getStringLen(lob.getFileName());
}
}
 else {
len+=getVarIntLen(small.length);
len+=small.length;
}
}
 else {
ValueLobDb lob=(ValueLobDb)v;
byte[] small=lob.getSmall();
if (small == null) {
len+=getVarIntLen(-3);
len+=getVarIntLen(lob.getTableId());
len+=getVarLongLen(lob.getLobId());
len+=getVarLongLen(lob.getPrecision());
}
 else {
len+=getVarIntLen(small.length);
len+=small.length;
}
}
return len;
}
case Value.ARRAY:
{
Value[] list=((ValueArray)v).getList();
int len=1 + getVarIntLen(list.length);
for (Value x : list) {
len+=getValueLen(x,handler);
}
return len;
}
case Value.RESULT_SET:
{
int len=1;
try {
ResultSet rs=((ValueResultSet)v).getResultSet();
rs.beforeFirst();
ResultSetMetaData meta=rs.getMetaData();
int columnCount=meta.getColumnCount();
len+=getVarIntLen(columnCount);
for (int i=0; i < columnCount; i++) {
len+=getStringLen(meta.getColumnName(i + 1));
len+=getVarIntLen(meta.getColumnType(i + 1));
len+=getVarIntLen(meta.getPrecision(i + 1));
len+=getVarIntLen(meta.getScale(i + 1));
}
while (rs.next()) {
len++;
for (int i=0; i < columnCount; i++) {
int t=DataType.getValueTypeFromResultSet(meta,i + 1);
Value val=DataType.readValue(null,rs,i + 1,t);
len+=getValueLen(val,handler);
}
}
len++;
rs.beforeFirst();
}
 catch (SQLException e) {
throw DbException.convert(e);
}
return len;
}
default :
throw DbException.throwInternalError("String_Node_Str" + v.getType());
}
}