HTMLTitleTextField, HTMLUserTextField: Check for `null` before using the value
authorPrateek Saxena <prtksxna@gmail.com>
Tue, 17 Jul 2018 04:54:03 +0000 (10:24 +0530)
committerBartosz Dziewoński <matma.rex@gmail.com>
Tue, 17 Jul 2018 22:42:52 +0000 (00:42 +0200)
Otherwise HTMLTitleTextField sends null to Title::newFromTextThrow(),
which causes an exception when trying to look it up in a cache.

Similar issue is present in HTMLUserTextField, although that one
hasn't caused problems in practice yet.

Follows-up on I93ad51ffe7bee597d2d127f4c5d6b2929ffc8f7e and
I0de4194a37b6ef260d35feb1e6730985775d5351.

Bug: T199763
Change-Id: I29ecd94cdf9e3064e6e9e7f4e65a50f267b5282d

includes/htmlform/fields/HTMLTitleTextField.php
includes/htmlform/fields/HTMLUserTextField.php

index 602ddee..0ad41d4 100644 (file)
@@ -41,6 +41,11 @@ class HTMLTitleTextField extends HTMLTextField {
                        return parent::validate( $value, $alldata );
                }
 
+               // Default value (from getDefault()) is null, which breaks Title::newFromTextThrow() below
+               if ( $value === null ) {
+                       $value = '';
+               }
+
                if ( !$this->mParams['required'] && $value === '' ) {
                        // If this field is not required and the value is empty, that's okay, skip validation
                        return parent::validate( $value, $alldata );
index e193970..d672314 100644 (file)
@@ -34,6 +34,11 @@ class HTMLUserTextField extends HTMLTextField {
        }
 
        public function validate( $value, $alldata ) {
+               // Default value (from getDefault()) is null, User::newFromName() expects a string
+               if ( $value === null ) {
+                       $value = '';
+               }
+
                // check, if a user exists with the given username
                $user = User::newFromName( $value, false );
                $rangeError = null;
@@ -43,7 +48,7 @@ class HTMLUserTextField extends HTMLTextField {
                } elseif (
                        // check, if the user exists, if requested
                        ( $this->mParams['exists'] && $user->getId() === 0 ) &&
-                       // check, if the username is a valid IP address, otherweise save the error message
+                       // check, if the username is a valid IP address, otherwise save the error message
                        !( $this->mParams['ipallowed'] && IP::isValid( $value ) ) &&
                        // check, if the username is a valid IP range, otherwise save the error message
                        !( $this->mParams['iprange'] && ( $rangeError = $this->isValidIPRange( $value ) ) === true )