[wp-trac] [WordPress Trac] #22192: update_option() strict checks can cause false negatives (was: update_metadata() and update_option() strict checks can cause false negatives)

WordPress Trac noreply at wordpress.org
Fri Sep 15 07:46:06 UTC 2023


#22192: update_option() strict checks can cause false negatives
-------------------------------------------------+-------------------------
 Reporter:  nacin                                |       Owner:  mukesh27
     Type:  defect (bug)                         |      Status:  assigned
 Priority:  normal                               |   Milestone:  6.4
Component:  Options, Meta APIs                   |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch 2nd-opinion dev-feedback   |     Focuses:
  has-unit-tests                                 |  performance
-------------------------------------------------+-------------------------
Description changed by mukesh27:

Old description:

> Given this:
>
> {{{
> add_post_meta( $post_id, 'key', 1 );
> update_post_meta( $post_id, 'key', 1 );
> }}}
>
> The update should not work, because they are the same. However, the meta
> cache will have "1" as a string, and then it will strict compare it to 1
> as an integer. Thus, an unnecessary update will run.
>
> Best I can tell, this could also affect update_option().
>
> It is quite common to use integers and booleans directly into these
> functions. They should be smart enough to recognize that "1" == 1 == true
> and "0" == 0 == false, and that any numeric string is also equal to a
> properly cast integer.
>
> Unit tests needed.
>
> Ticket from which this was spun: #22189, saving navigation menus is slow.

New description:

 Given this:

 {{{
 add_option( $option_name, 1 );
 update_option( $option_name, 1 );
 }}}

 The update should not work, because they are the same. However, the meta
 cache will have "1" as a string, and then it will strict compare it to 1
 as an integer. Thus, an unnecessary update will run.

 It is quite common to use integers and booleans directly into these
 functions. They should be smart enough to recognize that "1" == 1 == true
 and "0" == 0 == false, and that any numeric string is also equal to a
 properly cast integer.

 The new changes need unit tests.

 Ticket from which this was spun: #22189, saving navigation menus is slow.

--

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/22192#comment:34>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list