Skip to content

Commit c615ea6

Browse files
Merge pull request #12400 from vitlibar/fix-bad_typeid
Fix std::bad_typeid when JSON functions called with argument of wrong type
2 parents 7bc2c83 + 94c858b commit c615ea6

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

src/Common/typeid_cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ std::enable_if_t<std::is_pointer_v<To>, To> typeid_cast(From * from)
4747
{
4848
try
4949
{
50-
if ((typeid(From) == typeid(std::remove_pointer_t<To>)) || (typeid(*from) == typeid(std::remove_pointer_t<To>)))
50+
if ((typeid(From) == typeid(std::remove_pointer_t<To>)) || (from && typeid(*from) == typeid(std::remove_pointer_t<To>)))
5151
return static_cast<To>(from);
5252
else
5353
return nullptr;
@@ -64,7 +64,7 @@ std::enable_if_t<ext::is_shared_ptr_v<To>, To> typeid_cast(const std::shared_ptr
6464
{
6565
try
6666
{
67-
if ((typeid(From) == typeid(typename To::element_type)) || (typeid(*from) == typeid(typename To::element_type)))
67+
if ((typeid(From) == typeid(typename To::element_type)) || (from && typeid(*from) == typeid(typename To::element_type)))
6868
return std::static_pointer_cast<typename To::element_type>(from);
6969
else
7070
return nullptr;

src/Functions/FunctionsJSON.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ class JSONExtractImpl
892892
auto col_type_const = typeid_cast<const ColumnConst *>(col.column.get());
893893
if (!col_type_const || !isString(col.type))
894894
throw Exception{"The last argument of function " + String(function_name)
895-
+ " should be a constant string specifying the return data type, illegal value: " + col.column->getName(),
895+
+ " should be a constant string specifying the return data type, illegal value: " + col.name,
896896
ErrorCodes::ILLEGAL_COLUMN};
897897

898898
return DataTypeFactory::instance().get(col_type_const->getValue<String>());
@@ -929,7 +929,7 @@ class JSONExtractKeysAndValuesImpl
929929
auto col_type_const = typeid_cast<const ColumnConst *>(col.column.get());
930930
if (!col_type_const || !isString(col.type))
931931
throw Exception{"The last argument of function " + String(function_name)
932-
+ " should be a constant string specifying the values' data type, illegal value: " + col.column->getName(),
932+
+ " should be a constant string specifying the values' data type, illegal value: " + col.name,
933933
ErrorCodes::ILLEGAL_COLUMN};
934934

935935
DataTypePtr key_type = std::make_unique<DataTypeString>();

tests/queries/0_stateless/00918_json_functions.reference

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ d
9999
e
100100
u
101101
v
102+
--show error: type should be const string
102103
--allow_simdjson=0--
103104
--JSONLength--
104105
2
@@ -200,3 +201,4 @@ d
200201
e
201202
u
202203
v
204+
--show error: type should be const string

tests/queries/0_stateless/00918_json_functions.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ SELECT '--const/non-const mixed--';
108108
SELECT JSONExtractString('["a", "b", "c", "d", "e"]', idx) FROM (SELECT arrayJoin([1,2,3,4,5]) AS idx);
109109
SELECT JSONExtractString(json, 's') FROM (SELECT arrayJoin(['{"s":"u"}', '{"s":"v"}']) AS json);
110110

111+
SELECT '--show error: type should be const string';
112+
SELECT JSONExtractKeysAndValues([], JSONLength('^?V{LSwp')); -- { serverError 44 }
113+
WITH '{"i": 1, "f": 1.2}' AS json SELECT JSONExtract(json, 'i', JSONType(json, 'i')); -- { serverError 44 }
111114

112115

113116
SELECT '--allow_simdjson=0--';
@@ -219,3 +222,7 @@ SELECT JSONExtractKeysAndValuesRaw('{"a": "hello", "b": [-100, 200.0, 300], "c":
219222
SELECT '--const/non-const mixed--';
220223
SELECT JSONExtractString('["a", "b", "c", "d", "e"]', idx) FROM (SELECT arrayJoin([1,2,3,4,5]) AS idx);
221224
SELECT JSONExtractString(json, 's') FROM (SELECT arrayJoin(['{"s":"u"}', '{"s":"v"}']) AS json);
225+
226+
SELECT '--show error: type should be const string';
227+
SELECT JSONExtractKeysAndValues([], JSONLength('^?V{LSwp')); -- { serverError 44 }
228+
WITH '{"i": 1, "f": 1.2}' AS json SELECT JSONExtract(json, 'i', JSONType(json, 'i')); -- { serverError 44 }

0 commit comments

Comments
 (0)