| 1 |
/* Copyright (c) 2018, 2026, Oracle and/or its affiliates. |
| 2 |
|
| 3 |
This program is free software; you can redistribute it and/or modify |
| 4 |
it under the terms of the GNU General Public License, version 2.0, |
| 5 |
as published by the Free Software Foundation. |
| 6 |
|
| 7 |
This program is designed to work with certain software (including |
| 8 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 9 |
as designated in a particular file or component or in included license |
| 10 |
documentation. The authors of MySQL hereby grant you an additional |
| 11 |
permission to link the program and your derivative works with the |
| 12 |
separately licensed software that they have either included with |
| 13 |
the program or referenced in the documentation. |
| 14 |
|
| 15 |
This program is distributed in the hope that it will be useful, |
| 16 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
GNU General Public License, version 2.0, for more details. |
| 19 |
|
| 20 |
You should have received a copy of the GNU General Public License |
| 21 |
along with this program; if not, write to the Free Software |
| 22 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 |
|
| 24 |
#include "event_reader.h" |
| 25 |
#include <string> |
| 26 |
#include "mysql_com.h" // net_field_length_ll, net_field_length_size |
| 27 |
|
| 28 |
namespace binary_log { |
| 29 |
|
| 30 |
void Event_reader::set_error(const char *error) { |
| 31 |
BAPI_ASSERT(error != nullptr); |
| 32 |
BAPI_PRINT("debug", ("Event_reader::set_error(%s)", error)); |
| 33 |
m_error = error; |
| 34 |
} |
| 35 |
|
| 36 |
void Event_reader::set_length(unsigned long long length) { |
| 37 |
PRINT_READER_STATUS("Event_reader::set_length"); |
| 38 |
if (length < m_length) { |
| 39 |
BAPI_PRINT("debug", ("Event_reader::set_length(%llu)", length)); |
| 40 |
set_error("Buffer length cannot shrink"); |
| 41 |
} else { |
| 42 |
m_limit = m_length = length; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
void Event_reader::shrink_limit(unsigned long long bytes) { |
| 47 |
PRINT_READER_STATUS("Event_reader::shrink_limit"); |
| 48 |
if (bytes > m_limit || position() > m_limit - bytes) { |
| 49 |
BAPI_PRINT("debug", ("Event_reader::shrink_limit(%llu)", bytes)); |
| 50 |
set_error("Unable to shrink buffer limit"); |
| 51 |
} else |
| 52 |
m_limit = m_limit - bytes; |
| 53 |
} |
| 54 |
|
| 55 |
const char *Event_reader::ptr(unsigned long long length) { |
| 56 |
PRINT_READER_STATUS("Event_reader::ptr"); |
| 57 |
BAPI_PRINT("debug", ("Event_reader::ptr(%llu)", length)); |
| 58 |
if (!can_read(length)) { |
| 59 |
set_error("Cannot point to out of buffer bounds"); |
| 60 |
return nullptr; |
| 61 |
} |
| 62 |
const char *ret_ptr = m_ptr; |
| 63 |
m_ptr = m_ptr + length; |
| 64 |
return ret_ptr; |
| 65 |
} |
| 66 |
|
| 67 |
const char *Event_reader::go_to(unsigned long long position) { |
| 68 |
PRINT_READER_STATUS("Event_reader::go_to"); |
| 69 |
if (position >= m_limit) { |
| 70 |
BAPI_PRINT("debug", ("Event_reader::go_to(%llu)", position)); |
| 71 |
set_error("Cannot point to out of buffer bounds"); |
| 72 |
return nullptr; |
| 73 |
} |
| 74 |
m_ptr = m_buffer + position; |
| 75 |
return m_ptr; |
| 76 |
} |
| 77 |
|
| 78 |
void Event_reader::alloc_and_memcpy(unsigned char **destination, size_t length, |
| 79 |
int flags) { |
| 80 |
PRINT_READER_STATUS("Event_reader::alloc_and_memcpy"); |
| 81 |
if (!can_read(length)) { |
| 82 |
BAPI_PRINT("debug", ("Event_reader::alloc_and_copy(%zu)", length)); |
| 83 |
set_error("Cannot read from out of buffer bounds"); |
| 84 |
return; |
| 85 |
} |
| 86 |
BAPI_ASSERT(*destination == nullptr); |
| 87 |
*destination = static_cast<unsigned char *>(bapi_malloc(length, flags)); |
| 88 |
if (!*destination) { |
| 89 |
BAPI_PRINT("debug", ("Event_reader::alloc_and_copy(%zu)", length)); |
| 90 |
set_error("Out of memory"); |
| 91 |
return; |
| 92 |
} |
| 93 |
::memcpy(*destination, m_ptr, length); |
| 94 |
m_ptr = m_ptr + length; |
| 95 |
} |
| 96 |
|
| 97 |
void Event_reader::alloc_and_strncpy(char **destination, size_t length, |
| 98 |
int flags) { |
| 99 |
PRINT_READER_STATUS("Event_reader::alloc_and_strncpy"); |
| 100 |
if (!can_read(length)) { |
| 101 |
BAPI_PRINT("debug", ("Event_reader::alloc_and_strncpy(%zu)", length)); |
| 102 |
set_error("Cannot read from out of buffer bounds"); |
| 103 |
return; |
| 104 |
} |
| 105 |
BAPI_ASSERT(*destination == nullptr); |
| 106 |
*destination = static_cast<char *>(bapi_malloc(length + 1, flags)); |
| 107 |
if (!*destination) { |
| 108 |
BAPI_PRINT("debug", ("Event_reader::alloc_and_strncpy(%zu)", length)); |
| 109 |
set_error("Out of memory"); |
| 110 |
return; |
| 111 |
} |
| 112 |
if (length > 0) strncpy(*destination, m_ptr, length); |
| 113 |
(*destination)[length] = '\0'; |
| 114 |
m_ptr = m_ptr + length; |
| 115 |
} |
| 116 |
|
| 117 |
void Event_reader::read_str_at_most_255_bytes(const char **destination, |
| 118 |
uint8_t *lenght) { |
| 119 |
PRINT_READER_STATUS("Event_reader::read_str_at_most_255_bytes"); |
| 120 |
if (!can_read(sizeof(uint8_t))) { |
| 121 |
set_error("Cannot read from out of buffer bounds"); |
| 122 |
return; |
| 123 |
} |
| 124 |
::memcpy(lenght, m_ptr, sizeof(uint8_t)); |
| 125 |
m_ptr = m_ptr + sizeof(uint8_t); |
| 126 |
|
| 127 |
BAPI_PRINT("debug", |
| 128 |
("Event_reader::read_str_at_most_255_bytes(%u)", *lenght)); |
| 129 |
if (!can_read(*lenght)) { |
| 130 |
set_error("Cannot read from out of buffer bounds"); |
| 131 |
return; |
| 132 |
} |
| 133 |
*destination = m_ptr; |
| 134 |
m_ptr = m_ptr + *lenght; |
| 135 |
} |
| 136 |
|
| 137 |
uint64_t Event_reader::net_field_length_ll() { |
| 138 |
PRINT_READER_STATUS("Event_reader::net_field_length_ll"); |
| 139 |
if (!can_read(sizeof(uint8_t))) { |
| 140 |
set_error("Cannot read from out of buffer bounds"); |
| 141 |
return 0; |
| 142 |
} |
| 143 |
// It is safe to read the first byte of the transaction_length |
| 144 |
unsigned char *ptr_length; |
| 145 |
ptr_length = reinterpret_cast<unsigned char *>(const_cast<char *>(m_ptr)); |
| 146 |
unsigned int length_size = net_field_length_size(ptr_length); |
| 147 |
BAPI_PRINT("debug", ("Event_reader::read_net_field_length_ll(): " |
| 148 |
"expect to read length with %u byte(s)", |
| 149 |
length_size)); |
| 150 |
if (!can_read(length_size)) { |
| 151 |
set_error("Cannot read from out of buffer bounds"); |
| 152 |
return 0; |
| 153 |
} |
| 154 |
// It is safe to read the full transaction_length from the buffer |
| 155 |
uint64_t value = ::net_field_length_ll(&ptr_length); |
| 156 |
m_ptr = m_ptr + length_size; |
| 157 |
return value; |
| 158 |
} |
| 159 |
|
| 160 |
void Event_reader::read_data_set(uint32_t set_len, |
| 161 |
std::list<const char *> *set) { |
| 162 |
PRINT_READER_STATUS("Event_reader::read_data_set"); |
| 163 |
uint16_t len = 0; |
| 164 |
for (uint32_t i = 0; i < set_len; i++) { |
| 165 |
len = read<uint16_t>(); |
| 166 |
if (m_error) break; |
| 167 |
const char *hash = strndup<const char *>(len); |
| 168 |
if (m_error) break; |
| 169 |
set->push_back(hash); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
void Event_reader::read_data_map(uint32_t map_len, |
| 174 |
std::map<std::string, std::string> *map) { |
| 175 |
PRINT_READER_STATUS("Event_reader::read_data_map"); |
| 176 |
BAPI_ASSERT(map->empty()); |
| 177 |
for (uint32_t i = 0; i < map_len; i++) { |
| 178 |
uint16_t key_len = read<uint16_t>(); |
| 179 |
if (m_error) break; |
| 180 |
if (!can_read(key_len)) { |
| 181 |
set_error("Cannot read from out of buffer bounds"); |
| 182 |
break; |
| 183 |
} |
| 184 |
std::string key(m_ptr, key_len); |
| 185 |
m_ptr += key_len; |
| 186 |
|
| 187 |
uint32_t value_len = read<uint32_t>(); |
| 188 |
if (m_error) break; |
| 189 |
if (!can_read(value_len)) { |
| 190 |
set_error("Cannot read from out of buffer bounds"); |
| 191 |
break; |
| 192 |
} |
| 193 |
std::string value(m_ptr, value_len); |
| 194 |
m_ptr += value_len; |
| 195 |
|
| 196 |
(*map)[key] = value; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void Event_reader::strncpyz(char *destination, size_t max_length, |
| 201 |
size_t dest_length) { |
| 202 |
PRINT_READER_STATUS("Event_reader::strncpyz"); |
| 203 |
if (!can_read(max_length)) { |
| 204 |
BAPI_PRINT("debug", |
| 205 |
("Event_reader::strncpy(%zu, %zu)", max_length, dest_length)); |
| 206 |
set_error("Cannot read from out of buffer bounds"); |
| 207 |
return; |
| 208 |
} |
| 209 |
if (max_length > 0) strncpy(destination, m_ptr, max_length); |
| 210 |
destination[dest_length - 1] = 0; |
| 211 |
m_ptr = m_ptr + strlen(destination) + 1; |
| 212 |
} |
| 213 |
|
| 214 |
void Event_reader::assign(std::vector<uint8_t> *vector, size_t length) { |
| 215 |
PRINT_READER_STATUS("Event_reader::assign"); |
| 216 |
BAPI_ASSERT(vector->empty()); |
| 217 |
if (!can_read(length)) { |
| 218 |
set_error("Cannot read from out of buffer bounds"); |
| 219 |
return; |
| 220 |
} |
| 221 |
try { |
| 222 |
vector->assign(m_ptr, m_ptr + length); |
| 223 |
} catch (const std::bad_alloc &) { |
| 224 |
vector->clear(); |
| 225 |
set_error("std::bad_alloc"); |
| 226 |
} |
| 227 |
BAPI_ASSERT(vector->size() == length); |
| 228 |
m_ptr = m_ptr + length; |
| 229 |
} |
| 230 |
} // end namespace binary_log |