<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[32299] trunk: WPDB: Sanity check that any strings being stored in the DB are not too long to store correctly.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/32299">32299</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/32299","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>pento</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2015-04-27 14:02:45 +0000 (Mon, 27 Apr 2015)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>WPDB: Sanity check that any strings being stored in the DB are not too long to store correctly.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeswpdbphp">trunk/src/wp-includes/wp-db.php</a></li>
<li><a href="#trunktestsphpunittestscommentphp">trunk/tests/phpunit/tests/comment.php</a></li>
<li><a href="#trunktestsphpunittestsdbphp">trunk/tests/phpunit/tests/db.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeswpdbphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/wp-db.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/wp-db.php   2015-04-27 05:09:24 UTC (rev 32298)
+++ trunk/src/wp-includes/wp-db.php     2015-04-27 14:02:45 UTC (rev 32299)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1946,11 +1946,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        protected function process_fields( $table, $data, $format ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $data = $this->process_field_formats( $data, $format );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( false === $data ) {
+                       return false;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $data = $this->process_field_charsets( $data, $table );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $data ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return false;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $data = $this->process_field_lengths( $data, $table );
+               if ( false === $data ) {
+                       return false;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $converted_data = $this->strip_invalid_text( $data );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $data !== $converted_data ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2032,6 +2041,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * For string fields, record the maximum string length that field can safely save.
+        *
+        * @since 4.2.1
+        * @access protected
+        *
+        * @param array  $data  As it comes from the wpdb::process_field_charsets() method.
+        * @param string $table Table name.
+        * @return array|False The same array as $data with additional 'length' keys, or false if
+        *                     any of the values were too long for their corresponding field.
+        */
+       protected function process_field_lengths( $data, $table ) {
+               foreach ( $data as $field => $value ) {
+                       if ( '%d' === $value['format'] || '%f' === $value['format'] ) {
+                               // We can skip this field if we know it isn't a string.
+                               // This checks %d/%f versus ! %s because it's sprintf() could take more.
+                               $value['length'] = false;
+                       } else {
+                               $value['length'] = $this->get_col_length( $table, $field );
+                               if ( is_wp_error( $value['length'] ) ) {
+                                       return false;
+                               }
+                       }
+
+                       if ( false !== $value['length'] && strlen( $value['value'] ) > $value['length'] ) {
+                               return false;
+                       }
+
+                       $data[ $field ] = $value;
+               }
+
+               return $data;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Retrieve one variable from the database.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Executes a SQL query and returns the value from the SQL result.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2362,6 +2405,77 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Retrieve the maximum string length allowed in a given column.
+        *
+        * @since 4.2.1
+        * @access public
+        *
+        * @param string $table  Table name.
+        * @param string $column Column name.
+        * @return mixed Max column length as an int. False if the column has no
+        *               length. WP_Error object if there was an error.
+        */
+       public function get_col_length( $table, $column ) {
+               $tablekey = strtolower( $table );
+               $columnkey = strtolower( $column );
+
+               // Skip this entirely if this isn't a MySQL database.
+               if ( false === $this->is_mysql ) {
+                       return false;
+               }
+
+               if ( empty( $this->col_meta[ $tablekey ] ) ) {
+                       // This primes column information for us.
+                       $table_charset = $this->get_table_charset( $table );
+                       if ( is_wp_error( $table_charset ) ) {
+                               return $table_charset;
+                       }
+               }
+
+               if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) {
+                       return false;
+               }
+
+               $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type );
+
+               $type = strtolower( $typeinfo[0] );
+               if ( ! empty( $typeinfo[1] ) ) {
+                       $length = trim( $typeinfo[1], ')' );
+               } else {
+                       $length = false;
+               }
+
+               switch( $type ) {
+                       case 'binary':
+                       case 'char':
+                       case 'varbinary':
+                       case 'varchar':
+                               return $length;
+                               break;
+                       case 'tinyblob':
+                       case 'tinytext':
+                               return 255; // 2^8 - 1
+                               break;
+                       case 'blob':
+                       case 'text':
+                               return 65535; // 2^16 - 1
+                               break;
+                       case 'mediumblob':
+                       case 'mediumtext':
+                               return 16777215; // 2^24 - 1
+                               break;
+                       case 'longblob':
+                       case 'longtext':
+                               return 4294967295; // 2^32 - 1
+                               break;
+                       default:
+                               return false;
+               }
+
+               return false;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Check if a string is ASCII.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * The negative regex is faster for non-ASCII strings, as it allows
</span></span></pre></div>
<a id="trunktestsphpunittestscommentphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/comment.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/comment.php     2015-04-27 05:09:24 UTC (rev 32298)
+++ trunk/tests/phpunit/tests/comment.php       2015-04-27 14:02:45 UTC (rev 32299)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -112,4 +112,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        unset( $_SERVER['REMOTE_ADDR'] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       public function test_comment_field_lengths() {
+               // `wp_new_comment()` checks REMOTE_ADDR, so we fake it to avoid PHP notices.
+               if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
+                       $remote_addr = $_SERVER['REMOTE_ADDR'];
+               } else {
+                       $_SERVER['REMOTE_ADDR'] = '';
+               }
+
+               $post_id = $this->factory->post->create();
+
+               $data = array(
+                       'comment_post_ID' => $post_id,
+                       'comment_author' => rand_str(),
+                       'comment_author_url' => '',
+                       'comment_author_email' => '',
+                       'comment_type' => '',
+                       'comment_content' => str_repeat( 'A', 65536 ),
+                       'comment_date' => '2011-01-01 10:00:00',
+                       'comment_date_gmt' => '2011-01-01 10:00:00',
+               );
+
+               $id = wp_new_comment( $data );
+
+               $this->assertFalse( $id );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestsdbphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/db.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/db.php  2015-04-27 05:09:24 UTC (rev 32298)
+++ trunk/tests/phpunit/tests/db.php    2015-04-27 14:02:45 UTC (rev 32299)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -747,6 +747,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                'format' => '%s',
</span><span class="cx" style="display: block; padding: 0 10px">                                'charset' => $expected_charset,
</span><span class="cx" style="display: block; padding: 0 10px">                                'ascii' => false,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>