Bug #40262 Binary log information incorrect in backup of no data with binlog turned on
Submitted: 22 Oct 2008 18:22 Modified: 13 Dec 2008 17:33
Reporter: Chuck Bell Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Backup Severity:S3 (Non-critical)
Version:6.0.8, 6.0, 6.0-backup bzr OS:Any
Assigned to: Rafal Somla CPU Architecture:Any

[22 Oct 2008 18:22] Chuck Bell
Description:
If a backup is performed of a database with no data and binlogging is turned on, the backup image is flagged as having binlog information but no binlog information is saved. Thus, on restore the restore crashes trying to process the binlog file name.

How to repeat:
Run this test case:

source include/have_log_bin.inc;
DROP DATABASE IF EXISTS test;
CREATE DATABASE test;
USE test;

--disable_warnings
DROP TABLE IF EXISTS t1;
--enable_warnings

CREATE TABLE t1 (id INT);

INSERT INTO t1 VALUES (1), (2), (3);

DROP TABLE t1;
 
--replace_column 1 #
BACKUP DATABASE test TO 'test-image';
--replace_column 1 #
RESTORE FROM 'test-image';

-----

Observe that the server crashes on restore pointing to invalid memory access in si_logs.h @ line # 191:

inline
void Backup_log::binlog_file(char *file)
{
  if (strlen(file) > 0)   <----here
    m_op_hist.binlog_file= file;
}

The reason is the backup image was flagged using the BSTREAM_FLAG_BINLOG at the start, but during data read, the code for recording the binlog information (validity point) in write_table_data() in data_backup.cc is skipped. Thus, no binlog position or file name is recorded.

When the restore is run, the code to read the binlog position reads 0 for binlog_pos and binlog_file which causes the call to strlen(binlog_file) fails.

Note: Remove the drop from the test above and the server will not crash.

Suggested fix:
Change the code in write_table_data() to record the binlog information regardless of whether data was read or not.
[22 Oct 2008 20:23] Sveta Smirnova
Thank you for the report.

Verified as described.
[22 Oct 2008 20:24] Sveta Smirnova
Backtrace:

081022 23:22:09 [Note] Restore: Starting restore process
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(my_print_stacktrace+0x26)[0x892c12d]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(handle_segfault+0x2f3)[0x82f5b7b]
[0x2ce420]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_ZN6backup6Logger17report_binlog_posERK21st_bstream_binlog_pos+0xaa)[0x89cb90e]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_ZN18Backup_restore_ctx10do_restoreEv+0x424)[0x89ca588]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_Z22execute_backup_commandP3THDP3LEXP6String+0x511)[0x89caacf]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_Z21mysql_execute_commandP3THD+0xc5d)[0x830c075]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_Z11mysql_parseP3THDPKcjPS2_+0x22b)[0x83144ed]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_Z16dispatch_command19enum_server_commandP3THDPcj+0x8d9)[0x8314f51]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(_Z10do_commandP3THD+0x23a)[0x83162b8]
/users/ssmirnova/src/mysql-6.0-backup/sql/mysqld(handle_one_connection+0x11d)[0x8303545]
/lib/libpthread.so.0[0x45fbd4]
/lib/libc.so.6(__clone+0x5e)[0x3b74fe]
[14 Nov 2008 14:50] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/58795

2733 Rafal Somla	2008-11-14
      BUG#40262 (Binary log information incorrect in backup of no data with binlog 
                 turned on)
      
      This is a preliminary patch which fixes a problem in si_log.h which leads to a 
      carash reported in the bug. According to specifications, methods 
      Backup_log::binlog_file() and Backup_log::master_binlog_file() should do nothing 
      if NULL string is passed as the argument. But they crashed instead (due to a 
      call to strlen() on NULL string). This patch fixes this.
[17 Nov 2008 14:44] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/58964

2734 Rafal Somla	2008-11-17
      Bug #40262  - Binary log information incorrect in backup of no data with binlog 
                    turned on
      
      This is the second and last patch which fixes the main problem of reporting VP 
      info also in the case there are no tables to be backed-up. 
      
      It also fixes an issue with incorrect parsing of backup image upon restore, if 
      there are no table data stored in it.
[18 Nov 2008 13:47] Øystein Grøvlen
Both patches are approved to push, given that issue with long line is fixed.
[20 Nov 2008 10:10] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/59338

2734 Rafal Somla	2008-11-20
      Bug #40262  - Binary log information incorrect in backup of no data with binlog 
                    turned on
      
      This is the second and last patch which fixes the main problem of reporting VP 
      info also in the case there are no tables to be backed-up. 
      
      It also fixes an issue with incorrect parsing of backup image upon restore, if 
      there are no table data stored in it.
[25 Nov 2008 14:28] Chuck Bell
Patch approved after excellent commentary and explanation.
[26 Nov 2008 10:44] Rafal Somla
Pushed into team tree 6.0-backup (Wed Nov 26 10:16:27 2008).
[12 Dec 2008 6:25] Bugs System
Pushed into 6.0.9-alpha  (revid:rafal.somla@sun.com-20081120100853-drwfdymurv06atlb) (version source revid:jorgen.loland@sun.com-20081128091358-4518qjv1cnz0f8vb) (pib:5)
[13 Dec 2008 17:33] Paul DuBois
Noted in 6.0.9 changelog.

If BACKUP DATABASE was used to back up an empty database and binary
logging enabled, the backup image was flagged as containing binary
log information even though it did not. Using RESTORE with the backup
image then crashed trying to parse the binary log filename.