diff --git a/lib/ddl/simpletest/testddl.php b/lib/ddl/simpletest/testddl.php index aabd27c169f..378835214fb 100644 --- a/lib/ddl/simpletest/testddl.php +++ b/lib/ddl/simpletest/testddl.php @@ -434,6 +434,54 @@ class ddl_test extends UnitTestCase { $this->assertIdentical(get_class($e), 'coding_exception'); } + // Invalid decimal length + $table = new xmldb_table('test_table4'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('num', XMLDB_TYPE_NUMBER, '21,10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + try { + $dbman->create_table($table); + $this->fail('Exception expected'); + } catch (Exception $e) { + $this->assertIdentical(get_class($e), 'coding_exception'); + } + + // Invalid decimal decimals + $table = new xmldb_table('test_table4'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('num', XMLDB_TYPE_NUMBER, '10,11', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + try { + $dbman->create_table($table); + $this->fail('Exception expected'); + } catch (Exception $e) { + $this->assertIdentical(get_class($e), 'coding_exception'); + } + + // Invalid decimal default + $table = new xmldb_table('test_table4'); + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('num', XMLDB_TYPE_NUMBER, '10,5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 'x'); + $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); + $table->setComment("This is a test'n drop table. You can drop it safely"); + + $this->tables[$table->getName()] = $table; + + try { + $dbman->create_table($table); + $this->fail('Exception expected'); + } catch (Exception $e) { + $this->assertIdentical(get_class($e), 'coding_exception'); + } + } /** diff --git a/lib/xmldb/xmldb_field.php b/lib/xmldb/xmldb_field.php index 7716de4d362..d9e7a7f5c5b 100644 --- a/lib/xmldb/xmldb_field.php +++ b/lib/xmldb/xmldb_field.php @@ -726,6 +726,19 @@ class xmldb_field extends xmldb_object { break; case XMLDB_TYPE_NUMBER: + $length = $this->getLength(); + if (!is_number($length) or $length <= 0 or $length > 20) { + return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid length'; + } + $decimals = $this->getDecimals(); + $decimals = empty($decimals) ? 0 : $decimals; // fix missing decimals + if (!is_number($decimals) or $decimals < 0 or $decimals > $length) { + return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid decimals'; + } + $default = $this->getDefault(); + if (!empty($default) and !is_numeric($default)) { + return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_NUMBER field "'.$this->getName().'" has invalid default'; + } break; case XMLDB_TYPE_FLOAT: