Simple Machines Community Forum

SMF Development => Bug Reports => Fixed or Bogus Bugs => Topic started by: Matthew K. on March 28, 2016, 10:28:17 PM

Title: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 28, 2016, 10:28:17 PM
There is a bug in SMF 2.0.x that pertains to using db_create_table (found in DbPackages-mysql.php or the other dbms files).

When the function is parsing the size of the column that you are trying to create, it checks to see if the size is numeric (is_numeric(); (http://php.net/is_numeric)) which totally makes sense...in most cases. Where this becomes an issue is when you are wanting to use a column type that is more than just an integer. Right in phpMyAdmin, you can see the "size" is specified as "Length/Values", meaning it theoretically, in some cases can hold more than one value. I ran into this issue when trying to make two columns "points" and "total_points" both using float(6,2).

Code is below for reference. This is obviously just a slight oversight, but it prevents mod authors from using any column types that are beyond a single-integer, like float. To make my modification work, I also have to provide a modifications.xml file in an otherwise modification-less mod to fix SMF's bug in my mod.

./Sources/DbPackages-mysql:130-132:

// Sort out the size... and stuff...
$column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;
list ($type, $size) = $smcFunc['db_calculate_type']($column['type'], $column['size']);


Usage Example:

[
'name' => 'points',
'type' => 'float',
'size' => '6,2',
'unsigned' => true,
'default' => 0.00
],
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 28, 2016, 10:32:43 PM
A proposed fixed (without testing), would be to add right below line 132 the following:

if ($type === 'float')
$size = $column['size'];

Obviously, this should also carry over to decimals, and any other fields that potentially take on multiple values. Another thought might be to just remove the check for is_numeric() on size, since more than not, it might be wrong. Or explode by commas, and (int) or floatval(); each piece.

This issue is in two functions, I also found it later in db_add_column.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 28, 2016, 10:40:24 PM
After looking into this with a little bit more detail...I notice the obvious, that $column['size'] is over-written on line 131. Which means my proposed fix without testing will not work. More on this soon.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 28, 2016, 10:44:13 PM
Okay, so the fix is actually really simple:
Code (Find) Select

$column['size'] = isset($column['size']) && is_numeric($column['size']) ? $column['size'] : null;

Code (Replace) Select

$column['size'] = isset($column['size']) && ($column['type'] === 'float' || is_numeric($column['size'])) ? $column['size'] : null;

Again...this will have to be changed to something similar to this:

$column['size'] = isset($column['size']) && (in_array($column['type'], array('float', 'decimal', 'double')) || is_numeric($column['size'])) ? $column['size'] : null;
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 30, 2016, 11:30:24 PM
Does no one on the SMF team care to reply to this? I found a bug that's been in SMF for years, if not since SMF 2.0 at the very earliest and could possibly still be in SMF 2.1. And no reply?
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Kindred on March 30, 2016, 11:58:48 PM
Thank you for th report. Sorry for not responding as fast as you wanted.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 31, 2016, 10:37:18 AM
There's actually another bug that is somewhat related in the same file. I'll post it later, hopefully I'm not wasting my time reporting them.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Colin on March 31, 2016, 12:28:31 PM
Thanks for your time.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on March 31, 2016, 07:31:17 PM
./Sources/DbPackages-Mysql.php

Code (Find) Select

// Allow unsigned integers (mysql only)
$unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint')) && !empty($column['unsigned']) ? 'unsigned ' : '';

Code (Replace) Select

// Allow unsigned integers (mysql only)
$unsigned = in_array($type, array('int', 'tinyint', 'smallint', 'mediumint', 'bigint', 'float')) && !empty($column['unsigned']) ? 'unsigned ' : '';

Again...this would need to encompass other column types that can be UNSIGNED. Such as decimal, and double. There are three instances of this line in this file. That are all identical.
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on June 26, 2016, 12:05:50 PM
Is this going to get taken care of in the next patch of SMF 2.0.x and at the very least SMF 2.1?
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Kindred on June 26, 2016, 08:03:27 PM
Don't know about 2.0.12... but I think it is already fixed in 2.1
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: nend on June 26, 2016, 09:44:34 PM
It's included.  ;)
Title: Re: [2.0.x] Bug with Creating Tables in Modifications
Post by: Matthew K. on June 27, 2016, 12:57:00 PM
Great news, thanks guys