Skip to content

Refactor memory management and string handling in console code#25743

Merged
trufae merged 4 commits intomasterfrom
claude/fix-cons-performance-3flMy
Apr 7, 2026
Merged

Refactor memory management and string handling in console code#25743
trufae merged 4 commits intomasterfrom
claude/fix-cons-performance-3flMy

Conversation

@trufae
Copy link
Copy Markdown
Collaborator

@trufae trufae commented Apr 7, 2026

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:

  • Fix memory leak in colorcode() by freeing res before reassigning buffer
  • Improve JSON formatting to use r_str_append() instead of r_str_newf() for efficiency
  • Replace strdup() with r_str_ndup() to handle buffer lengths correctly (fixes potential issues with non-null-terminated buffers)
  • Remove unnecessary calloc() and strcpy() in zoom handling
  • Optimize grep highlighting logic to only allocate string when needed
  • Fix buffer length calculation in r_cons_grep_line() to use actual length instead of strlen() for non-null-terminated data
  • Defer out buffer allocation until actually needed

libr/cons/rgb.c:

  • Refactor r_cons_rgb_tostring() to use early returns instead of multiple conditional assignments
  • Fix color comparison logic (e.g., g == b && g == 0g == 0 && b == 0)
  • Simplify control flow for better readability

libr/cons/canvas.c:

  • Replace RStrBuf with stack-allocated buffer for small attribute strings
  • Optimize __expandLine() by using memmove() instead of temporary buffer allocation
  • Remove unnecessary strdup() in r_cons_canvas_write()
  • Reorder null checks in r_cons_canvas_tostring() for clarity

libr/cons/dietline.c:

  • Fix buffer length calculation in __delete_current_char() to use actual length instead of strlen()
  • Optimize shift_buffer() to calculate length directly instead of calling strlen()
  • Fix unix_word_rubout() length calculation
  • Replace r_str_pad() with direct printf formatting in history listing
  • Cache option string length in selection_widget_select() to avoid repeated strlen() calls
  • Add missing free(prompt) in __update_prompt_color()

libr/cons/hud.c:

  • Simplify hud_render_prompt() by using pointer arithmetic instead of string duplication

libr/cons/cons.c:

  • Replace strlen() with constant for known string length in r_cons_enable_mouse()
  • Remove unnecessary string duplication in 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

claude added 4 commits April 6, 2026 22:07
- 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
@trufae trufae merged commit 662ef9d into master Apr 7, 2026
37 of 43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants