Skip to content

Fix negative TIME(1)/TIME(2) with a fraction in the binlog decoder - #200

Open
panchenkoai wants to merge 1 commit into
blackbeam:masterfrom
rivet-lab:time2-negative-fraction
Open

panchenkoai wants to merge 1 commit into
blackbeam:masterfrom
rivet-lab:time2-negative-fraction

Conversation

@panchenkoai

Copy link
Copy Markdown

my_time_packed_from_binary (src/binlog/misc.rs) reads the one-byte fraction of a TIME(1)/TIME(2) value as u32 and, for a negative value with a fraction, runs frac -= 0x100. The subtraction underflows: it panics with overflow checks on (debug), and wraps to a duration of about 625 hours with them off (release). The TIME(3)/TIME(4) arm just below already reads the fraction as i32.

Minimal reproduction: a MySQL server with binlog_format=ROW, then

CREATE TABLE t (id INT PRIMARY KEY, t TIME(2));
INSERT INTO t VALUES (1, '-00:00:00.50');

The row event carries the TIME2 bytes 7f ff ff ce (integer part -1, fraction byte 206). Without the change:

my_time_packed_from_binary(&[0x7f, 0xff, 0xff, 0xce][..], 2)
// debug: panicked at src/binlog/misc.rs:38:17: attempt to subtract with overflow
// release (overflow-checks off): Value::Time(false, 26, 0, 63, 63, 277216) after time_from_packed, about 625 hours

With the change it returns -500_000 microseconds, and time_from_packed gives Value::Time(true, 0, 0, 0, 0, 500_000), which is what the server stored.

The change reads the fraction as i32 and adds unit tests for negative TIME(1)/(2) with a fraction (-00:00:00.50, -00:00:01.25), the TIME(3)/(4) arm, and a positive value.

The same line is present in v0.37.3 and v0.38.2.

my_time_packed_from_binary read the one-byte fraction of TIME(1)/TIME(2) as u32 and subtracted 0x100 for a negative value, which underflows: a panic with overflow checks, a wrap to about 625 hours without. The TIME(3)/TIME(4) arm already uses i32.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant