Program: 139294
Project: 139294
Buggy source:
/** 
 * Method "getSchemas" returns a map of catalogs to schemas. Warning: if no catalog is found, catalog name (i.e. key of the map) can be null.
 * @param connection the connection
 * @return a map [catalog's name -> list of Schemas ].
 * @throws SQLException
 */
public static Map<String,List<TdSchema>> getSchemas(Connection connection) throws SQLException {
  Map<String,List<TdSchema>> catalogName2schemas=new HashMap<String,List<TdSchema>>();
  DatabaseMetaData connectionMetadata=getConnectionMetadata(connection);
  final List<String> catalogNames=new ArrayList<String>();
  fillListOfCatalogs(connection,catalogNames);
  boolean odbcMssqlFlag=ConnectionUtils.isOdbcMssql(connection);
  boolean postgresqlFlag=ConnectionUtils.isPostgresql(connection);
  ResultSet schemas=null;
  try {
    schemas=connectionMetadata.getSchemas();
  }
 catch (  Exception e1) {
    log.warn("String_Node_Str",e1);
  }
  try {
    if (schemas != null) {
      final int columnCount=schemas.getMetaData() == null ? 0 : schemas.getMetaData().getColumnCount();
      boolean hasSchema=false;
      while (schemas.next()) {
        String catName=null;
        String schemaName=null;
        if (columnCount > 1) {
          try {
            catName=schemas.getString(MetaDataConstants.TABLE_CATALOG.name());
          }
 catch (          Exception e) {
            log.warn("String_Node_Str",e);
          }
          try {
            schemaName=schemas.getString(MetaDataConstants.TABLE_SCHEM.name());
          }
 catch (          Exception e) {
            log.warn(e,e);
          }
          if (schemaName == null) {
            try {
              schemaName=schemas.getString(DatabaseConstant.ODBC_ORACLE_SCHEMA_NAME);
            }
 catch (            Exception e) {
              log.warn(e,e);
            }
          }
          if (schemaName == null) {
            try {
              schemaName=schemas.getString(1);
            }
 catch (            Exception e) {
              log.warn(e,e);
            }
          }
          if (catName != null) {
            createSchema(schemaName,catName,catalogName2schemas);
          }
        }
        if (odbcMssqlFlag || postgresqlFlag) {
          try {
            schemaName=schemas.getString(1);
          }
 catch (          Exception e) {
            log.warn(e,e);
          }
        }
        if (catName == null) {
          if (catalogNames.isEmpty()) {
            if (schemaName == null) {
              schemaName=schemas.getString(MetaDataConstants.TABLE_SCHEM.name());
            }
            createSchema(schemaName,null,catalogName2schemas);
          }
 else {
            for (            String catalogName : catalogNames) {
              createSchema(schemaName,catalogName,catalogName2schemas);
            }
          }
        }
        hasSchema=true;
      }
      if (!hasSchema && catalogName2schemas.isEmpty()) {
        TdSchema schema=createSchema("String_Node_Str");
        MultiMapHelper.addUniqueObjectToListMap(null,schema,catalogName2schemas);
      }
    }
  }
 catch (  SQLException e) {
    throw e;
  }
 finally {
    if (schemas != null) {
      schemas.close();
    }
  }
  return catalogName2schemas;
}

Fixed version (ground truth):
/** 
 * Method "getSchemas" returns a map of catalogs to schemas. Warning: if no catalog is found, catalog name (i.e. key of the map) can be null.
 * @param connection the connection
 * @return a map [catalog's name -> list of Schemas ].
 * @throws SQLException
 */
public static Map<String,List<TdSchema>> getSchemas(Connection connection) throws SQLException {
  Map<String,List<TdSchema>> catalogName2schemas=new HashMap<String,List<TdSchema>>();
  DatabaseMetaData connectionMetadata=getConnectionMetadata(connection);
  final List<String> catalogNames=new ArrayList<String>();
  fillListOfCatalogs(connection,catalogNames);
  boolean odbcMssqlFlag=ConnectionUtils.isOdbcMssql(connection);
  boolean postgresqlFlag=ConnectionUtils.isPostgresql(connection);
  ResultSet schemas=null;
  try {
    schemas=connectionMetadata.getSchemas();
  }
 catch (  Exception e1) {
    log.warn("String_Node_Str",e1);
  }
  try {
    if (schemas != null) {
      final int columnCount=schemas.getMetaData() == null ? 0 : schemas.getMetaData().getColumnCount();
      boolean hasSchema=false;
      while (schemas.next()) {
        String catName=null;
        String schemaName=null;
        if (columnCount > 1) {
          try {
            catName=schemas.getString(MetaDataConstants.TABLE_CATALOG.name());
          }
 catch (          Exception e) {
            log.warn("String_Node_Str",e);
          }
          try {
            schemaName=schemas.getString(MetaDataConstants.TABLE_SCHEM.name());
          }
 catch (          Exception e) {
            log.warn(e,e);
          }
          if (schemaName == null) {
            try {
              schemaName=schemas.getString(DatabaseConstant.ODBC_ORACLE_SCHEMA_NAME);
            }
 catch (            Exception e) {
              log.warn(e,e);
            }
          }
          if (schemaName == null) {
            try {
              schemaName=schemas.getString(1);
            }
 catch (            Exception e) {
              log.warn(e,e);
            }
          }
          if (catName != null) {
            createSchema(schemaName,catName,catalogName2schemas);
          }
        }
        if (odbcMssqlFlag || postgresqlFlag) {
          try {
            schemaName=schemas.getString(1);
          }
 catch (          Exception e) {
            log.warn(e,e);
          }
        }
        if (catName == null) {
          if (catalogNames.isEmpty()) {
            if (schemaName == null) {
              schemaName=getSchemaName(schemas);
            }
            createSchema(schemaName,null,catalogName2schemas);
          }
 else {
            for (            String catalogName : catalogNames) {
              createSchema(schemaName,catalogName,catalogName2schemas);
            }
          }
        }
        hasSchema=true;
      }
      if (!hasSchema && catalogName2schemas.isEmpty()) {
        TdSchema schema=createSchema("String_Node_Str");
        MultiMapHelper.addUniqueObjectToListMap(null,schema,catalogName2schemas);
      }
    }
  }
 catch (  SQLException e) {
    throw e;
  }
 finally {
    if (schemas != null) {
      schemas.close();
    }
  }
  return catalogName2schemas;
}