Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve memory management and string handling in ItemInput class #256

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions src/ItemInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,19 +266,18 @@ class ItemInput : public MenuItem {
*/
void typeChar(MenuRenderer* renderer, const unsigned char character) {
uint8_t length = strlen(value);
char* buf = new char[length + 2];
if (cursor < length) {
char start[length];
char end[length];
char* joined = new char[length + 2];
substring(value, 0, cursor, start);
substring(value, cursor, length - cursor, end);
concat(start, character, end, joined);
value = joined;
concat(start, character, end, buf);
} else {
char* buf = new char[length + 2];
concat(value, character, buf);
value = buf;
}
delete[] value;
value = buf;
forntoh marked this conversation as resolved.
Show resolved Hide resolved
cursor++;
uint8_t viewSize = getViewSize(renderer);
if (cursor > (view + viewSize - 1)) {
Expand All @@ -293,7 +292,7 @@ class ItemInput : public MenuItem {
* @brief Clear the value of the input field
*/
void clear(MenuRenderer* renderer) {
value = (char*)"";
value[0] = '\0';
forntoh marked this conversation as resolved.
Show resolved Hide resolved
draw(renderer);
renderer->drawBlinker();
// Log
Expand Down
Loading