Skip to content

Commit 7a7333b

Browse files
committed
atk-4459: fix unsigned binary data overflow
1 parent ec08a41 commit 7a7333b

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

src/main/java/com/actiontech/dble/net/mysql/BinaryRowDataPacket.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.actiontech.dble.net.connection.AbstractConnection;
1212
import com.actiontech.dble.net.service.AbstractService;
1313
import com.actiontech.dble.net.service.WriteFlags;
14+
import com.actiontech.dble.plan.common.field.FieldUtil;
1415
import com.actiontech.dble.statistic.sql.StatisticListener;
1516
import com.actiontech.dble.util.ByteUtil;
1617
import com.actiontech.dble.util.DateUtil;
@@ -91,6 +92,7 @@ private void storeNullBitMap(int i) {
9192
private void convert(byte[] fv, FieldPacket fieldPk) {
9293

9394
int fieldType = fieldPk.getType();
95+
boolean unsigned = (fieldPk.getFlags() & FieldUtil.UNSIGNED_FLAG) != 0;
9496
switch (fieldType) {
9597
case Fields.FIELD_TYPE_STRING:
9698
case Fields.FIELD_TYPE_VARCHAR:
@@ -118,8 +120,14 @@ private void convert(byte[] fv, FieldPacket fieldPk) {
118120

119121
// Example
120122
// 01 00 00 00 00 00 00 00 -- int64 = 1
121-
long longVar = ByteUtil.getLong(fv);
122-
this.fieldValues.add(ByteUtil.getBytes(longVar));
123+
if (unsigned) {
124+
long longVar = ByteUtil.getUnsignedLong(fv);
125+
this.fieldValues.add(ByteUtil.getBytes(longVar));
126+
} else {
127+
long longVar = ByteUtil.getLong(fv);
128+
this.fieldValues.add(ByteUtil.getBytes(longVar));
129+
}
130+
123131
break;
124132
case Fields.FIELD_TYPE_LONG:
125133
case Fields.FIELD_TYPE_INT24:
@@ -128,8 +136,14 @@ private void convert(byte[] fv, FieldPacket fieldPk) {
128136

129137
// Example
130138
// 01 00 00 00 -- int32 = 1
131-
int intVar = ByteUtil.getInt(fv);
132-
this.fieldValues.add(ByteUtil.getBytes(intVar));
139+
if (unsigned) {
140+
int intVar = ByteUtil.getUnsignedInt(fv);
141+
this.fieldValues.add(ByteUtil.getBytes(intVar));
142+
} else {
143+
int intVar = ByteUtil.getInt(fv);
144+
this.fieldValues.add(ByteUtil.getBytes(intVar));
145+
}
146+
133147
break;
134148
case Fields.FIELD_TYPE_SHORT:
135149
case Fields.FIELD_TYPE_YEAR:

src/main/java/com/actiontech/dble/util/ByteUtil.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ public static int getInt(byte[] bytes) {
159159
// (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
160160
}
161161

162+
public static int getUnsignedInt(byte[] bytes) {
163+
return Integer.parseUnsignedInt(new String(bytes));
164+
// return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 &
165+
// (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
166+
}
167+
162168
public static long getLong(byte[] bytes) {
163169
return Long.parseLong(new String(bytes));
164170
// return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) |
@@ -169,6 +175,16 @@ public static long getLong(byte[] bytes) {
169175
// 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
170176
}
171177

178+
public static long getUnsignedLong(byte[] bytes) {
179+
return Long.parseUnsignedLong(new String(bytes));
180+
// return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) |
181+
// (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3]
182+
// << 24))
183+
// | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L &
184+
// ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] <<
185+
// 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
186+
}
187+
172188
public static double getDouble(byte[] bytes) {
173189
return Double.parseDouble(new String(bytes));
174190
}

0 commit comments

Comments
 (0)