summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRucha Deodhar <rucha.deodhar@mariadb.com>2023-03-02 17:19:36 +0530
committerRucha Deodhar <rucha.deodhar@mariadb.com>2023-04-26 11:00:09 +0530
commit2c4c7c8b02140ff3abb8325f6261ccf1ae18b477 (patch)
treeeb823175f618dc8d059bc34eb05baba638170891
parentdffd1679ba97e5e8145575f0f11cb87553670c6f (diff)
downloadmariadb-git-2c4c7c8b02140ff3abb8325f6261ccf1ae18b477.tar.gz
MDEV-30704: JSON_SCHEMA_VALID: multipleOf must be greater than zero
Analysis: multipleOf must be strictly greater then 0. However the implementation only disallowed values strcitly less than 0 Fix: check if value is less than or equal to 0 instead of less than 0 and return true. Also fixed the incorrect return value for some other keywords.
-rw-r--r--mysql-test/main/func_json.result29
-rw-r--r--mysql-test/main/func_json.test39
-rw-r--r--sql/json_schema.cc41
-rw-r--r--sql/json_schema.h2
4 files changed, 100 insertions, 11 deletions
diff --git a/mysql-test/main/func_json.result b/mysql-test/main/func_json.result
index 5fee9ce4c32..11eb3a5aefb 100644
--- a/mysql-test/main/func_json.result
+++ b/mysql-test/main/func_json.result
@@ -4605,4 +4605,33 @@ SET @schema = '{
}';
SELECT JSON_SCHEMA_VALID(@schema, '2');
ERROR HY000: Invalid value for keyword enum
+#
+# MDEV-30704: JSON_SCHEMA_VALID: multipleOf must be greater than zero
+#
+SET @schema = '{
+ "multipleOf": 0
+ }';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword multipleOf
+SET @schema= '{ "maxLength" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword maxLength
+SET @schema= '{ "minLength" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword minLength
+SET @schema= '{ "maxProperties" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword maxProperties
+SET @schema= '{ "minProperties" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword minProperties
+SET @schema= '{ "maxItems" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword maxItems
+SET @schema= '{ "minItems" : -3}';
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+ERROR HY000: Invalid value for keyword maxLength
+SET @schema= '{ "items" : ["str1"]}';
+SELECT JSON_SCHEMA_VALID(@schema, '[]');
+ERROR HY000: Invalid value for keyword items
# End of 11.1 test
diff --git a/mysql-test/main/func_json.test b/mysql-test/main/func_json.test
index a1e52a0494c..3d9a8e3116b 100644
--- a/mysql-test/main/func_json.test
+++ b/mysql-test/main/func_json.test
@@ -3494,4 +3494,43 @@ SET @schema = '{
SELECT JSON_SCHEMA_VALID(@schema, '2');
+--echo #
+--echo # MDEV-30704: JSON_SCHEMA_VALID: multipleOf must be greater than zero
+--echo #
+
+SET @schema = '{
+ "multipleOf": 0
+ }';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "maxLength" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "minLength" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "maxProperties" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "minProperties" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "maxItems" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "minItems" : -3}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '2');
+
+SET @schema= '{ "items" : ["str1"]}';
+--error ER_JSON_INVALID_VALUE_FOR_KEYWORD
+SELECT JSON_SCHEMA_VALID(@schema, '[]');
+
+
--echo # End of 11.1 test
diff --git a/sql/json_schema.cc b/sql/json_schema.cc
index d0bb162cefe..7956ae49fff 100644
--- a/sql/json_schema.cc
+++ b/sql/json_schema.cc
@@ -774,7 +774,7 @@ bool Json_schema_multiple_of::validate(const json_engine_t *je,
double val= je->s.cs->strntod((char *) je->value,
je->value_len, &end, &err);
- double temp= val / this->value;
+ double temp= val / multiple_of;
bool res= (temp - (long long int)temp) == 0;
return !res;
@@ -797,12 +797,12 @@ bool Json_schema_multiple_of::handle_keyword(THD *thd, json_engine_t *je,
double val= je->s.cs->strntod((char *) je->value,
je->value_len, &end, &err);
- if (val < 0)
+ if (val <= 0)
{
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "multipleOf");
return true;
}
- value= val;
+ multiple_of= val;
return false;
}
@@ -834,8 +834,11 @@ bool Json_schema_max_len::handle_keyword(THD *thd, json_engine_t *je,
double val= je->s.cs->strntod((char *) je->value,
je->value_len, &end, &err);
if (val < 0)
+ {
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "maxLength");
- value= val;
+ return true;
+ }
+ value= (int)val;
return false;
}
@@ -869,9 +872,17 @@ bool Json_schema_min_len::handle_keyword(THD *thd, json_engine_t *je,
if (val < 0)
{
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "minLength");
+<<<<<<< HEAD
return true;
}
value= val;
+||||||| parent of 203f63d7bf1... MDEV-30704: JSON_SCHEMA_VALID: multipleOf must be greater than zero
+ value= val;
+=======
+ return true;
+ }
+ value= (int)val;
+>>>>>>> 203f63d7bf1... MDEV-30704: JSON_SCHEMA_VALID: multipleOf must be greater than zero
return false;
}
@@ -982,8 +993,11 @@ bool Json_schema_max_items::handle_keyword(THD *thd, json_engine_t *je,
double val= je->s.cs->strntod((char *) je->value,
je->value_len, &end, &err);
if (val < 0)
+ {
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "maxItems");
- value= val;
+ return true;
+ }
+ value= (int)val;
return false;
}
@@ -1037,7 +1051,7 @@ bool Json_schema_min_items::handle_keyword(THD *thd, json_engine_t *je,
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "maxLength");
return true;
}
- value= val;
+ value= (int)val;
return false;
}
@@ -1279,9 +1293,14 @@ bool Json_schema_prefix_items::handle_keyword(THD *thd, json_engine_t *je,
char *begin, *end;
int len;
- if (json_read_value(je))
- return true;
- begin= (char*)je->value;
+ if (json_read_value(je))
+ return true;
+ if (je->value_type != JSON_VALUE_OBJECT)
+ {
+ my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "items");
+ return true;
+ }
+ begin= (char*)je->value;
if (json_skip_level(je))
return true;
@@ -1452,7 +1471,7 @@ bool Json_schema_max_prop::handle_keyword(THD *thd, json_engine_t *je,
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "maxProperties");
return true;
}
- value= val;
+ value= (int)val;
return false;
}
@@ -1511,7 +1530,7 @@ bool Json_schema_min_prop::handle_keyword(THD *thd, json_engine_t *je,
my_error(ER_JSON_INVALID_VALUE_FOR_KEYWORD, MYF(0), "minProperties");
return true;
}
- value= val;
+ value= (int)val;
return false;
}
diff --git a/sql/json_schema.h b/sql/json_schema.h
index 4f1e146c862..53b6aaaff9f 100644
--- a/sql/json_schema.h
+++ b/sql/json_schema.h
@@ -227,6 +227,8 @@ class Json_schema_minimum : public Json_schema_keyword
class Json_schema_multiple_of : public Json_schema_keyword
{
+ private:
+ double multiple_of;
public:
bool validate(const json_engine_t *je, const uchar *k_start= NULL,
const uchar *k_end= NULL) override;