|
6 | 6 | package com.microsoft.sqlserver.jdbc; |
7 | 7 |
|
8 | 8 | import java.math.BigDecimal; |
| 9 | +import java.nio.BufferUnderflowException; |
9 | 10 | import java.nio.ByteBuffer; |
10 | 11 | import java.text.MessageFormat; |
11 | 12 | import java.util.ArrayList; |
@@ -98,7 +99,7 @@ abstract class SQLServerSpatialDatatype { |
98 | 99 | * corresponding data structures. |
99 | 100 | * |
100 | 101 | */ |
101 | | - protected abstract void parseWkb(); |
| 102 | + protected abstract void parseWkb() throws SQLServerException; |
102 | 103 |
|
103 | 104 | /** |
104 | 105 | * Create the WKT representation of Geometry/Geography from the deserialized data. |
@@ -1234,71 +1235,82 @@ protected void interpretSerializationPropBytes() { |
1234 | 1235 | isLargerThanHemisphere = (serializationProperties & isLargerThanHemisphereMask) != 0; |
1235 | 1236 | } |
1236 | 1237 |
|
1237 | | - protected void readNumberOfPoints() { |
| 1238 | + protected void readNumberOfPoints() throws SQLServerException { |
1238 | 1239 | if (isSinglePoint) { |
1239 | 1240 | numberOfPoints = 1; |
1240 | 1241 | } else if (isSingleLineSegment) { |
1241 | 1242 | numberOfPoints = 2; |
1242 | 1243 | } else { |
1243 | | - numberOfPoints = buffer.getInt(); |
| 1244 | + numberOfPoints = readInt(); |
| 1245 | + if (numberOfPoints <= 0) { |
| 1246 | + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ParsingError")); |
| 1247 | + Object[] msgArgs = {JDBCType.VARBINARY}; |
| 1248 | + throw new SQLServerException(this, form.format(msgArgs), null, 0, false); |
| 1249 | + } |
1244 | 1250 | } |
1245 | 1251 | } |
1246 | 1252 |
|
1247 | | - protected void readZvalues() { |
| 1253 | + protected void readZvalues() throws SQLServerException { |
1248 | 1254 | zValues = new double[numberOfPoints]; |
1249 | 1255 | for (int i = 0; i < numberOfPoints; i++) { |
1250 | | - zValues[i] = buffer.getDouble(); |
| 1256 | + zValues[i] = readDouble(); |
1251 | 1257 | } |
1252 | 1258 | } |
1253 | 1259 |
|
1254 | | - protected void readMvalues() { |
| 1260 | + protected void readMvalues() throws SQLServerException { |
1255 | 1261 | mValues = new double[numberOfPoints]; |
1256 | 1262 | for (int i = 0; i < numberOfPoints; i++) { |
1257 | | - mValues[i] = buffer.getDouble(); |
| 1263 | + mValues[i] = readDouble(); |
1258 | 1264 | } |
1259 | 1265 | } |
1260 | 1266 |
|
1261 | | - protected void readNumberOfFigures() { |
1262 | | - numberOfFigures = buffer.getInt(); |
| 1267 | + protected void readNumberOfFigures() throws SQLServerException { |
| 1268 | + numberOfFigures = readInt(); |
1263 | 1269 | } |
1264 | 1270 |
|
1265 | | - protected void readFigures() { |
| 1271 | + protected void readFigures() throws SQLServerException { |
1266 | 1272 | byte fa; |
1267 | 1273 | int po; |
1268 | 1274 | figures = new Figure[numberOfFigures]; |
1269 | 1275 | for (int i = 0; i < numberOfFigures; i++) { |
1270 | | - fa = buffer.get(); |
1271 | | - po = buffer.getInt(); |
| 1276 | + fa = readByte(); |
| 1277 | + po = readInt(); |
1272 | 1278 | figures[i] = new Figure(fa, po); |
1273 | 1279 | } |
1274 | 1280 | } |
1275 | 1281 |
|
1276 | | - protected void readNumberOfShapes() { |
1277 | | - numberOfShapes = buffer.getInt(); |
| 1282 | + protected void readNumberOfShapes() throws SQLServerException { |
| 1283 | + numberOfShapes = readInt(); |
1278 | 1284 | } |
1279 | 1285 |
|
1280 | | - protected void readShapes() { |
| 1286 | + protected void readShapes() throws SQLServerException { |
1281 | 1287 | int po; |
1282 | 1288 | int fo; |
1283 | 1289 | byte ogt; |
1284 | 1290 | shapes = new Shape[numberOfShapes]; |
1285 | 1291 | for (int i = 0; i < numberOfShapes; i++) { |
1286 | | - po = buffer.getInt(); |
1287 | | - fo = buffer.getInt(); |
1288 | | - ogt = buffer.get(); |
| 1292 | + po = readInt(); |
| 1293 | + fo = readInt(); |
| 1294 | + ogt = readByte(); |
1289 | 1295 | shapes[i] = new Shape(po, fo, ogt); |
1290 | 1296 | } |
1291 | 1297 | } |
1292 | 1298 |
|
1293 | | - protected void readNumberOfSegments() { |
1294 | | - numberOfSegments = buffer.getInt(); |
| 1299 | + protected void readNumberOfSegments() throws SQLServerException { |
| 1300 | + numberOfSegments = readInt(); |
1295 | 1301 | } |
1296 | 1302 |
|
1297 | | - protected void readSegments() { |
| 1303 | + protected void readSegments() throws SQLServerException { |
1298 | 1304 | byte st; |
1299 | | - segments = new Segment[numberOfSegments]; |
| 1305 | + try { |
| 1306 | + segments = new Segment[numberOfSegments]; |
| 1307 | + } catch (NegativeArraySizeException | OutOfMemoryError e) { |
| 1308 | + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ParsingError")); |
| 1309 | + Object[] msgArgs = {JDBCType.VARBINARY};//should throw some kind of 'array size too large error here' |
| 1310 | + throw new SQLServerException(this, form.format(msgArgs), null, 0, false); |
| 1311 | + } |
1300 | 1312 | for (int i = 0; i < numberOfSegments; i++) { |
1301 | | - st = buffer.get(); |
| 1313 | + st = readByte(); |
1302 | 1314 | segments[i] = new Segment(st); |
1303 | 1315 | } |
1304 | 1316 | } |
@@ -1646,6 +1658,29 @@ private void skipWhiteSpaces() { |
1646 | 1658 | currentWktPos++; |
1647 | 1659 | } |
1648 | 1660 | } |
| 1661 | + |
| 1662 | + private void checkBuffer(int i) throws SQLServerException { |
| 1663 | + if (buffer.remaining() < i) { |
| 1664 | + MessageFormat form = new MessageFormat(SQLServerException.getErrString("R_ParsingError")); |
| 1665 | + Object[] msgArgs = {JDBCType.VARBINARY};//invalid buffer error message maybe? |
| 1666 | + throw new SQLServerException(this, form.format(msgArgs), null, 0, false); |
| 1667 | + } |
| 1668 | + } |
| 1669 | + |
| 1670 | + protected byte readByte() throws SQLServerException { |
| 1671 | + checkBuffer(1); |
| 1672 | + return buffer.get(); |
| 1673 | + } |
| 1674 | + |
| 1675 | + protected int readInt() throws SQLServerException { |
| 1676 | + checkBuffer(4); |
| 1677 | + return buffer.getInt(); |
| 1678 | + } |
| 1679 | + |
| 1680 | + protected double readDouble() throws SQLServerException { |
| 1681 | + checkBuffer(8); |
| 1682 | + return buffer.getDouble(); |
| 1683 | + } |
1649 | 1684 | } |
1650 | 1685 |
|
1651 | 1686 |
|
|
0 commit comments