[v2] Verify that returned rows exist before extracting columns

Message ID 20200212200001.37293-1-lfleischer@archlinux.org
State New
Headers show
Series
  • [v2] Verify that returned rows exist before extracting columns
Related show

Commit Message

Lukas Fleischer Feb. 12, 2020, 8 p.m. UTC
Fix PHP notices such as "Trying to access array offset on value of type
bool" or "Trying to access array offset on value of type null".

Signed-off-by: Lukas Fleischer <lfleischer@archlinux.org>
---
 web/html/login.php       |  4 +++-
 web/lib/aur.inc.php      | 21 +++++++++++++++++++++
 web/lib/pkgfuncs.inc.php |  3 +++
 3 files changed, 27 insertions(+), 1 deletion(-)

Patch

diff --git a/web/html/login.php b/web/html/login.php
index 0145441..5308147 100644
--- a/web/html/login.php
+++ b/web/html/login.php
@@ -6,7 +6,9 @@  include_once("aur.inc.php");
 $disable_http_login = config_get_bool('options', 'disable_http_login');
 if (!$disable_http_login || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'])) {
 	$login = try_login();
-	$login_error = $login['error'];
+	if ($login) {
+		$login_error = $login['error'];
+	}
 }
 
 html_header('AUR ' . __("Login"));
diff --git a/web/lib/aur.inc.php b/web/lib/aur.inc.php
index e9530fc..2507df6 100644
--- a/web/lib/aur.inc.php
+++ b/web/lib/aur.inc.php
@@ -197,6 +197,9 @@  function username_from_id($id) {
 	}
 
 	$row = $result->fetch(PDO::FETCH_NUM);
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -222,6 +225,9 @@  function username_from_sid($sid="") {
 	}
 	$row = $result->fetch(PDO::FETCH_NUM);
 
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -339,6 +345,9 @@  function email_from_sid($sid="") {
 	}
 	$row = $result->fetch(PDO::FETCH_NUM);
 
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -365,6 +374,9 @@  function account_from_sid($sid="") {
 	}
 	$row = $result->fetch(PDO::FETCH_NUM);
 
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -390,6 +402,9 @@  function uid_from_sid($sid="") {
 	}
 	$row = $result->fetch(PDO::FETCH_NUM);
 
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -512,6 +527,9 @@  function uid_from_username($username) {
 	}
 
 	$row = $result->fetch(PDO::FETCH_NUM);
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
@@ -546,6 +564,9 @@  function uid_from_email($email) {
 	}
 
 	$row = $result->fetch(PDO::FETCH_NUM);
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }
 
diff --git a/web/lib/pkgfuncs.inc.php b/web/lib/pkgfuncs.inc.php
index a4cd17a..b30bfa9 100644
--- a/web/lib/pkgfuncs.inc.php
+++ b/web/lib/pkgfuncs.inc.php
@@ -147,6 +147,9 @@  function pkg_from_name($name="") {
 		return;
 	}
 	$row = $result->fetch(PDO::FETCH_NUM);
+	if (!$row) {
+		return null;
+	}
 	return $row[0];
 }