From d73a4de9b7e7a6e73f5b8e71d6c5e8611adf53cd Mon Sep 17 00:00:00 2001
From: unknown <joreland@mysql.com>
Date: Tue, 11 Jan 2005 13:30:11 +0100
Subject: [PATCH] bug#7798 - ndb - range scan with invalid table version could
 cause node failure

mysql-test/r/ndb_index_ordered.result:
  Test scan with invalid table version
mysql-test/t/ndb_index_ordered.test:
  Test scan with invalid table version
ndb/src/kernel/blocks/dbtc/DbtcMain.cpp:
  Set apiConnectstate= CS_ABORTING when receving a scan req with invalid
    table version
---
 mysql-test/r/ndb_index_ordered.result   | 16 ++++++++++++++
 mysql-test/t/ndb_index_ordered.test     | 18 ++++++++++++++++
 ndb/src/kernel/blocks/dbtc/DbtcMain.cpp | 28 ++++++++++++++++++++-----
 3 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/mysql-test/r/ndb_index_ordered.result b/mysql-test/r/ndb_index_ordered.result
index 75a5e42732b..943571aa524 100644
--- a/mysql-test/r/ndb_index_ordered.result
+++ b/mysql-test/r/ndb_index_ordered.result
@@ -420,3 +420,19 @@ count(*)-8
 select count(*)-9 from t1 use index (ti) where ti <= '23:59:59';
 count(*)-9
 0
+drop table t1;
+create table t1(a int primary key, b int not null, index(b));
+insert into t1 values (1,1), (2,2);
+set autocommit=0;
+begin;
+select count(*) from t1;
+count(*)
+2
+ALTER TABLE t1 ADD COLUMN c int;
+select a from t1 where b = 2;
+a
+2
+show tables;
+Tables_in_test
+t1
+drop table t1;
diff --git a/mysql-test/t/ndb_index_ordered.test b/mysql-test/t/ndb_index_ordered.test
index 71635159604..89f1e5b7e9f 100644
--- a/mysql-test/t/ndb_index_ordered.test
+++ b/mysql-test/t/ndb_index_ordered.test
@@ -236,3 +236,21 @@ select count(*)-5 from t1 use index (ti) where ti <  '10:11:11';
 select count(*)-6 from t1 use index (ti) where ti <= '10:11:11';
 select count(*)-8 from t1 use index (ti) where ti <  '23:59:59';
 select count(*)-9 from t1 use index (ti) where ti <= '23:59:59';
+
+drop table t1;
+
+# bug#7798
+create table t1(a int primary key, b int not null, index(b));
+insert into t1 values (1,1), (2,2);
+connect (con1,localhost,,,test);
+connect (con2,localhost,,,test);
+connection con1;
+set autocommit=0;
+begin;
+select count(*) from t1;
+connection con2;
+ALTER TABLE t1 ADD COLUMN c int;
+connection con1;
+select a from t1 where b = 2;
+show tables;
+drop table t1;
diff --git a/ndb/src/kernel/blocks/dbtc/DbtcMain.cpp b/ndb/src/kernel/blocks/dbtc/DbtcMain.cpp
index dd1252b76b9..815d6c9d838 100644
--- a/ndb/src/kernel/blocks/dbtc/DbtcMain.cpp
+++ b/ndb/src/kernel/blocks/dbtc/DbtcMain.cpp
@@ -1879,7 +1879,6 @@ void Dbtc::packKeyData000Lab(Signal* signal,
 			     Uint32 totalLen) 
 {
   CacheRecord * const regCachePtr = cachePtr.p;
-  UintR Tmp;
 
   jam();
   Uint32 len = 0;
@@ -8503,14 +8502,16 @@ void Dbtc::execSCAN_TABREQ(Signal* signal)
   apiConnectptr.i = scanTabReq->apiConnectPtr;
   tabptr.i = scanTabReq->tableId;
 
-  if (apiConnectptr.i >= capiConnectFilesize ||
-      tabptr.i >= ctabrecFilesize) {
+  if (apiConnectptr.i >= capiConnectFilesize)
+  {
     jam();
     warningHandlerLab(signal);
     return;
   }//if
+
   ptrAss(apiConnectptr, apiConnectRecord);
   ApiConnectRecord * transP = apiConnectptr.p;
+
   if (transP->apiConnectstate != CS_CONNECTED) {
     jam();
     // could be left over from TCKEYREQ rollback
@@ -8524,9 +8525,16 @@ void Dbtc::execSCAN_TABREQ(Signal* signal)
     } else {
       jam();
       errCode = ZSTATE_ERROR;
-      goto SCAN_TAB_error;
+      goto SCAN_TAB_error_no_state_change;
     }
   }
+
+  if(tabptr.i >= ctabrecFilesize)
+  {
+    errCode = ZUNKNOWN_TABLE_ERROR;
+    goto SCAN_TAB_error;
+  }
+
   ptrAss(tabptr, tableRecord);
   if ((aiLength == 0) ||
       (!tabptr.p->checkTable(schemaVersion)) ||
@@ -8621,8 +8629,18 @@ void Dbtc::execSCAN_TABREQ(Signal* signal)
   errCode = ZNO_SCANREC_ERROR;
   goto SCAN_TAB_error;
  
- SCAN_TAB_error:
+SCAN_TAB_error:
   jam();
+  /**
+   * Prepare for up coming ATTRINFO/KEYINFO
+   */
+  transP->apiConnectstate = CS_ABORTING;
+  transP->abortState = AS_IDLE;
+  transP->transid[0] = transid1;
+  transP->transid[1] = transid2;
+ 
+SCAN_TAB_error_no_state_change:
+  
   ScanTabRef * ref = (ScanTabRef*)&signal->theData[0];
   ref->apiConnectPtr = transP->ndbapiConnect;
   ref->transId1 = transid1;
-- 
2.30.9