From 46bef1aaecfaa4f5859359f0be1193c1dc679120 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 14 Aug 2021 11:03:31 -0700 Subject: [PATCH] Switch all malloc() calls in djui to calloc(). Possibly fixes menu bug --- src/pc/djui/djui_base.c | 2 +- src/pc/djui/djui_bind.c | 2 +- src/pc/djui/djui_button.c | 2 +- src/pc/djui/djui_chat_box.c | 2 +- src/pc/djui/djui_chat_message.c | 2 +- src/pc/djui/djui_checkbox.c | 4 ++-- src/pc/djui/djui_flow_layout.c | 2 +- src/pc/djui/djui_image.c | 2 +- src/pc/djui/djui_inputbox.c | 8 +++----- src/pc/djui/djui_interactable.c | 2 +- src/pc/djui/djui_panel.c | 2 +- src/pc/djui/djui_panel_host_message.c | 2 +- src/pc/djui/djui_popup.c | 4 ++-- src/pc/djui/djui_rect.c | 2 +- src/pc/djui/djui_root.c | 2 +- src/pc/djui/djui_selectionbox.c | 9 ++++----- src/pc/djui/djui_slider.c | 2 +- src/pc/djui/djui_text.c | 4 ++-- src/pc/djui/djui_three_panel.c | 2 +- 19 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/pc/djui/djui_base.c b/src/pc/djui/djui_base.c index 92d742e0..c29d790e 100644 --- a/src/pc/djui/djui_base.c +++ b/src/pc/djui/djui_base.c @@ -175,7 +175,7 @@ static void djui_base_add_child(struct DjuiBase* parent, struct DjuiBase* base) if (parent == NULL) { return; } // allocate linked list node - struct DjuiBaseChild* baseChild = malloc(sizeof(struct DjuiBaseChild)); + struct DjuiBaseChild* baseChild = calloc(1, sizeof(struct DjuiBaseChild)); baseChild->base = base; baseChild->next = NULL; diff --git a/src/pc/djui/djui_bind.c b/src/pc/djui/djui_bind.c index 266af2f4..bbbaf006 100644 --- a/src/pc/djui/djui_bind.c +++ b/src/pc/djui/djui_bind.c @@ -48,7 +48,7 @@ static void djui_bind_destroy(struct DjuiBase* base) { } struct DjuiBind* djui_bind_create(struct DjuiBase* parent, const char* message, unsigned int configKey[]) { - struct DjuiBind* bind = malloc(sizeof(struct DjuiBind)); + struct DjuiBind* bind = calloc(1, sizeof(struct DjuiBind)); struct DjuiBase* base = &bind->base; bind->configKey = configKey; diff --git a/src/pc/djui/djui_button.c b/src/pc/djui/djui_button.c index d8414642..aa179038 100644 --- a/src/pc/djui/djui_button.c +++ b/src/pc/djui/djui_button.c @@ -42,7 +42,7 @@ static void djui_button_destroy(struct DjuiBase* base) { } struct DjuiButton* djui_button_create(struct DjuiBase* parent, const char* message) { - struct DjuiButton* button = malloc(sizeof(struct DjuiButton)); + struct DjuiButton* button = calloc(1, sizeof(struct DjuiButton)); struct DjuiBase* base = &button->base; djui_base_init(parent, base, NULL, djui_button_destroy); diff --git a/src/pc/djui/djui_chat_box.c b/src/pc/djui/djui_chat_box.c index 0bdf569d..807d49dd 100644 --- a/src/pc/djui/djui_chat_box.c +++ b/src/pc/djui/djui_chat_box.c @@ -93,7 +93,7 @@ struct DjuiChatBox* djui_chat_box_create(void) { gDjuiChatBox = NULL; } - struct DjuiChatBox* chatBox = malloc(sizeof(struct DjuiChatBox)); + struct DjuiChatBox* chatBox = calloc(1, sizeof(struct DjuiChatBox)); struct DjuiBase* base = &chatBox->base; djui_base_init(&gDjuiRoot->base, base, djui_chat_box_render, djui_chat_box_destroy); diff --git a/src/pc/djui/djui_chat_message.c b/src/pc/djui/djui_chat_message.c index b91b5905..a1697acc 100644 --- a/src/pc/djui/djui_chat_message.c +++ b/src/pc/djui/djui_chat_message.c @@ -53,7 +53,7 @@ struct DjuiChatMessage* djui_chat_message_create_from(u8 globalIndex, char* mess } struct DjuiChatMessage* djui_chat_message_create(char* message) { - struct DjuiChatMessage* chatMessage = malloc(sizeof(struct DjuiChatMessage)); + struct DjuiChatMessage* chatMessage = calloc(1, sizeof(struct DjuiChatMessage)); struct DjuiBase* base = &chatMessage->base; djui_base_init(&gDjuiChatBox->chatFlow->base, base, djui_chat_message_render, djui_chat_message_destroy); djui_base_set_size_type(base, DJUI_SVT_RELATIVE, DJUI_SVT_ABSOLUTE); diff --git a/src/pc/djui/djui_checkbox.c b/src/pc/djui/djui_checkbox.c index bcdce16a..d06631c9 100644 --- a/src/pc/djui/djui_checkbox.c +++ b/src/pc/djui/djui_checkbox.c @@ -50,8 +50,8 @@ static void djui_checkbox_destroy(struct DjuiBase* base) { } struct DjuiCheckbox* djui_checkbox_create(struct DjuiBase* parent, const char* message, bool* value) { - struct DjuiCheckbox* checkbox = malloc(sizeof(struct DjuiCheckbox)); - struct DjuiBase* base = &checkbox->base; + struct DjuiCheckbox* checkbox = calloc(1, sizeof(struct DjuiCheckbox)); + struct DjuiBase* base = &checkbox->base; checkbox->value = value; diff --git a/src/pc/djui/djui_flow_layout.c b/src/pc/djui/djui_flow_layout.c index 3fd7d7f7..4b5660d1 100644 --- a/src/pc/djui/djui_flow_layout.c +++ b/src/pc/djui/djui_flow_layout.c @@ -46,7 +46,7 @@ static void djui_flow_layout_destroy(struct DjuiBase* base) { } struct DjuiFlowLayout* djui_flow_layout_create(struct DjuiBase* parent) { - struct DjuiFlowLayout* layout = malloc(sizeof(struct DjuiFlowLayout)); + struct DjuiFlowLayout* layout = calloc(1, sizeof(struct DjuiFlowLayout)); struct DjuiBase* base = &layout->base; djui_base_init(parent, base, djui_rect_render, djui_flow_layout_destroy); diff --git a/src/pc/djui/djui_image.c b/src/pc/djui/djui_image.c index 28de68c2..2c120a7f 100644 --- a/src/pc/djui/djui_image.c +++ b/src/pc/djui/djui_image.c @@ -50,7 +50,7 @@ static void djui_image_destroy(struct DjuiBase* base) { } struct DjuiImage* djui_image_create(struct DjuiBase* parent, const u8* texture, u16 textureWidth, u16 textureHeight, u16 textureBitSize) { - struct DjuiImage* image = malloc(sizeof(struct DjuiImage)); + struct DjuiImage* image = calloc(1, sizeof(struct DjuiImage)); struct DjuiBase* base = &image->base; djui_base_init(parent, base, djui_image_render, djui_image_destroy); diff --git a/src/pc/djui/djui_inputbox.c b/src/pc/djui/djui_inputbox.c index 48ea257f..855a271c 100644 --- a/src/pc/djui/djui_inputbox.c +++ b/src/pc/djui/djui_inputbox.c @@ -348,7 +348,7 @@ static void djui_inputbox_on_text_input(struct DjuiBase *base, char* text) { } // back up current message - char* sMsg = malloc(sizeof(char) * (inputbox->bufferSize)); + char* sMsg = calloc(inputbox->bufferSize, sizeof(char)); memcpy(sMsg, msg, inputbox->bufferSize); // insert text @@ -534,12 +534,10 @@ static void djui_inputbox_destroy(struct DjuiBase* base) { } struct DjuiInputbox* djui_inputbox_create(struct DjuiBase* parent, u16 bufferSize) { - struct DjuiInputbox* inputbox = malloc(sizeof(struct DjuiInputbox)); + struct DjuiInputbox* inputbox = calloc(1, sizeof(struct DjuiInputbox)); struct DjuiBase* base = &inputbox->base; - memset(inputbox, 0, sizeof(struct DjuiInputbox)); inputbox->bufferSize = bufferSize; - inputbox->buffer = malloc(sizeof(char) * bufferSize); - memset(inputbox->buffer, 0, sizeof(char) * bufferSize); + inputbox->buffer = calloc(bufferSize, sizeof(char)); djui_base_init(parent, base, djui_inputbox_render, djui_inputbox_destroy); djui_base_set_size(base, 200, 32); diff --git a/src/pc/djui/djui_interactable.c b/src/pc/djui/djui_interactable.c index 6f737137..346ffa52 100644 --- a/src/pc/djui/djui_interactable.c +++ b/src/pc/djui/djui_interactable.c @@ -430,7 +430,7 @@ void djui_interactable_create(struct DjuiBase* base) { free(base->interactable); } - struct DjuiInteractable* interactable = malloc(sizeof(struct DjuiInteractable)); + struct DjuiInteractable* interactable = calloc(1, sizeof(struct DjuiInteractable)); memset(interactable, 0, sizeof(struct DjuiInteractable)); base->interactable = interactable; } diff --git a/src/pc/djui/djui_panel.c b/src/pc/djui/djui_panel.c index 475c5983..c91c9ac0 100644 --- a/src/pc/djui/djui_panel.c +++ b/src/pc/djui/djui_panel.c @@ -34,7 +34,7 @@ void djui_panel_add(struct DjuiBase* caller, struct DjuiBase* panelBase, struct } // allocate panel - struct DjuiPanel* panel = malloc(sizeof(struct DjuiPanel)); + struct DjuiPanel* panel = calloc(1, sizeof(struct DjuiPanel)); panel->parent = sPanelList; panel->base = panelBase; panel->defaultElementBase = defaultElementBase; diff --git a/src/pc/djui/djui_panel_host_message.c b/src/pc/djui/djui_panel_host_message.c index ae1ebc10..1dc6d78f 100644 --- a/src/pc/djui/djui_panel_host_message.c +++ b/src/pc/djui/djui_panel_host_message.c @@ -58,7 +58,7 @@ void djui_panel_host_message_create(struct DjuiBase* caller) { #endif { warningLines = 5; - warningMessage = malloc(sizeof(char) * 256); + warningMessage = calloc(256, sizeof(char)); sprintf(warningMessage, sWarningSocket, configHostPort); } diff --git a/src/pc/djui/djui_popup.c b/src/pc/djui/djui_popup.c index d0d98ad9..d1d10554 100644 --- a/src/pc/djui/djui_popup.c +++ b/src/pc/djui/djui_popup.c @@ -16,7 +16,7 @@ static struct DjuiPopupList* sPopupListHead = NULL; static f32 sPopupListY = 0; static void djui_popup_add_to_list(struct DjuiPopup* popup) { - struct DjuiPopupList* node = malloc(sizeof(struct DjuiPopupList)); + struct DjuiPopupList* node = calloc(1, sizeof(struct DjuiPopupList)); node->popup = popup; node->createTime = clock_elapsed(); node->next = sPopupListHead; @@ -34,7 +34,7 @@ static void djui_popup_destroy(struct DjuiBase* base) { } struct DjuiPopup* djui_popup_create(const char* message, int lines) { - struct DjuiPopup* popup = malloc(sizeof(struct DjuiPopup)); + struct DjuiPopup* popup = calloc(1, sizeof(struct DjuiPopup)); struct DjuiBase* base = &popup->base; f32 height = lines * 32 + 32; diff --git a/src/pc/djui/djui_rect.c b/src/pc/djui/djui_rect.c index 42acb484..33592f49 100644 --- a/src/pc/djui/djui_rect.c +++ b/src/pc/djui/djui_rect.c @@ -33,7 +33,7 @@ static void djui_rect_destroy(struct DjuiBase* base) { } struct DjuiRect* djui_rect_create(struct DjuiBase* parent) { - struct DjuiRect* rect = malloc(sizeof(struct DjuiRect)); + struct DjuiRect* rect = calloc(1, sizeof(struct DjuiRect)); struct DjuiBase* base = &rect->base; djui_base_init(parent, base, djui_rect_render, djui_rect_destroy); diff --git a/src/pc/djui/djui_root.c b/src/pc/djui/djui_root.c index 7c28e972..57bfd241 100644 --- a/src/pc/djui/djui_root.c +++ b/src/pc/djui/djui_root.c @@ -23,7 +23,7 @@ static void djui_root_destroy(struct DjuiBase* base) { } struct DjuiRoot* djui_root_create(void) { - struct DjuiRoot* root = malloc(sizeof(struct DjuiRoot)); + struct DjuiRoot* root = calloc(1, sizeof(struct DjuiRoot)); struct DjuiBase* base = &root->base; djui_base_init(NULL, base, djui_root_render, djui_root_destroy); diff --git a/src/pc/djui/djui_selectionbox.c b/src/pc/djui/djui_selectionbox.c index a33ba3a1..250de64d 100644 --- a/src/pc/djui/djui_selectionbox.c +++ b/src/pc/djui/djui_selectionbox.c @@ -73,15 +73,14 @@ static void djui_selectionbox_destroy(struct DjuiBase* base) { } struct DjuiSelectionbox* djui_selectionbox_create(struct DjuiBase* parent, const char* message, char* choices[], u8 choiceCount, unsigned int* value) { - struct DjuiSelectionbox* selectionbox = malloc(sizeof(struct DjuiSelectionbox)); - struct DjuiBase* base = &selectionbox->base; + struct DjuiSelectionbox* selectionbox = calloc(1, sizeof(struct DjuiSelectionbox)); + struct DjuiBase* base = &selectionbox->base; selectionbox->value = value; - selectionbox->choices = malloc(sizeof(char*) * choiceCount); + selectionbox->choices = calloc(choiceCount, sizeof(char*)); for (int i = 0; i < choiceCount; i++) { u32 length = strlen(choices[i]); - selectionbox->choices[i] = malloc(sizeof(char) * (length + 1)); - memset(selectionbox->choices[i], 0, sizeof(char) * (length + 1)); + selectionbox->choices[i] = calloc((length + 1), sizeof(char)); sprintf(selectionbox->choices[i], "%s", choices[i]); } selectionbox->choiceCount = choiceCount; diff --git a/src/pc/djui/djui_slider.c b/src/pc/djui/djui_slider.c index 425599d6..eef05420 100644 --- a/src/pc/djui/djui_slider.c +++ b/src/pc/djui/djui_slider.c @@ -114,7 +114,7 @@ static void djui_slider_destroy(struct DjuiBase* base) { } struct DjuiSlider* djui_slider_create(struct DjuiBase* parent, const char* message, unsigned int* value, unsigned int min, unsigned int max) { - struct DjuiSlider* slider = malloc(sizeof(struct DjuiSlider)); + struct DjuiSlider* slider = calloc(1, sizeof(struct DjuiSlider)); struct DjuiBase* base = &slider->base; slider->value = value; diff --git a/src/pc/djui/djui_text.c b/src/pc/djui/djui_text.c index 9880de9d..072b8dc7 100644 --- a/src/pc/djui/djui_text.c +++ b/src/pc/djui/djui_text.c @@ -19,7 +19,7 @@ void djui_text_set_text(struct DjuiText* text, const char* message) { // allocate and set new message u16 messageLen = strlen(message); - text->message = malloc(sizeof(char) * (messageLen + 1)); + text->message = calloc((messageLen + 1), sizeof(char)); memcpy(text->message, message, sizeof(char) * (messageLen + 1)); } @@ -380,7 +380,7 @@ static void djui_text_destroy(struct DjuiBase* base) { } struct DjuiText* djui_text_create(struct DjuiBase* parent, const char* message) { - struct DjuiText* text = malloc(sizeof(struct DjuiText)); + struct DjuiText* text = calloc(1, sizeof(struct DjuiText)); struct DjuiBase* base = &text->base; djui_base_init(parent, base, djui_text_render, djui_text_destroy); diff --git a/src/pc/djui/djui_three_panel.c b/src/pc/djui/djui_three_panel.c index 51186f8b..0200a823 100644 --- a/src/pc/djui/djui_three_panel.c +++ b/src/pc/djui/djui_three_panel.c @@ -142,7 +142,7 @@ static void djui_three_panel_destroy(struct DjuiBase* base) { } struct DjuiThreePanel* djui_three_panel_create(struct DjuiBase* parent, f32 minHeaderSize, f32 bodySize, f32 minFooterSize) { - struct DjuiThreePanel* threePanel = malloc(sizeof(struct DjuiThreePanel)); + struct DjuiThreePanel* threePanel = calloc(1, sizeof(struct DjuiThreePanel)); struct DjuiBase* base = &threePanel->base; djui_base_init(parent, base, djui_three_panel_render, djui_three_panel_destroy);