From 500f7a5bad20004fcf8a87d46428d0a20d9cd97b Mon Sep 17 00:00:00 2001 From: Prateek Saxena Date: Tue, 17 Jul 2018 10:24:03 +0530 Subject: [PATCH] HTMLTitleTextField, HTMLUserTextField: Check for `null` before using the value 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 | 5 +++++ includes/htmlform/fields/HTMLUserTextField.php | 7 ++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/includes/htmlform/fields/HTMLTitleTextField.php b/includes/htmlform/fields/HTMLTitleTextField.php index 602ddee486..0ad41d43bd 100644 --- a/includes/htmlform/fields/HTMLTitleTextField.php +++ b/includes/htmlform/fields/HTMLTitleTextField.php @@ -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 ); diff --git a/includes/htmlform/fields/HTMLUserTextField.php b/includes/htmlform/fields/HTMLUserTextField.php index e1939705ab..d6723144f5 100644 --- a/includes/htmlform/fields/HTMLUserTextField.php +++ b/includes/htmlform/fields/HTMLUserTextField.php @@ -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 ) -- 2.20.1