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"
json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
if (!options && optionslen > 0) {
return NULL;
}
json_object *rjson = json_object_new_object();
if (!rjson) {
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) {
switch (options[i].type) {
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)));
break;
}
@@ -25,12 +32,19 @@ json_object *json_builder(tgbot_option_s *options, size_t optionslen) {
break;
}
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)));
break;
}
case tgbot_opt_inlinekeyboard: {
if (options[i].value != NULL) {
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);
}
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) {
if (!keyboard) {
return NULL;
}
json_object *reply_markup = json_object_new_object();
if (!reply_markup) {
return NULL;
@@ -58,9 +76,15 @@ json_object *json_ikb_new(tgbot_inlinekeyboard_s *keyboard) {
for (size_t i = 0; i < keyboard->rows; ++i) {
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) {
tgbot_inlinekeyboardbutton_s *kbbutton = tgbot_inlinekb_button_at(keyboard, i, j);
if (strcmp(kbbutton->text, "") == 0) {
if (!kbbutton || kbbutton->text[0] == '\0') {
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, "url", json_object_new_string(kbbutton->url));
json_object_object_add(button, "callback_data", json_object_new_string(kbbutton->callback_data));
if (kbbutton->url[0] != '\0') {
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);
}