Make WordPress Core

Ticket #15158: wpdb_prepare_null.15158.v3.patch

File wpdb_prepare_null.15158.v3.patch, 1.7 KB (added by jbutkus, 11 years ago)

Fixed patch for NULLs support in wpdb::prepare(), handling arguments count being lower than expected

  • wp-includes/wp-db.php

    diff --git a/wp-includes/wp-db.php b/wp-includes/wp-db.php
    index 391cfa4..9422835 100644
    a b class wpdb { 
    10001000                $query = str_replace( '"%s"', '%s', $query ); // doublequote unquoting
    10011001                $query = preg_replace( '|(?<!%)%f|' , '%F', $query ); // Force floats to be locale unaware
    10021002                $query = preg_replace( '|(?<!%)%s|', "'%s'", $query ); // quote the strings, avoiding escaped strings like %%s
     1003
     1004                // START replace NULLs
     1005                preg_match_all(
     1006                        '#\'?(?:[^%]|^)%(?:(\d+)\$)?[dfs]\'?#',
     1007                        $query,
     1008                        $positions,
     1009                        PREG_OFFSET_CAPTURE
     1010                );
     1011                if ( ! empty( $positions ) ) {
     1012                        $values     = array_values( $args );
     1013                        $index      = 0;
     1014                        $str_offset = 0;
     1015                        $max_index  = 0;
     1016                        foreach ( $positions[0] as $ref => $pattern ) {
     1017                                $loc_index = 0;
     1018                                if ( ! empty( $positions[1][$ref] ) ) {
     1019                                        $loc_index = ( (int)$positions[1][$ref][0] ) - 1;
     1020                                } else {
     1021                                        $loc_index = $index++;
     1022                                }
     1023                                if ( $loc_index > $max_index ) {
     1024                                        $max_index = $loc_index;
     1025                                }
     1026                                if ( ! isset( $values[$loc_index] ) ) {
     1027                                        unset( $values[$loc_index] ); // NULL is not set, but present
     1028                                        $format_length = strlen( $pattern[0] );
     1029                                        $query = substr(
     1030                                                $query,
     1031                                                0,
     1032                                                $pattern[1] + $str_offset
     1033                                                ) . ' NULL ' .
     1034                                                substr(
     1035                                                        $query,
     1036                                                        $pattern[1] + $format_length + $str_offset
     1037                                                );
     1038                                        if ( $format_length != 6 ) {
     1039                                                $str_offset += 6 - $format_length;
     1040                                        }
     1041                                }
     1042                        }
     1043                        if ( ++$max_index > count( $args ) ) {
     1044                                return; // too few arguments provided
     1045                        }
     1046                        $args = array_values( $values );
     1047                }
     1048                // END replace NULLs
     1049
    10031050                array_walk( $args, array( $this, 'escape_by_ref' ) );
    10041051                return @vsprintf( $query, $args );
    10051052        }