]> git.joonet.de Git - adminer.git/commitdiff
Notices: Avoid accessing offset on null
authorJakub Vrana <jakub@vrana.cz>
Wed, 26 Mar 2025 03:16:17 +0000 (04:16 +0100)
committerJakub Vrana <jakub@vrana.cz>
Wed, 26 Mar 2025 06:20:10 +0000 (07:20 +0100)
Thanks to @peterpp at 62017e3.

16 files changed:
adminer/call.inc.php
adminer/foreign.inc.php
adminer/include/adminer.inc.php
adminer/include/auth.inc.php
adminer/include/coverage.inc.php
adminer/include/design.inc.php
adminer/include/editing.inc.php
adminer/include/errors.inc.php
adminer/include/functions.inc.php
adminer/include/html.inc.php
adminer/indexes.inc.php
adminer/schema.inc.php
adminer/select.inc.php
coverage.php
editor/include/adminer.inc.php
plugins/drivers/elastic.php

index a9df194d646afbeb05fec0e3593c56b831a117ef..90d28596edbd7cf24af1f7835943ab4a7beb482f 100644 (file)
@@ -71,13 +71,13 @@ if ($in) {
                $field = $routine["fields"][$key];
                $name = $field["field"];
                echo "<tr><th>" . $adminer->fieldName($field);
-               $value = $_POST["fields"][$name];
+               $value = idx($_POST["fields"], $name);
                if ($value != "") {
                        if ($field["type"] == "set") {
                                $value = implode(",", $value);
                        }
                }
-               input($field, $value, (string) $_POST["function"][$name]); // param name can be empty
+               input($field, $value, idx($_POST["function"], $name, "")); // param name can be empty
                echo "\n";
        }
        echo "</table>\n";
index 3685d057ad30b06d32157de176b33061a507356a..3ce59fb917a76a56e455ff733d9716be90536f87 100644 (file)
@@ -95,7 +95,7 @@ $j = 0;
 foreach ($row["source"] as $key => $val) {
        echo "<tr>";
        echo "<td>" . html_select("source[" . (+$key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow.call(this);" : ""), "label-source");
-       echo "<td>" . html_select("target[" . (+$key) . "]", $target, $row["target"][$key], "", "label-target");
+       echo "<td>" . html_select("target[" . (+$key) . "]", $target, idx($row["target"], $key), "", "label-target");
        $j++;
 }
 ?>
index d79a50025d382384db2d47be73f7547d19071bb7..5529b54a23ee07cbd37e90d9bf66b8c3dd1c8ddf 100644 (file)
@@ -667,7 +667,7 @@ class Adminer {
                global $driver;
                restart_session();
                $history = &get_session("queries");
-               if (!$history[$_GET["db"]]) {
+               if (!idx($history, $_GET["db"])) {
                        $history[$_GET["db"]] = array();
                }
                if (strlen($query) > 1e6) {
index 5cb616213f9d20038c4303ad1c208e4f20262015..66abb11144edd40061dacd75ad22fde1b818c751 100644 (file)
@@ -62,7 +62,7 @@ function check_invalid_login() {
                }
        }
        $invalid = ($invalids ? $invalids[$adminer->bruteForceKey()] : array());
-       $next_attempt = ($invalid[1] > 29 ? $invalid[0] - time() : 0); // allow 30 invalid attempts
+       $next_attempt = (idx($invalid, 1) > 29 ? $invalid[0] - time() : 0); // allow 30 invalid attempts
        if ($next_attempt > 0) { //! do the same with permanent login
                auth_error(lang('Too many unsuccessful logins, try again in %d minute(s).', ceil($next_attempt / 60)));
        }
index e4eefdbab509b81828b4f48dc7fbe8cb8db11473..04db84b7c5ec9e29da7a3af75f94c8817b4f2898 100644 (file)
@@ -8,7 +8,7 @@ if (extension_loaded("xdebug") && file_exists(sys_get_temp_dir() . "/adminer.cov
                $coverage = unserialize(file_get_contents($coverage_filename));
                foreach (xdebug_get_code_coverage() as $filename => $lines) {
                        foreach ($lines as $l => $val) {
-                               if (!$coverage[$filename][$l] || $val > 0) {
+                               if (!idx($coverage[$filename], $l) || $val > 0) {
                                        $coverage[$filename][$l] = $val;
                                }
                        }
index 92a2ca31012221880506c8fcbe7a6d1492f93608..de33995e40cd5063137ff617a67532eb49fbf3a9 100644 (file)
@@ -182,7 +182,7 @@ function get_nonce() {
 function page_messages($error) {
        global $adminer;
        $uri = preg_replace('~^[^?]*~', '', $_SERVER["REQUEST_URI"]);
-       $messages = $_SESSION["messages"][$uri];
+       $messages = idx($_SESSION["messages"], $uri);
        if ($messages) {
                echo "<div class='message'>" . implode("</div>\n<div class='message'>", $messages) . "</div>" . script("messagesPrint();");
                unset($_SESSION["messages"][$uri]);
index 9e067d879e8abce885b8fb816de5168da4d7d62f..de12030e9c1824da7cf3048bd8dbea0410bae7bf 100644 (file)
@@ -353,7 +353,7 @@ function edit_fields($fields, $collations, $type = "TABLE", $foreign_keys = arra
        foreach ($fields as $i => $field) {
                $i++;
                $orig = $field[($_POST ? "orig" : "field")];
-               $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !$_POST["drop_col"][$i])) && (support("drop_col") || $orig == "");
+               $display = (isset($_POST["add"][$i-1]) || (isset($field["field"]) && !idx($_POST["drop_col"], $i))) && (support("drop_col") || $orig == "");
                echo "<tr" . ($display ? "" : " style='display: none;'") . ">\n";
                echo ($type == "PROCEDURE" ? "<td>" . html_select("fields[$i][inout]", explode("|", $driver->inout), $field["inout"]) : "") . "<th>";
                if ($display) {
index ac7c54c0adf9695125a66d177ae44590d191df54..b9a8838951d3c378c9da21c548e3a1798cc252f1 100644 (file)
@@ -3,8 +3,7 @@ namespace Adminer;
 
 error_reporting(24575); // all but E_DEPRECATED (overriding mysqli methods without types is deprecated)
 set_error_handler(function ($errno, $errstr) {
-       // "offset on null" mutes $_GET["fields"][0] if there's no ?fields[]= (62017e3 is a wrong fix for this)
        // "Undefined array key" mutes $_GET["q"] if there's no ?q=
        // "Undefined offset" and "Undefined index" are older messages for the same thing
-       return !!preg_match('~^(Trying to access array offset on( value of type)? null|Undefined (array key|offset|index))~', $errstr);
+       return !!preg_match('~^(Undefined (array key|offset|index))~', $errstr);
 }, E_WARNING | E_NOTICE); // warning since PHP 8.0
index adec2ef9e0b0c941e599566838e79d652af04866..95cdde8338941a136cd731037e42ff41e6021ebe 100644 (file)
@@ -65,6 +65,16 @@ function escape_string($val) {
        return substr(q($val), 1, -1);
 }
 
+/** Get a possibly missing item from a possibly missing array
+* @param array|null
+* @param string|int
+* @param mixed
+* @return mixed
+*/
+function idx($array, $key, $default = null) {
+       return ($array && array_key_exists($key, $array) ? $array[$key] : $default);
+}
+
 /** Remove non-digits from a string
 * @param string
 * @return string
@@ -302,12 +312,13 @@ function where($where, $fields = array()) {
        foreach ((array) $where["where"] as $key => $val) {
                $key = bracket_escape($key, 1); // 1 - back
                $column = escape_key($key);
-               $field_type = $fields[$key]["type"];
+               $field = ($fields ? $fields[$key] : array());
+               $field_type = $field["type"];
                $return[] = $column
                        . (JUSH == "sql" && $field_type == "json" ? " = CAST(" . q($val) . " AS JSON)"
                                : (JUSH == "sql" && is_numeric($val) && preg_match('~\.~', $val) ? " LIKE " . q($val) // LIKE because of floats but slow with ints
                                : (JUSH == "mssql" && strpos($field_type, "datetime") === false ? " LIKE " . q(preg_replace('~[_%[]~', '[\0]', $val)) // LIKE because of text but it does not work with datetime
-                               : " = " . unconvert_field($fields[$key], q($val)))))
+                               : " = " . unconvert_field($field, q($val)))))
                ; //! enum and set
                if (JUSH == "sql" && preg_match('~char|text~', $field_type) && preg_match("~[^ -@]~", $val)) { // not just [a-z] to catch non-ASCII characters
                        $return[] = "$column = " . q($val) . " COLLATE " . charset($connection) . "_bin";
index 97cf771b6871c4e8fcd1403cdb570bb598b99341..c85b1f935acd9e6e02956eded8a911d84670604a 100644 (file)
@@ -352,7 +352,7 @@ function process_input($field) {
                return null;
        }
        $idf = bracket_escape($field["field"]);
-       $function = $_POST["function"][$idf];
+       $function = idx($_POST["function"], $idf);
        $value = $_POST["fields"][$idf];
        if ($field["type"] == "enum" || $driver->enumLength($field)) {
                if ($value == -1) {
@@ -453,7 +453,7 @@ function edit_form($table, $fields, $row, $update) {
                $autofocus = !$_POST;
                foreach ($fields as $name => $field) {
                        echo "<tr><th>" . $adminer->fieldName($field);
-                       $default = $_GET["set"][bracket_escape($name)];
+                       $default = idx($_GET["set"], bracket_escape($name));
                        if ($default === null) {
                                $default = $field["default"];
                                if ($field["type"] == "bit" && preg_match("~^b'([01]*)'\$~", $default, $regs)) {
@@ -477,7 +477,7 @@ function edit_form($table, $fields, $row, $update) {
                                $value = $adminer->editVal($value, $field);
                        }
                        $function = ($_POST["save"]
-                               ? (string) $_POST["function"][$name]
+                               ? idx($_POST["function"], $name, "")
                                : ($update && preg_match('~^CURRENT_TIMESTAMP~i', $field["on_update"])
                                        ? "now"
                                        : ($value === false ? null : ($value !== null ? '' : 'NULL'))
index 89586f0d911c3f9d21d66a5ed128a57e040a5e66..02700a5f6ff43655d6b1d3d11d2fdb403e2f0ff9 100644 (file)
@@ -33,8 +33,8 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"]) {
                        ksort($index["columns"]);
                        foreach ($index["columns"] as $key => $column) {
                                if ($column != "") {
-                                       $length = $index["lengths"][$key];
-                                       $desc = $index["descs"][$key];
+                                       $length = idx($index["lengths"], $key);
+                                       $desc = idx($index["descs"], $key);
                                        $set[] = idf_escape($column) . ($length ? "(" . (+$length) . ")" : "") . ($desc ? " DESC" : "");
                                        $columns[] = $column;
                                        $lengths[] = ($length ?: null);
index f361f0fb3bcfea7f2b42eadf48354e5fb543b4a7..b2284d19491ed9456d111f36be64bd44dd55e3da 100644 (file)
@@ -32,8 +32,8 @@ foreach (table_status('', true) as $table => $table_status) {
        foreach ($adminer->foreignKeys($table) as $val) {
                if (!$val["db"]) {
                        $left = $base_left;
-                       if ($table_pos[$table][1] || $table_pos[$val["table"]][1]) {
-                               $left = min(floatval($table_pos[$table][1]), floatval($table_pos[$val["table"]][1])) - 1;
+                       if (idx($table_pos[$table], 1) || idx($table_pos[$val["table"]], 1)) {
+                               $left = min(idx($table_pos[$table], 1, 0), idx($table_pos[$val["table"]], 1, 0)) - 1;
                        } else {
                                $base_left -= .1;
                        }
@@ -71,7 +71,7 @@ foreach ($schema as $name => $table) {
 
        foreach ((array) $table["references"] as $target_name => $refs) {
                foreach ($refs as $left => $ref) {
-                       $left1 = $left - $table_pos[$name][1];
+                       $left1 = $left - idx($table_pos[$name], 1);
                        $i = 0;
                        foreach ($ref[0] as $source) {
                                echo "\n<div class='references' title='" . h($target_name) . "' id='refs$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$source]["pos"] . "em; padding-top: .5em;'>"
@@ -83,7 +83,7 @@ foreach ($schema as $name => $table) {
 
        foreach ((array) $referenced[$name] as $target_name => $refs) {
                foreach ($refs as $left => $columns) {
-                       $left1 = $left - $table_pos[$name][1];
+                       $left1 = $left - idx($table_pos[$name], 1);
                        $i = 0;
                        foreach ($columns as $target) {
                                echo "\n<div class='references arrow' title='" . h($target_name) . "' id='refd$left-" . ($i++) . "' style='left: $left1" . "em; top: " . $table["fields"][$target]["pos"] . "em;'>"
index 8ae4ca581c5f2797c56f031432e9600d94a54e8d..1cbb068339f78b3e8b21e641393b7c2c902b0ed4 100644 (file)
@@ -339,7 +339,7 @@ if (!$columns && support("table")) {
                        $rank = 1;
                        foreach ($rows[0] as $key => $val) {
                                if (!isset($unselected[$key])) {
-                                       $val = $_GET["columns"][key($select)];
+                                       $val = idx($_GET["columns"], key($select)) ?: array();
                                        $field = $fields[$select ? ($val ? $val["col"] : current($select)) : $key];
                                        $name = ($field ? $adminer->fieldName($field, $rank) : ($val["fun"] ? "*" : h($key)));
                                        if ($name != "") {
@@ -450,7 +450,7 @@ if (!$columns && support("table")) {
 
                                                $val = select_value($val, $link, $field, $text_length);
                                                $id = h("val[$unique_idf][" . bracket_escape($key) . "]");
-                                               $value = $_POST["val"][$unique_idf][bracket_escape($key)];
+                                               $value = idx(idx($_POST["val"], $unique_idf), bracket_escape($key));
                                                $editable = !is_array($row[$key]) && is_utf8($val) && $rows[$n][$key] == $row[$key] && !$functions[$key] && !$field["generated"];
                                                $text = preg_match('~text|json|lob~', $field["type"]);
                                                echo "<td id='$id'" . (preg_match(number_type(), $field["type"]) && is_numeric(strip_tags($val)) ? " class='number'" : "");
index 2c99751065d713a584960aeebcab03326965068d..fb015444c4ebfce01e6464760d3e8cb77f52cf80 100644 (file)
@@ -40,7 +40,7 @@ if (!extension_loaded("xdebug")) {
        for ($l=0; $l <= count($file); $l++) {
                $line = $file[$l];
                $color = "#C0FFC0"; // tested
-               switch ($coverage[realpath($filename)][$l+1]) {
+               switch ($coverage[realpath($filename)][$l+1] ?? null) {
                        case -1: // untested
                                $color = "#FFC0C0";
                                break;
index 6778772ce8f689bd8d93540e8f54a78832fe4c02..bda63f029790b6097f18df8d5e67f4aa9a4a9a74 100644 (file)
@@ -259,7 +259,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row
                                }
                                $key = $keys[$name];
                                $i--;
-                               echo "<div>" . h($desc) . input_hidden("where[$i][col]", $name) . input_hidden("where[$i][op]", "=") . ": <select name='where[$i][val]'>" . optionlist($options, $where[$key]["val"], true) . "</select></div>\n";
+                               echo "<div>" . h($desc) . input_hidden("where[$i][col]", $name) . input_hidden("where[$i][op]", "=") . ": <select name='where[$i][val]'>" . optionlist($options, idx($where[$key], "val"), true) . "</select></div>\n";
                                unset($columns[$name]);
                        }
                }
@@ -294,7 +294,7 @@ ORDER BY ORDINAL_POSITION", null, "") as $row
                }
                if ($orders) {
                        echo '<fieldset><legend>' . lang('Sort') . "</legend><div>";
-                       echo "<select name='index_order'>" . optionlist(array("" => "") + $orders, ($_GET["order"][0] != "" ? "" : $_GET["index_order"]), true) . "</select>";
+                       echo "<select name='index_order'>" . optionlist(array("" => "") + $orders, (idx($_GET["order"], 0) != "" ? "" : $_GET["index_order"]), true) . "</select>";
                        echo "</div></fieldset>\n";
                }
                if ($_GET["order"]) {
index 4b9f1cc133a8249b43531592fd191d3936667e7c..2369d0e80957163707c4d4b81ead58b0201f56d4 100644 (file)
@@ -274,7 +274,7 @@ if (isset($_GET["elastic"])) {
                function delete($table, $queryWhere, $limit = 0) {
                        //! use $limit
                        $ids = array();
-                       if (isset($_GET["where"]["_id"]) && $_GET["where"]["_id"]) {
+                       if (idx($_GET["where"], "_id")) {
                                $ids[] = $_GET["where"]["_id"];
                        }
                        if (isset($_POST['check'])) {