fix(mysql): return error instead of panic on truncated OK packet#4176
fix(mysql): return error instead of panic on truncated OK packet#4176abonander merged 2 commits intolaunchbadge:mainfrom
Conversation
d0ca316 to
0ffef15
Compare
| let affected_rows = buf.get_uint_lenenc(); | ||
| let last_insert_id = buf.get_uint_lenenc(); |
There was a problem hiding this comment.
I don't want to move the goalposts too much, but it seems like these fields should at least be covered by the length check too.
In the long run we should probably ban use of bytes::Buf methods and instead write our own that return Result.
There was a problem hiding this comment.
Checking get_uint_lenenc calls is a bit trickier since the method advances a variable number of bytes depending on the prefix. I've changed its signature to return Result and added remaining-bytes checks inside the method, propagating with ? at all call sites.
If this approach is good, I could follow up with a separate PR that wraps the other panicking bytes::Buf methods in the same way.
There was a problem hiding this comment.
I was mostly thinking that we should just check that the packet is nonempty, but this is a great improvement, thanks!
If this approach is good, I could follow up with a separate PR that wraps the other panicking
bytes::Bufmethods in the same way.
I think that would be a good idea, there's a lot of shortcuts that we took early on that are coming back to bite us and using things like this that panic instead of returning errors is a big one.
53b3be5 to
33b6165
Compare
Change get_uint_lenenc to return Result and validate buffer has sufficient bytes before each read
33b6165 to
af6fb80
Compare
Does your PR solve an issue?
fixes #4139
Is this a breaking change?
No. This changes a panic into a returned
Err(Protocol(...)), which callers already handle. The connection pool will gracefully discard the bad connection instead of crashing the tokio worker thread.