Refactor memory management and string handling in console code#25743
Merged
Refactor memory management and string handling in console code#25743
Conversation
- canvas: Remove redundant strdup in r_cons_canvas_write (r_str_ansi_resetbg already copies) - canvas: Replace malloc+memcpy+free with memmove in __expandLine - canvas: Use stack buffer instead of RStrBuf for small ANSI attrs in set_attr - canvas: Check olen before allocating in r_cons_canvas_tostring - grep: Remove dead code (unused sin buffer allocation in zoom handler) - grep: Fix memory leak in colorcode() (res was never freed) - grep: Defer out buffer allocation in r_cons_grep_line until needed - grep: Avoid r_str_ndup when grep_highlight is off (use r_strbuf_append_n) - grep: Use r_str_ndup with known length instead of strdup for buffers - grep: Use r_str_append instead of r_str_newf for simple newline append - cons: Remove unnecessary r_str_ndup copy in pager ".." path - cons: Use constant length for mouse escape sequence strings - hud: Remove unnecessary strdup in hud_render_prompt - rgb: Use early returns in r_cons_rgb_tostring instead of chain of ifs https://claude.ai/code/session_01RoQJa7ykF4SPBVStcxtJXX
…locs - dietline: Fix memory leak in __update_prompt_color (r_str_escape result never freed) - dietline: Replace strlen with O(1) arithmetic after memmove in shift_buffer - dietline: Replace strlen with O(1) arithmetic in __delete_current_char memmove size - dietline: Replace strlen with O(1) arithmetic after memmove in unix_word_rubout - dietline: Cache strlen result in selection_widget_select (was called twice) - dietline: Use printf width specifier for padding instead of heap alloc per entry - grep: Replace strlen(p) with O(1) pointer arithmetic in grep end-anchor check https://claude.ai/code/session_01RoQJa7ykF4SPBVStcxtJXX
The single strdup at the end with a shared pointer produces a smaller function than multiple return strdup() call sites. https://claude.ai/code/session_01RoQJa7ykF4SPBVStcxtJXX
The strlen on a string literal is resolved at compile time, and is more readable than a magic constant 18. https://claude.ai/code/session_01RoQJa7ykF4SPBVStcxtJXX
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR refactors memory management and string handling across multiple console modules to improve code quality, fix potential issues, and optimize performance:
Key Changes
libr/cons/grep.c:
colorcode()by freeingresbefore reassigning bufferr_str_append()instead ofr_str_newf()for efficiencystrdup()withr_str_ndup()to handle buffer lengths correctly (fixes potential issues with non-null-terminated buffers)calloc()andstrcpy()in zoom handlingr_cons_grep_line()to use actual length instead ofstrlen()for non-null-terminated dataoutbuffer allocation until actually neededlibr/cons/rgb.c:
r_cons_rgb_tostring()to use early returns instead of multiple conditional assignmentsg == b && g == 0→g == 0 && b == 0)libr/cons/canvas.c:
RStrBufwith stack-allocated buffer for small attribute strings__expandLine()by usingmemmove()instead of temporary buffer allocationstrdup()inr_cons_canvas_write()r_cons_canvas_tostring()for claritylibr/cons/dietline.c:
__delete_current_char()to use actual length instead ofstrlen()shift_buffer()to calculate length directly instead of callingstrlen()unix_word_rubout()length calculationr_str_pad()with direct printf formatting in history listingselection_widget_select()to avoid repeatedstrlen()callsfree(prompt)in__update_prompt_color()libr/cons/hud.c:
hud_render_prompt()by using pointer arithmetic instead of string duplicationlibr/cons/cons.c:
strlen()with constant for known string length inr_cons_enable_mouse()r_cons_flush()Benefits
Eliminates memory leaks and improves resource management
Fixes handling of non-null-terminated buffers by using length-aware functions
Reduces unnecessary allocations and string copies
Improves code clarity and maintainability
Better performance through optimized string operations
Mark this if you consider it ready to merge
I've added tests (optional)
I wrote some lines in the book (optional)
https://claude.ai/code/session_01RoQJa7ykF4SPBVStcxtJXX