refactor: code hardening

This commit is contained in:
2026-05-27 00:43:26 +02:00
parent 1d261bb693
commit b3198429e2
4 changed files with 141 additions and 43 deletions
+31 -3
View File
@@ -6,6 +6,10 @@
#include "tg_types.h" #include "tg_types.h"
json_object *json_builder(tgbot_option_s *options, size_t optionslen) { json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
if (!options && optionslen > 0) {
return NULL;
}
json_object *rjson = json_object_new_object(); json_object *rjson = json_object_new_object();
if (!rjson) { if (!rjson) {
return NULL; return NULL;
@@ -14,6 +18,9 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
for (size_t i = 0; i < optionslen; ++i) { for (size_t i = 0; i < optionslen; ++i) {
switch (options[i].type) { switch (options[i].type) {
case tgbot_opt_int: { case tgbot_opt_int: {
if (!options[i].value) {
break;
}
json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value))); json_object_object_add(rjson, options[i].key, json_object_new_int(*((int32_t *)options[i].value)));
break; break;
} }
@@ -25,12 +32,19 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
break; break;
} }
case tgbot_opt_int64: { case tgbot_opt_int64: {
if (!options[i].value) {
break;
}
json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value))); json_object_object_add(rjson, options[i].key, json_object_new_int64(*((int64_t *)options[i].value)));
break; break;
} }
case tgbot_opt_inlinekeyboard: { case tgbot_opt_inlinekeyboard: {
if (options[i].value != NULL) { if (options[i].value != NULL) {
json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value); json_object *reply_markup = json_ikb_new((tgbot_inlinekeyboard_s *)options[i].value);
if (!reply_markup) {
json_object_put(rjson);
return NULL;
}
json_object_object_add(rjson, options[i].key, reply_markup); json_object_object_add(rjson, options[i].key, reply_markup);
} }
break; break;
@@ -45,6 +59,10 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
} }
json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) { json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
if (!keyboard) {
return NULL;
}
json_object *reply_markup = json_object_new_object(); json_object *reply_markup = json_object_new_object();
if (!reply_markup) { if (!reply_markup) {
return NULL; return NULL;
@@ -58,9 +76,15 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
for (size_t i = 0; i < keyboard->rows; ++i) { for (size_t i = 0; i < keyboard->rows; ++i) {
json_object *row = json_object_new_array(); json_object *row = json_object_new_array();
if (!row) {
json_object_put(inline_keyboard_array);
json_object_put(reply_markup);
return NULL;
}
for (size_t j = 0; j < keyboard->columns; ++j) { for (size_t j = 0; j < keyboard->columns; ++j) {
tgbot_inlinekeyboardbutton_s *kbbutton = tgbot_inlinekb_button_at(keyboard, i, j); tgbot_inlinekeyboardbutton_s *kbbutton = tgbot_inlinekb_button_at(keyboard, i, j);
if (strcmp(kbbutton->text, "") == 0) { if (!kbbutton || kbbutton->text[0] == '\0') {
continue; continue;
} }
@@ -70,8 +94,12 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
} }
json_object_object_add(button, "text", json_object_new_string(kbbutton->text)); json_object_object_add(button, "text", json_object_new_string(kbbutton->text));
json_object_object_add(button, "url", json_object_new_string(kbbutton->url)); if (kbbutton->url[0] != '\0') {
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data)); json_object_object_add(button, "url", json_object_new_string(kbbutton->url));
}
if (kbbutton->callback_data[0] != '\0') {
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
}
json_object_array_add(row, button); json_object_array_add(row, button);
} }
+62 -29
View File
@@ -2,41 +2,56 @@
#include "internal/common.h" #include "internal/common.h"
int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) { int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *result) {
if (!bot || !update || !result) {
return -1;
}
json_object *update_id = json_object_object_get(result, "update_id"); json_object *update_id = json_object_object_get(result, "update_id");
if (!update_id) {
return -1;
}
bot->offset = json_object_get_int(update_id) + 1; bot->offset = json_object_get_int(update_id) + 1;
update->update_id = json_object_get_int(update_id); update->update_id = json_object_get_int(update_id);
json_object *message = json_object_object_get(result, "message"); json_object *message = json_object_object_get(result, "message");
if (!message) {
return -1;
}
json_object *message_id = json_object_object_get(message, "message_id"); json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) { if (message_id) {
update->message_id = json_object_get_int(message_id); update->message_id = json_object_get_int64(message_id);
} }
json_object *chat = json_object_object_get(message, "chat"); json_object *chat = json_object_object_get(message, "chat");
json_object *chat_id = json_object_object_get(chat, "id"); if (chat) {
if (chat_id) { json_object *chat_id = json_object_object_get(chat, "id");
update->chat_id = json_object_get_int64(chat_id); if (chat_id) {
} update->chat_id = json_object_get_int64(chat_id);
}
json_object *chat_first_name = json_object_object_get(chat, "first_name"); json_object *chat_first_name = json_object_object_get(chat, "first_name");
if (chat_first_name) { if (chat_first_name) {
snprintf(update->chat_first_name, sizeof(update->chat_first_name), "%s", snprintf(update->chat_first_name, sizeof(update->chat_first_name), "%s",
json_object_get_string(chat_first_name)); json_object_get_string(chat_first_name));
} }
json_object *chat_last_name = json_object_object_get(chat, "last_name"); json_object *chat_last_name = json_object_object_get(chat, "last_name");
if (chat_last_name) { if (chat_last_name) {
snprintf(update->chat_last_name, sizeof(update->chat_last_name), "%s", json_object_get_string(chat_last_name)); snprintf(update->chat_last_name, sizeof(update->chat_last_name), "%s",
} json_object_get_string(chat_last_name));
}
json_object *chat_username = json_object_object_get(chat, "username"); json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username != NULL) { if (chat_username) {
snprintf(update->chat_username, sizeof(update->chat_username), "%s", json_object_get_string(chat_username)); snprintf(update->chat_username, sizeof(update->chat_username), "%s", json_object_get_string(chat_username));
} }
json_object *chat_type = json_object_object_get(chat, "type"); json_object *chat_type = json_object_object_get(chat, "type");
if (chat_type) { if (chat_type) {
snprintf(update->chat_type, sizeof(update->chat_type), "%s", json_object_get_string(chat_type)); snprintf(update->chat_type, sizeof(update->chat_type), "%s", json_object_get_string(chat_type));
}
} }
json_object *date = json_object_object_get(message, "date"); json_object *date = json_object_object_get(message, "date");
@@ -53,26 +68,44 @@ int tgbot_parse_message(tgbot_s *bot, tgbot_update_s *update, json_object *resul
} }
int tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) { int tgbot_parse_cbquery(tgbot_s *bot, tgbot_cbquery_s *query, json_object *result, Callback cbq_handler) {
if (!bot || !query || !result || !cbq_handler) {
return -1;
}
json_object *update_id = json_object_object_get(result, "update_id"); json_object *update_id = json_object_object_get(result, "update_id");
if (!update_id) {
return -1;
}
bot->offset = json_object_get_int(update_id) + 1; bot->offset = json_object_get_int(update_id) + 1;
query->update_id = json_object_get_int(update_id); query->update_id = json_object_get_int(update_id);
json_object *callback_query = json_object_object_get(result, "callback_query"); json_object *callback_query = json_object_object_get(result, "callback_query");
if (!callback_query) {
return -1;
}
json_object *message = json_object_object_get(callback_query, "message"); json_object *message = json_object_object_get(callback_query, "message");
if (!message) {
return -1;
}
json_object *message_id = json_object_object_get(message, "message_id"); json_object *message_id = json_object_object_get(message, "message_id");
if (message_id) { if (message_id) {
query->message_id = json_object_get_int(message_id); query->message_id = json_object_get_int64(message_id);
} }
json_object *chat = json_object_object_get(message, "chat"); json_object *chat = json_object_object_get(message, "chat");
json_object *chat_id = json_object_object_get(chat, "id"); if (chat) {
if (chat_id) { json_object *chat_id = json_object_object_get(chat, "id");
query->chat_id = json_object_get_int64(chat_id); if (chat_id) {
} query->chat_id = json_object_get_int64(chat_id);
}
json_object *chat_username = json_object_object_get(chat, "username"); json_object *chat_username = json_object_object_get(chat, "username");
if (chat_username) { if (chat_username) {
snprintf(query->chat_username, sizeof(query->chat_username), "%s", json_object_get_string(chat_username)); snprintf(query->chat_username, sizeof(query->chat_username), "%s", json_object_get_string(chat_username));
}
} }
json_object *date = json_object_object_get(message, "date"); json_object *date = json_object_object_get(message, "date");
+26 -3
View File
@@ -8,21 +8,44 @@
#include "tgbot.h" #include "tgbot.h"
tgbot_s *tgbot_new(const char *token) { tgbot_s *tgbot_new(const char *token) {
if (!token || token[0] == '\0') {
return NULL;
}
tgbot_s *bot = malloc(sizeof(tgbot_s)); tgbot_s *bot = malloc(sizeof(tgbot_s));
if (!bot) { if (!bot) {
return NULL; return NULL;
} }
curl_global_init(CURL_GLOBAL_DEFAULT); if (curl_global_init(CURL_GLOBAL_DEFAULT) != CURLE_OK) {
free(bot);
return NULL;
}
int chars = snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token);
if (chars < 0 || (size_t)chars >= sizeof(bot->api)) {
curl_global_cleanup();
free(bot);
return NULL;
}
chars = snprintf(bot->token, sizeof(bot->token), "%s", token);
if (chars < 0 || (size_t)chars >= sizeof(bot->token)) {
curl_global_cleanup();
free(bot);
return NULL;
}
snprintf(bot->api, sizeof(bot->api), "https://api.telegram.org/bot%s/", token);
snprintf(bot->token, sizeof(bot->token), "%s", token);
bot->offset = 0; bot->offset = 0;
return bot; return bot;
} }
void tgbot_free(tgbot_s *bot) { void tgbot_free(tgbot_s *bot) {
if (!bot) {
return;
}
curl_global_cleanup(); curl_global_cleanup();
free(bot); free(bot);
} }
+22 -8
View File
@@ -1,35 +1,46 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns) { tgbot_inlinekeyboard_s *tgbot_inlinekb_new(size_t rows, size_t columns) {
tgbot_inlinekeyboard_s *keyboard = (tgbot_inlinekeyboard_s *)malloc(sizeof(tgbot_inlinekeyboard_s)); if (rows == 0 || columns == 0 || rows > SIZE_MAX / columns) {
return NULL;
}
tgbot_inlinekeyboard_s *keyboard = malloc(sizeof(tgbot_inlinekeyboard_s));
if (!keyboard) { if (!keyboard) {
return NULL; return NULL;
} }
keyboard->rows = rows; keyboard->rows = rows;
keyboard->columns = columns; keyboard->columns = columns;
keyboard->buttons = (tgbot_inlinekeyboardbutton_s *)malloc(rows * columns * sizeof(tgbot_inlinekeyboardbutton_s));
size_t count = rows * columns;
keyboard->buttons = malloc(count * sizeof(tgbot_inlinekeyboardbutton_s));
if (!keyboard->buttons) { if (!keyboard->buttons) {
free(keyboard); free(keyboard);
return NULL; return NULL;
} }
memset(keyboard->buttons, 0, rows * columns * sizeof(tgbot_inlinekeyboardbutton_s)); memset(keyboard->buttons, 0, count * sizeof(tgbot_inlinekeyboardbutton_s));
return keyboard; return keyboard;
} }
void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard) { void tgbot_inlinekb_free(tgbot_inlinekeyboard_s *keyboard) {
if (!keyboard) {
return;
}
free(keyboard->buttons); free(keyboard->buttons);
free(keyboard); free(keyboard);
} }
tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column) { tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column) {
if (row >= keyboard->rows || column >= keyboard->columns) { if (!keyboard || row >= keyboard->rows || column >= keyboard->columns) {
return NULL; return NULL;
} }
@@ -39,10 +50,13 @@ tgbot_inlinekeyboardbutton_s *tgbot_inlinekb_button_at(tgbot_inlinekeyboard_s *k
int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text, int tgbot_inlinekb_button(tgbot_inlinekeyboard_s *keyboard, size_t row, size_t column, const char *text,
const char *url, const char *callback_data) { const char *url, const char *callback_data) {
tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column); tgbot_inlinekeyboardbutton_s *button = tgbot_inlinekb_button_at(keyboard, row, column);
if (!button) {
return -1;
}
strncpy(button->text, text, sizeof(button->text) - 1); snprintf(button->text, sizeof(button->text), "%s", text ? text : "");
strncpy(button->url, url, sizeof(button->url) - 1); snprintf(button->url, sizeof(button->url), "%s", url ? url : "");
strncpy(button->callback_data, callback_data, sizeof(button->callback_data) - 1); snprintf(button->callback_data, sizeof(button->callback_data), "%s", callback_data ? callback_data : "");
return 0; return 0;
} }