Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 1f2e21e

Browse files
committed
* Added codegen.cpp to source validator whitelist, since we have the ability to call printf() from codegen for debug.
* Beefed up overflow checks in NumericRuntime. * Fixed tests.
1 parent 9ec13a7 commit 1f2e21e

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

script/validators/source_validator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@
5858
"src/network/protocol.cpp",
5959
"src/include/common/macros.h",
6060
"src/common/stack_trace.cpp",
61-
"src/include/parser/sql_scanner.h", # There is a free() in comments
6261
"src/include/index/bloom_filter.h",
6362
"src/include/index/compact_ints_key.h",
6463
"src/include/index/bwtree.h",
6564
"src/codegen/util/oa_hash_table.cpp",
66-
"src/codegen/util/cc_hash_table.cpp"
65+
"src/codegen/util/cc_hash_table.cpp",
66+
"src/codegen/codegen.cpp", # We allow calling printf() from codegen for debugging
6767
]
6868

6969
## ==============================================

src/function/numeric_functions.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,26 @@ T ParseInteger(const char *ptr, uint32_t len) {
216216
}
217217

218218
// Convert
219+
uint64_t cutoff =
220+
static_cast<uint64_t>(negative ? -std::numeric_limits<int64_t>::min()
221+
: std::numeric_limits<int64_t>::max());
222+
uint64_t cutlimit = cutoff % 10;
223+
cutoff /= 10;
224+
219225
int64_t num = 0;
220226
while (start < end) {
221227
if (*start < '0' || *start > '9') {
222228
break;
223229
}
224230

225-
num = (num * 10) + (*start - '0');
231+
uint32_t c = static_cast<uint32_t>(*start - '0');
232+
233+
if (static_cast<uint64_t>(num) > cutoff ||
234+
(static_cast<uint64_t>(num) == cutoff && c > cutlimit)) {
235+
goto overflow;
236+
}
237+
238+
num = (num * 10) + c;
226239

227240
start++;
228241
}
@@ -234,8 +247,7 @@ T ParseInteger(const char *ptr, uint32_t len) {
234247

235248
// If we haven't consumed everything at this point, it was an invalid input
236249
if (start < end) {
237-
codegen::RuntimeFunctions::ThrowInvalidInputStringException();
238-
__builtin_unreachable();
250+
goto invalid;
239251
}
240252

241253
// Negate number if we need to
@@ -246,12 +258,19 @@ T ParseInteger(const char *ptr, uint32_t len) {
246258
// Range check
247259
if (num <= std::numeric_limits<T>::min() ||
248260
num >= std::numeric_limits<T>::max()) {
249-
codegen::RuntimeFunctions::ThrowOverflowException();
250-
__builtin_unreachable();
261+
goto overflow;
251262
}
252263

253264
// Done
254265
return static_cast<T>(num);
266+
267+
overflow:
268+
codegen::RuntimeFunctions::ThrowOverflowException();
269+
__builtin_unreachable();
270+
271+
invalid:
272+
codegen::RuntimeFunctions::ThrowInvalidInputStringException();
273+
__builtin_unreachable();
255274
}
256275

257276
} // namespace

test/codegen/value_integrity_test.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,9 @@ void TestInputIntegral(
198198

199199
// Default overflow tests
200200
std::vector<std::string> overflow_tests = {
201-
std::to_string(static_cast<int64_t>(std::numeric_limits<T>::min()) - 1),
202-
std::to_string(static_cast<int64_t>(std::numeric_limits<T>::max()) + 1)};
201+
std::to_string(std::numeric_limits<T>::min()) + "1",
202+
std::to_string(std::numeric_limits<T>::max()) + "1",
203+
"123456789123456789123456789"};
203204
overflow_tests.insert(overflow_tests.end(), extra_overflow_tests.begin(),
204205
extra_overflow_tests.end());
205206

test/common/internal_types_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ TEST_F(InternalTypesTests, PlanNodeTypeTest) {
325325
PlanNodeType::ORDERBY, PlanNodeType::PROJECTION,
326326
PlanNodeType::MATERIALIZE, PlanNodeType::LIMIT, PlanNodeType::DISTINCT,
327327
PlanNodeType::SETOP, PlanNodeType::APPEND, PlanNodeType::AGGREGATE_V2,
328-
PlanNodeType::HASH, PlanNodeType::RESULT, PlanNodeType::COPY,
329-
PlanNodeType::MOCK};
328+
PlanNodeType::HASH, PlanNodeType::RESULT,
329+
PlanNodeType::EXPORT_EXTERNAL_FILE, PlanNodeType::MOCK};
330330

331331
// Make sure that ToString and FromString work
332332
for (auto val : list) {

0 commit comments

Comments
 (0)