From 26c9d64bfd436d54ae151a14323e27e86131b63f Mon Sep 17 00:00:00 2001 From: jakubvrana Date: Wed, 19 Aug 2009 17:10:06 +0000 Subject: [PATCH] Fix long SQL query crash (bug #2839231) Remove regular expression double repetition git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@982 7c3ca157-0c34-0410-bff1-cbf682f78f5c --- adminer/database.inc.php | 2 +- adminer/include/adminer.inc.php | 2 +- adminer/include/editing.inc.php | 2 +- adminer/include/functions.inc.php | 10 +++++----- adminer/include/mysql.inc.php | 4 ++-- adminer/index.php | 2 +- adminer/select.inc.php | 2 +- adminer/sql.inc.php | 4 ++-- changes.txt | 2 ++ 9 files changed, 16 insertions(+), 14 deletions(-) diff --git a/adminer/database.inc.php b/adminer/database.inc.php index b7bc6f67..e04355bf 100644 --- a/adminer/database.inc.php +++ b/adminer/database.inc.php @@ -51,7 +51,7 @@ if ($_POST) { // propose database name with limited privileges $result = $dbh->query("SHOW GRANTS"); while ($row = $result->fetch_row()) { - if (preg_match('~ ON (`(([^\\\\`]+|``|\\\\.)*)%`\\.\\*)?~', $row[0], $match) && $match[1]) { + if (preg_match('~ ON (`(([^\\\\`]|``|\\\\.)*)%`\\.\\*)?~', $row[0], $match) && $match[1]) { $name = stripcslashes(idf_unescape($match[2])); break; } diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index 461c896b..77954298 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -301,7 +301,7 @@ class Adminer { function selectOrderProcess($fields, $indexes) { $return = array(); foreach ((array) $_GET["order"] as $key => $val) { - if (isset($fields[$val]) || preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $val)) { + if (isset($fields[$val]) || preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $val)) { $return[] = idf_escape($val) . (isset($_GET["desc"][$key]) ? " DESC" : ""); } } diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index afbb8921..e0168b0d 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -146,7 +146,7 @@ function routine($name, $type) { global $dbh, $enum_length, $inout; $aliases = array("bit" => "tinyint", "bool" => "tinyint", "boolean" => "tinyint", "integer" => "int", "double precision" => "float", "real" => "float", "dec" => "decimal", "numeric" => "decimal", "fixed" => "decimal", "national char" => "char", "national varchar" => "varchar"); $type_pattern = "([a-z]+)(?:\\s*\\(((?:[^'\")]*|$enum_length)+)\\))?\\s*(zerofill\\s*)?(unsigned(?:\\s+zerofill)?)?(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s]+)['\"]?)?"; - $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]+|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; + $pattern = "\\s*(" . ($type == "FUNCTION" ? "" : implode("|", $inout)) . ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; $create = $dbh->result($dbh->query("SHOW CREATE $type " . idf_escape($name)), 2); preg_match("~\\(((?:$pattern\\s*,?)*)\\)" . ($type == "FUNCTION" ? "\\s*RETURNS\\s+$type_pattern" : "") . "\\s*(.*)~is", $create, $match); $fields = array(); diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index a0e297bd..a4b29bb1 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -67,7 +67,7 @@ function unique_idf($row, $indexes) { } $return = array(); foreach ($row as $key => $val) { - if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]+|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]+|``)+`\\))$~', $key)) { //! columns looking like functions + if (!preg_match('~^(COUNT\\((\\*|(DISTINCT )?`(?:[^`]|``)+`)\\)|(AVG|GROUP_CONCAT|MAX|MIN|SUM)\\(`(?:[^`]|``)+`\\))$~', $key)) { //! columns looking like functions $return[] = (isset($val) ? urlencode("where[" . bracket_escape($key) . "]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key)); } } @@ -79,11 +79,11 @@ function where($where) { $return = array(); foreach ((array) $where["where"] as $key => $val) { $key = bracket_escape($key, "back"); - $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " = BINARY " . $dbh->quote($val); //! enum and set, columns looking like functions + $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " = BINARY " . $dbh->quote($val); //! enum and set, columns looking like functions } foreach ((array) $where["null"] as $key) { $key = bracket_escape($key, "back"); - $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]+|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL"; + $return[] = (preg_match('~^[A-Z0-9_]+\\(`(?:[^`]|``)+`\\)$~', $key) ? $key : idf_escape($key)) . " IS NULL"; } return implode(" AND ", $return); } @@ -293,7 +293,7 @@ function input($field, $value, $function) { if (!isset($_GET["default"])) { echo "'; } - preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); + preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); foreach ($matches[1] as $i => $val) { $val = stripcslashes(str_replace("''", "'", $val)); $checked = (is_int($value) ? $value == $i+1 : $value === $val); @@ -308,7 +308,7 @@ function input($field, $value, $function) { if (strlen($input)) { echo $input; } elseif ($field["type"] == "set") { //! 64 bits - preg_match_all("~'((?:[^']+|'')*)'~", $field["length"], $matches); + preg_match_all("~'((?:[^']|'')*)'~", $field["length"], $matches); foreach ($matches[1] as $i => $val) { $val = stripcslashes(str_replace("''", "'", $val)); $checked = (is_int($value) ? ($value >> $i) & 1 : in_array($val, explode(",", $value), true)); diff --git a/adminer/include/mysql.inc.php b/adminer/include/mysql.inc.php index 42db3ff9..d6aca975 100644 --- a/adminer/include/mysql.inc.php +++ b/adminer/include/mysql.inc.php @@ -234,7 +234,7 @@ function indexes($table, $dbh2 = null) { function foreign_keys($table) { global $dbh, $on_actions; - static $pattern = '(?:[^`]+|``)+'; + static $pattern = '(?:[^`]|``)+'; $return = array(); $result = $dbh->query("SHOW CREATE TABLE " . idf_escape($table)); if ($result) { @@ -259,7 +259,7 @@ function foreign_keys($table) { function view($name) { global $dbh; - return array("select" => preg_replace('~^(?:[^`]+|`[^`]*`)* AS ~U', '', $dbh->result($dbh->query("SHOW CREATE VIEW " . idf_escape($name)), 1))); + return array("select" => preg_replace('~^(?:[^`]|`[^`]*`)* AS ~U', '', $dbh->result($dbh->query("SHOW CREATE VIEW " . idf_escape($name)), 1))); } function collations() { diff --git a/adminer/index.php b/adminer/index.php index 3e445478..4770b84e 100644 --- a/adminer/index.php +++ b/adminer/index.php @@ -8,7 +8,7 @@ include "./include/bootstrap.inc.php"; -$enum_length = '\'(?:\'\'|[^\'\\\\]+|\\\\.)*\'|"(?:""|[^"\\\\]+|\\\\.)*"'; +$enum_length = '\'(?:\'\'|[^\'\\\\]|\\\\.)*\'|"(?:""|[^"\\\\]|\\\\.)*"'; $inout = array("IN", "OUT", "INOUT"); if (isset($_GET["download"])) { diff --git a/adminer/select.inc.php b/adminer/select.inc.php index 2f2eacbe..82383415 100644 --- a/adminer/select.inc.php +++ b/adminer/select.inc.php @@ -85,7 +85,7 @@ if ($_POST && !$error) { $file = preg_replace("~^\xEF\xBB\xBF~", '', $file); //! character set $cols = ""; $rows = array(); //! packet size - preg_match_all('~("[^"]*"|[^"\\n]+)+~', $file, $matches); + preg_match_all('~("[^"]*"|[^"\\n])+~', $file, $matches); foreach ($matches[0] as $key => $val) { $row = array(); preg_match_all('~(("[^"]*")+|[^,]*),~', "$val,", $matches2); diff --git a/adminer/sql.inc.php b/adminer/sql.inc.php index 5da39648..587c1122 100644 --- a/adminer/sql.inc.php +++ b/adminer/sql.inc.php @@ -19,7 +19,7 @@ if (!$error && $_POST) { $delimiter = ";"; $offset = 0; $empty = true; - $space = "(\\s+|/\\*.*\\*/|(#|-- )[^\n]*\n|--\n)"; + $space = "(\\s|/\\*.*\\*/|(#|-- )[^\n]*\n|--\n)"; $dbh2 = (strlen($_GET["db"]) ? connect() : null); // connection for exploring indexes (to not replace FOUND_ROWS()) //! PDO - silent error if (is_object($dbh2)) { $dbh2->select_db($_GET["db"]); @@ -31,7 +31,7 @@ if (!$error && $_POST) { } elseif (preg_match('(' . preg_quote($delimiter) . '|[\'`"]|/\\*|-- |#|$)', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { if ($match[0][0] && $match[0][0] != $delimiter) { // is not end of a query - find closing part - $pattern = ($match[0][0] == "-- " || $match[0][0] == "#" ? '~.*~' : ($match[0][0] == "/*" ? '~.*\\*/~sU' : '~\\G([^\\\\' . $match[0][0] . ']+|\\\\.)*(' . $match[0][0] . '|$)~s')); + $pattern = ($match[0][0] == "-- " || $match[0][0] == "#" ? '~.*~' : ($match[0][0] == "/*" ? '~.*\\*/~sU' : '~\\G([^\\\\' . $match[0][0] . ']|\\\\.)*(' . $match[0][0] . '|$)~s')); //! respect sql_mode NO_BACKSLASH_ESCAPES preg_match($pattern, $query, $match, PREG_OFFSET_CAPTURE, $match[0][1] + 1); $offset = $match[0][1] + strlen($match[0][0]); } else { diff --git a/changes.txt b/changes.txt index dd0a8eef..8ab9aa5a 100644 --- a/changes.txt +++ b/changes.txt @@ -1,5 +1,7 @@ Adminer 2.0.1: +Display column comments in table overview Fix Editor date format +Fix long SQL query crash (bug #2839231) Adminer 2.0.0 (released 2009-08-06): Editor: User friendly data editor