MDL-26922 dml - avoid PHP_INT_MAX overflow on limit queries
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 21 Mar 2011 17:49:08 +0000 (18:49 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Mon, 21 Mar 2011 17:49:08 +0000 (18:49 +0100)
lib/dml/mssql_native_moodle_database.php
lib/dml/sqlsrv_native_moodle_database.php

index be9fdb1..a1f1ddf 100644 (file)
@@ -694,6 +694,9 @@ class mssql_native_moodle_database extends moodle_database {
         if ($limitfrom or $limitnum) {
             if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later)
                 $fetch = $limitfrom + $limitnum;
+                if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow
+                    $fetch = PHP_INT_MAX;
+                }
                 $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
                                     "\\1SELECT\\2 TOP $fetch", $sql);
             }
index a319935..f3f25f9 100644 (file)
@@ -763,6 +763,9 @@ class sqlsrv_native_moodle_database extends moodle_database {
         if ($limitfrom or $limitnum) {
             if ($limitnum >= 1) { // Only apply TOP clause if we have any limitnum (limitfrom offset is handled later)
                 $fetch = $limitfrom + $limitnum;
+                if (PHP_INT_MAX - $limitnum < $limitfrom) { // Check PHP_INT_MAX overflow
+                    $fetch = PHP_INT_MAX;
+                }
                 $sql = preg_replace('/^([\s(])*SELECT([\s]+(DISTINCT|ALL))?(?!\s*TOP\s*\()/i',
                                     "\\1SELECT\\2 TOP $fetch", $sql);
             }