• Sujatha's avatar
    MDEV-18970: uninited var can be read in gtid_delete_pending() · cacdcfd0
    Sujatha authored
    Problem:
    ========
    gcc 8 -O2 seems to indicate a real error for this code:
    
    direct_pos= table->file->ha_table_flags() & HA_PRIMARY_KEY_REQUIRED_FOR_POSITION;
    
    the warning: /mariadb/10.4/sql/rpl_gtid.cc:980:7:
    warning: 'direct_pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
    
    Analysis:
    =========
    'direct_pos' is a variable which holds 'table_flags'. If this flag is set it means
    that a record within a table can be directly located by using its position. If
    this flag is set to '0' means there is no direct access is available, hence
    index scan must be initiated to locate the record.  This direct_pos is used to
    locate a row within mysql.gtid_slave_pos table for deletion.
    
    Prior to the initialization of 'direct_pos' following steps take place.
    
    1. mysql.gtid_slave_pos table is opened and 'table_opened' flag is set to true.
    2. State check for mysql.gtid_slave_pos table is initiated.
    
    If there is a failure during step2 code will be redirected to the error handling
    part. This error handling code will access uninitialized value of 'direct_pos'.
    This results in above mentioned warning.
    
    Another issue found during analysis is the error handling code uses '!direct_pos'
    to identify if the index is initialized or not. This is incorrect.
    
    The index initialization code is shown below.
    
      if (!direct_pos && (err= table->file->ha_index_init(0, 0)))
        {
          table->file->print_error(err, MYF(0));
          goto end;
        }
    
    In case there is a failure during ha_index_init code will be redirected to end
    part which tries to close the uninitialized index. It will result in an assert
    
    10.4/sql/handler.h:3186: int handler::ha_index_end(): Assertion `inited==INDEX'
    failed.
    
    Fix:
    ===
    Introduce a new variable named 'index_inited'. Set this variable upon successful
    initialization of index initialization otherwise by default it is false. Use
    this variable during error handling.
    cacdcfd0
rpl_gtid.cc 78.8 KB