-
Notifications
You must be signed in to change notification settings - Fork 3
Add object info like printf #186
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
Changes from all commits
cb8756c
17658a2
45409cc
61d2150
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
|
|
||
| #include <limits.h> | ||
| #include <math.h> | ||
| #include <stdarg.h> | ||
| #include <stdbool.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
|
|
@@ -271,10 +272,11 @@ t_unit_config *core_get_unitConfig(t_unit_type type); | |
| // ----- DEBUG FUNCTIONS ----- | ||
|
|
||
| /// @brief Add debug information to an object for visualization | ||
| /// @details This function accumulates debug strings for objects. Multiple calls per tick will append the strings (with newlines). | ||
| /// @details This function accumulates debug strings for objects. Multiple calls per tick will append the strings. | ||
| /// @param obj The object to attach debug info to | ||
| /// @param info The debug string to add | ||
| void core_debug_addObjectInfo(const t_obj *obj, const char *info); | ||
| /// @param format printf-style format debug string to add | ||
| /// @param ... Format arguments matching the format string | ||
| void core_debug_addObjectInfo(const t_obj *obj, const char *format, ...); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In entry->info = realloc(entry->info, old_len + new_len + 1);
strcat(entry->info, msg); // UB / crash if realloc returned NULL
free(msg);If 🐛 Proposed fix- entry->info = realloc(entry->info, old_len + new_len + 1);
- strcat(entry->info, msg);
- free(msg);
+ char *tmp = realloc(entry->info, old_len + new_len + 1);
+ if (!tmp)
+ {
+ free(msg);
+ return;
+ }
+ entry->info = tmp;
+ strcat(entry->info, msg);
+ free(msg);🤖 Prompt for AI Agents |
||
|
|
||
| /// @brief Add a step to the end of the debug path of an object for visualization | ||
| /// @param unit The unit to attach the debug path step to | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -31,23 +31,51 @@ static t_debug_entry *core_static_findOrCreateEntry(unsigned long object_id) | |||||||||||||||||||||||||||||||||||||
| return entry; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| void core_debug_addObjectInfo(const t_obj *obj, const char *info) | ||||||||||||||||||||||||||||||||||||||
| void core_debug_addObjectInfo(const t_obj *obj, const char *format, ...) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| if (!obj || !info) return; | ||||||||||||||||||||||||||||||||||||||
| if (!obj || !format) return; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| t_debug_entry *entry = core_static_findOrCreateEntry(obj->id); | ||||||||||||||||||||||||||||||||||||||
| if (!entry) return; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // format the new info string | ||||||||||||||||||||||||||||||||||||||
| va_list ap; | ||||||||||||||||||||||||||||||||||||||
| va_start(ap, format); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| va_list ap2; | ||||||||||||||||||||||||||||||||||||||
| va_copy(ap2, ap); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| int needed = vsnprintf(NULL, 0, format, ap); | ||||||||||||||||||||||||||||||||||||||
| va_end(ap); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| if (needed < 0) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| va_end(ap2); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| char *msg = (char *)malloc((size_t)needed + 1); | ||||||||||||||||||||||||||||||||||||||
| if (!msg) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| va_end(ap2); | ||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| vsnprintf(msg, (size_t)needed + 1, format, ap2); | ||||||||||||||||||||||||||||||||||||||
| va_end(ap2); | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| // append to debug info | ||||||||||||||||||||||||||||||||||||||
| if (entry->info == NULL) | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| // First info for this object | ||||||||||||||||||||||||||||||||||||||
| entry->info = strdup(info); | ||||||||||||||||||||||||||||||||||||||
| entry->info = msg; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| size_t old_len = strlen(entry->info); | ||||||||||||||||||||||||||||||||||||||
| size_t new_len = strlen(info); | ||||||||||||||||||||||||||||||||||||||
| size_t new_len = strlen(msg); | ||||||||||||||||||||||||||||||||||||||
| entry->info = realloc(entry->info, old_len + new_len + 1); | ||||||||||||||||||||||||||||||||||||||
| strcat(entry->info, info); | ||||||||||||||||||||||||||||||||||||||
| strcat(entry->info, msg); | ||||||||||||||||||||||||||||||||||||||
| free(msg); | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
74
to
+78
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If 🐛 Proposed fix size_t old_len = strlen(entry->info);
- size_t new_len = strlen(msg);
- entry->info = realloc(entry->info, old_len + new_len + 1);
- strcat(entry->info, msg);
- free(msg);
+ size_t new_len = (size_t)needed;
+ char *tmp = realloc(entry->info, old_len + new_len + 1);
+ if (!tmp)
+ {
+ free(msg);
+ return;
+ }
+ entry->info = tmp;
+ memcpy(entry->info + old_len, msg, new_len + 1);
+ free(msg);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@details"(with newlines)" is misleadingThe implementation in
debug.cconcatenates messages with no automatic newline between them — callers must embed\nin their format string (as in the PR example). The parenthetical "(with newlines)" suggests the function does it automatically, which could mislead future users.📝 Suggested clarification
📝 Committable suggestion
🤖 Prompt for AI Agents