Skip to content

Commit fe0ba53

Browse files
committed
Meta: Improve ID casting when getting, updating or deleting meta data.
Blindly casting IDs to absolute integers in `get_metadata_by_mid()`, `update_metadata_by_mid()` and `delete_metadata_by_mid()` can cause unexpected behaviour when a floating or negative number is passed. Fixes #37746. git-svn-id: https://develop.svn.wordpress.org/trunk@38699 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 17ef6d8 commit fe0ba53

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/wp-includes/meta.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,12 +567,12 @@ function metadata_exists( $meta_type, $object_id, $meta_key ) {
567567
function get_metadata_by_mid( $meta_type, $meta_id ) {
568568
global $wpdb;
569569

570-
if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
570+
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
571571
return false;
572572
}
573573

574-
$meta_id = absint( $meta_id );
575-
if ( ! $meta_id ) {
574+
$meta_id = intval( $meta_id );
575+
if ( $meta_id <= 0 ) {
576576
return false;
577577
}
578578

@@ -611,12 +611,12 @@ function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key =
611611
global $wpdb;
612612

613613
// Make sure everything is valid.
614-
if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
614+
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
615615
return false;
616616
}
617617

618-
$meta_id = absint( $meta_id );
619-
if ( ! $meta_id ) {
618+
$meta_id = intval( $meta_id );
619+
if ( $meta_id <= 0 ) {
620620
return false;
621621
}
622622

@@ -702,12 +702,12 @@ function delete_metadata_by_mid( $meta_type, $meta_id ) {
702702
global $wpdb;
703703

704704
// Make sure everything is valid.
705-
if ( ! $meta_type || ! is_numeric( $meta_id ) ) {
705+
if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
706706
return false;
707707
}
708708

709-
$meta_id = absint( $meta_id );
710-
if ( ! $meta_id ) {
709+
$meta_id = intval( $meta_id );
710+
if ( $meta_id <= 0 ) {
711711
return false;
712712
}
713713

tests/phpunit/tests/meta.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,44 @@ function test_non_numeric_meta_id() {
292292
$this->assertFalse( delete_metadata_by_mid( 'user', array( 1 ) ) );
293293
}
294294

295+
/**
296+
* @ticket 37746
297+
*/
298+
function test_negative_meta_id() {
299+
$negative_mid = $this->meta_id * -1;
300+
301+
$this->assertTrue( $negative_mid < 0 );
302+
$this->assertFalse( get_metadata_by_mid( 'user', $negative_mid ) );
303+
$this->assertFalse( update_metadata_by_mid( 'user', $negative_mid, 'meta_new_value' ) );
304+
$this->assertFalse( delete_metadata_by_mid( 'user', $negative_mid ) );
305+
}
306+
307+
/**
308+
* @ticket 37746
309+
*/
310+
function test_floating_meta_id() {
311+
$floating_mid = $this->meta_id + 0.1337;
312+
313+
$this->assertTrue( floor( $floating_mid ) !== $floating_mid );
314+
$this->assertFalse( get_metadata_by_mid( 'user', $floating_mid ) );
315+
$this->assertFalse( update_metadata_by_mid( 'user', $floating_mid, 'meta_new_value' ) );
316+
$this->assertFalse( delete_metadata_by_mid( 'user', $floating_mid ) );
317+
}
318+
319+
/**
320+
* @ticket 37746
321+
*/
322+
function test_string_point_zero_meta_id() {
323+
$meta_id = add_metadata( 'user', $this->author->ID, 'meta_key', 'meta_value_2' );
324+
325+
$string_mid = "{$meta_id}.0";
326+
327+
$this->assertTrue( floor( $string_mid ) == $string_mid );
328+
$this->assertNotEquals( false, get_metadata_by_mid( 'user', $string_mid ) );
329+
$this->assertNotEquals( false, update_metadata_by_mid( 'user', $string_mid, 'meta_new_value_2' ) );
330+
$this->assertNotEquals( false, delete_metadata_by_mid( 'user', $string_mid ) );
331+
}
332+
295333
/**
296334
* @ticket 15030
297335
*/

0 commit comments

Comments
 (0)