Skip to content

Commit

Permalink
MDL-32112 validate integer definition in sql_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Mar 24, 2012
1 parent f9090b1 commit 720d605
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/ddl/simpletest/testddl.php
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,38 @@ public function test_create_table() {
$this->assertIdentical(get_class($e), 'coding_exception');
}

// Invalid integer 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('course', XMLDB_TYPE_INTEGER, '21', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '2');
$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 integer 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('course', XMLDB_TYPE_INTEGER, '10', 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');
}

}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/xmldb/xmldb_field.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,14 @@ function validateDefinition(xmldb_table $xmldb_table=null) {

switch ($this->getType()) {
case XMLDB_TYPE_INTEGER:
$length = $this->getLength();
if (!is_number($length) or $length <= 0 or $length > 20) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid length';
}
$default = $this->getDefault();
if (!empty($default) and !is_number($default)) {
return 'Invalid field definition in table {'.$xmldb_table->getName(). '}: XMLDB_TYPE_INTEGER field "'.$this->getName().'" has invalid default';
}
break;

case XMLDB_TYPE_NUMBER:
Expand Down

0 comments on commit 720d605

Please sign in to comment.