WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 4 years ago

#33470 closed defect (bug) (fixed)

wpdb class method get_table_from_query() malfunctions if table name contains a dash (-)

Reported by: dustinbolton Owned by: pento
Milestone: 4.3.1 Priority: normal
Severity: normal Version: 4.1.2
Component: Database Keywords: has-patch commit fixed-major
Focuses: Cc:
PR Number:

Description

The wpdb class in wp-db.php contains a method get_table_from_query() for getting the table name from a passed query. This method as it stands does not properly handle tables with a dash (-) in the table name, instead only returning the table name characters up the point of the dash. This appears to impact all query formats. Adding a dash (-) in three locations to the regular expressions for the table name capture corrects this issue.

Examples:

get_table_from_query( "SHOW COLUMNS FROM wp_posts" );

Returns: wp_posts

get_table_from_query( "SHOW COLUMNS FROM wp-posts" );

Returns: wp

Attachments (2)

wp-db.php.diff (1.4 KB) - added by dustinbolton 4 years ago.
wp-db.php diff of changes
33470.patch (720 bytes) - added by tyxla 4 years ago.
Unit test for allowing dashes in table names in $wpdb::get_table_from_query()

Download all attachments as: .zip

Change History (16)

@dustinbolton
4 years ago

wp-db.php diff of changes

#1 @swissspidy
4 years ago

  • Keywords has-patch needs-unit-tests added

#2 @aaroncampbell
4 years ago

So, here's the thing with this. According to the MySQL Docs, unquoted table names can only contain [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) and U+0080 .. U+FFFF. A hyphen-minus is U+002D, so according to the docs that needs to be quoted. WordPress uses unquoted table names in a lot of places, so I think it probably makes sense to only match accordingly.

Having said that, in my tests on all the MySQL versions I have handy, hyphens seem to work just fine in table names unquoted. It looks like a discrepancy in their docs and actual practice.

#3 @pento
4 years ago

  • Milestone changed from Awaiting Review to 4.3.1
  • Version changed from trunk to 4.1.2

Patch looks good. Could you add some unit tests, please?

This will also need to be backported to all branches.

@tyxla
4 years ago

Unit test for allowing dashes in table names in $wpdb::get_table_from_query()

#4 @tyxla
4 years ago

  • Keywords needs-unit-tests removed

Patch works for me as well.
Attached a unit test for that case.
Some more different queries can be tested with dashed table names if you guys feel this is necessary.

#5 @pento
4 years ago

  • Owner set to pento
  • Resolution set to fixed
  • Status changed from new to closed

In 33718:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Props dustinbolton for the fix.

Fixes #33470.

#6 @pento
4 years ago

  • Keywords commit fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Re-opened for backporting.

Thanks for the patch, @tyxla. I ended up reworking an existing test to test any kind of table name we want to sanity check.

#7 @pento
4 years ago

In 33991:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 4.3 branch.

Props dustinbolton, pento.

See #33470.

#8 @pento
4 years ago

In 33992:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 4.2 branch.

Props dustinbolton, pento.

See #33470.

#9 @pento
4 years ago

In 33993:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 4.1 branch.

Props dustinbolton, pento.

See #33470.

#10 @pento
4 years ago

In 33994:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 4.0 branch.

Props dustinbolton, pento.

See #33470.

#11 @pento
4 years ago

In 33995:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 3.9 branch.

Props dustinbolton, pento.

See #33470.

#12 @pento
4 years ago

In 33996:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 3.8 branch.

Props dustinbolton, pento.

See #33470.

#13 @pento
4 years ago

In 33997:

WPDB: get_table_from_query() didn't find table names with hyphens in them.

Merge of [33718] to the 3.8 branch.

Props dustinbolton, pento.

See #33470.

#14 @pento
4 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

Okay, that's enough.

Congratulations to @dustinbolton for his first ticket and patch being backported all the way to 3.7! :-)

Note: See TracTickets for help on using tickets.