#vulnerable code 
                                @Test
    public void testIndexHalfStoreFileReader() throws Exception {
        Connection conn1 = getConnection();
        ConnectionQueryServices connectionQueryServices = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES);
		HBaseAdmin admin = connectionQueryServices.getAdmin();
		String tableName = "TBL_" + generateUniqueName();
		String indexName = "IDX_" + generateUniqueName();
        try {
            dropTable(admin, conn1);
            createBaseTable(conn1, tableName, "('e')");
            conn1.createStatement().execute("CREATE "+(localIndex?"LOCAL":"")+" INDEX " + indexName + " ON " + tableName + "(v1)" + (localIndex?"":" SPLIT ON ('e')"));
            conn1.createStatement().execute("UPSERT INTO "+tableName+" values('b',1,2,4,'z')");
            conn1.createStatement().execute("UPSERT INTO "+tableName+" values('f',1,2,3,'z')");
            conn1.createStatement().execute("UPSERT INTO "+tableName+" values('j',2,4,2,'a')");
            conn1.createStatement().execute("UPSERT INTO "+tableName+" values('q',3,1,1,'c')");
            conn1.commit();
            

            String query = "SELECT count(*) FROM " + tableName +" where v1<='z'";
            ResultSet rs = conn1.createStatement().executeQuery(query);
            assertTrue(rs.next());
            assertEquals(4, rs.getInt(1));

            TableName indexTable = TableName.valueOf(localIndex?tableName: indexName);
            admin.flush(indexTable);
            boolean merged = false;
            HTableInterface table = connectionQueryServices.getTable(indexTable.getName());
            // merge regions until 1 left
            long numRegions = 0;
            while (true) {
              rs = conn1.createStatement().executeQuery(query);
              assertTrue(rs.next());
              assertEquals(4, rs.getInt(1)); //TODO this returns 5 sometimes instead of 4, duplicate results?
              try {
                List<HRegionInfo> indexRegions = admin.getTableRegions(indexTable);
                numRegions = indexRegions.size();
                if (numRegions==1) {
                  break;
                }
                if(!merged) {
                          List<HRegionInfo> regions =
                                  admin.getTableRegions(indexTable);
                    Log.info("Merging: " + regions.size());
                    admin.mergeRegions(regions.get(0).getEncodedNameAsBytes(),
                        regions.get(1).getEncodedNameAsBytes(), false);
                    merged = true;
                    Threads.sleep(10000);
                }
              } catch (Exception ex) {
                Log.info(ex);
              }
              long waitStartTime = System.currentTimeMillis();
              // wait until merge happened
              while (System.currentTimeMillis() - waitStartTime < 10000) {
                List<HRegionInfo> regions = admin.getTableRegions(indexTable);
                Log.info("Waiting:" + regions.size());
                if (regions.size() < numRegions) {
                  break;
                }
                Threads.sleep(1000);
              }
              SnapshotTestingUtils.waitForTableToBeOnline(BaseTest.getUtility(), indexTable);
              assertTrue("Index table should be online ", admin.isTableAvailable(indexTable));
            }
        } finally {
            dropTable(admin, conn1);
        }
    } 
                            #location 9 
                            #vulnerability type RESOURCE_LEAK
#fixed code
                                @Test
    public void testIndexHalfStoreFileReader() throws Exception {
        Connection conn1 = getConnection();
        ConnectionQueryServices connectionQueryServices = driver.getConnectionQueryServices(getUrl(), TestUtil.TEST_PROPERTIES);
		HBaseAdmin admin = connectionQueryServices.getAdmin();
		String tableName = "TBL_" + generateUniqueName();
		String indexName = "IDX_" + generateUniqueName();
        createBaseTable(conn1, tableName, "('e')");
        conn1.createStatement().execute("CREATE "+(localIndex?"LOCAL":"")+" INDEX " + indexName + " ON " + tableName + "(v1)" + (localIndex?"":" SPLIT ON ('e')"));
        conn1.createStatement().execute("UPSERT INTO "+tableName+" values('b',1,2,4,'z')");
        conn1.createStatement().execute("UPSERT INTO "+tableName+" values('f',1,2,3,'z')");
        conn1.createStatement().execute("UPSERT INTO "+tableName+" values('j',2,4,2,'a')");
        conn1.createStatement().execute("UPSERT INTO "+tableName+" values('q',3,1,1,'c')");
        conn1.commit();
        

        String query = "SELECT count(*) FROM " + tableName +" where v1<='z'";
        ResultSet rs = conn1.createStatement().executeQuery(query);
        assertTrue(rs.next());
        assertEquals(4, rs.getInt(1));

        TableName indexTable = TableName.valueOf(localIndex?tableName: indexName);
        admin.flush(indexTable);
        boolean merged = false;
        HTableInterface table = connectionQueryServices.getTable(indexTable.getName());
        // merge regions until 1 left
        long numRegions = 0;
        while (true) {
          rs = conn1.createStatement().executeQuery(query);
          assertTrue(rs.next());
          assertEquals(4, rs.getInt(1)); //TODO this returns 5 sometimes instead of 4, duplicate results?
          try {
            List<HRegionInfo> indexRegions = admin.getTableRegions(indexTable);
            numRegions = indexRegions.size();
            if (numRegions==1) {
              break;
            }
            if(!merged) {
                      List<HRegionInfo> regions =
                              admin.getTableRegions(indexTable);
                Log.info("Merging: " + regions.size());
                admin.mergeRegions(regions.get(0).getEncodedNameAsBytes(),
                    regions.get(1).getEncodedNameAsBytes(), false);
                merged = true;
                Threads.sleep(10000);
            }
          } catch (Exception ex) {
            Log.info(ex);
          }
          long waitStartTime = System.currentTimeMillis();
          // wait until merge happened
          while (System.currentTimeMillis() - waitStartTime < 10000) {
            List<HRegionInfo> regions = admin.getTableRegions(indexTable);
            Log.info("Waiting:" + regions.size());
            if (regions.size() < numRegions) {
              break;
            }
            Threads.sleep(1000);
          }
          SnapshotTestingUtils.waitForTableToBeOnline(BaseTest.getUtility(), indexTable);
          assertTrue("Index table should be online ", admin.isTableAvailable(indexTable));
        }
    }