Ensure that strings passed to TDLib's log message callback are UTF-8 encoded.

This commit is contained in:
levlam 2022-06-04 14:57:29 +03:00
parent a8e8136f34
commit cfe5c4a43b
3 changed files with 16 additions and 3 deletions

View File

@ -24,6 +24,8 @@
#include "td/utils/port/RwMutex.h" #include "td/utils/port/RwMutex.h"
#include "td/utils/port/thread.h" #include "td/utils/port/thread.h"
#include "td/utils/Slice.h" #include "td/utils/Slice.h"
#include "td/utils/StringBuilder.h"
#include "td/utils/utf8.h"
#include <algorithm> #include <algorithm>
#include <atomic> #include <atomic>
@ -679,7 +681,18 @@ static std::atomic<ClientManager::LogMessageCallbackPtr> log_message_callback;
static void log_message_callback_wrapper(int verbosity_level, CSlice message) { static void log_message_callback_wrapper(int verbosity_level, CSlice message) {
auto callback = log_message_callback.load(std::memory_order_relaxed); auto callback = log_message_callback.load(std::memory_order_relaxed);
if (callback != nullptr) { if (callback != nullptr) {
if (check_utf8(message)) {
callback(verbosity_level, message.c_str()); callback(verbosity_level, message.c_str());
} else {
size_t pos = 0;
while (1 <= message[pos] && message[pos] <= 126) {
pos++;
}
CHECK(pos + 1 < message.size());
auto utf8_message = PSTRING() << message.substr(0, pos)
<< url_encode(message.substr(pos, message.size() - pos - 1)) << '\n';
callback(verbosity_level, utf8_message.c_str());
}
} }
} }

View File

@ -129,7 +129,7 @@ class ClientManager final {
* \param verbosity_level Log verbosity level with which the message was added (-1 - 1024). * \param verbosity_level Log verbosity level with which the message was added (-1 - 1024).
* If 0, then TDLib will crash as soon as the callback returns. * If 0, then TDLib will crash as soon as the callback returns.
* None of the TDLib methods can be called from the callback. * None of the TDLib methods can be called from the callback.
* \param message Null-terminated string with the message added to the log. * \param message Null-terminated UTF-8-encoded string with the message added to the log.
*/ */
using LogMessageCallbackPtr = void (*)(int verbosity_level, const char *message); using LogMessageCallbackPtr = void (*)(int verbosity_level, const char *message);

View File

@ -91,7 +91,7 @@ TDJSON_EXPORT const char *td_execute(const char *request);
* \param verbosity_level Log verbosity level with which the message was added (-1 - 1024). * \param verbosity_level Log verbosity level with which the message was added (-1 - 1024).
* If 0, then TDLib will crash as soon as the callback returns. * If 0, then TDLib will crash as soon as the callback returns.
* None of the TDLib methods can be called from the callback. * None of the TDLib methods can be called from the callback.
* \param message Null-terminated string with the logged message. The string isn't guaranteed to be encoded in UTF-8. * \param message Null-terminated UTF-8-encoded string with the message added to the log.
*/ */
typedef void (*td_log_message_callback_ptr)(int verbosity_level, const char *message); typedef void (*td_log_message_callback_ptr)(int verbosity_level, const char *message);