Skip to content

rz-find: fix string truncation in -Z mode - #6782

Open
kisnner26 wants to merge 1 commit into
rizinorg:devfrom
kisnner26:fix/rz-find-string-truncation
Open

kisnner26 wants to merge 1 commit into
rizinorg:devfrom
kisnner26:fix/rz-find-string-truncation

Conversation

@kisnner26

Copy link
Copy Markdown

Closes #6289

Problem

rz-find -Z truncates any string longer than 127 bytes because the output
buffer is a fixed char _str[128] on the stack. Long mangled C++ symbols
(e.g. STL/Android NDK demangled names) get cut off mid-string with no
indication of truncation, and the three near-duplicate extraction loops in
hit() (plain string, wide string, non-showstr) had diverging bounds
checks — the wide-string path in particular indexed ro->buf[i] against
i < sizeof(_str) instead of the actual search buffer size, which is an
out-of-bounds read when a hit lands close to the end of ro->buf.

Fix

  • Added extract_string_unified(): a single bounds-checked extraction
    routine (dest cap, source cap, wide-mode flag, optional display cap)
    that replaces the three duplicated loops.
  • hit() now allocates str with malloc() sized to the actual bytes
    available from delta to the end of the search buffer, instead of a
    fixed 128-byte stack buffer, and frees it through a single cleanup:
    label on every return path.

Testing

  • Compiled clean, no warnings.
  • Verified against the binary from rz-find truncates strings during the -Z mode #6289: rz-find -Z output now matches
    izz byte-for-byte for the long mangled symbol that used to get cut at
    127 chars.
  • Added a regression test in test/db/tools/rz_find using
    test/bins/elf/0pack, asserting the full untruncated string.
  • rz-test passes (pre-existing unrelated failures on this machine are
    due to a missing system magic DB and a dev-build version string, not
    this change).

- Replace the hardcoded 128-byte buffer in `hit()` with a
  dynamically sized one based on `ro->bsize - delta`, so long
  strings are no longer silently cut off.
- Extract the duplicated ascii/wide string-copy loops into a
  single static helper, `extract_string_unified()`.
- Fix a memory leak: the early returns for `exec_command` and
  `rizin_command` now go through a shared `cleanup:` label so
  the buffer is always freed.

Closes rizinorg#6289
@kisnner26

Copy link
Copy Markdown
Author

For context: I had a previous attempt at this issue (#6761) that used AI-generated code and was correctly closed for that. This PR is a rewrite from scratch — I worked through the buffer-sizing math, the leak fix via goto cleanup, and the wide-string byte-order edge case myself. I used an AI assistant for review only (pointing out bugs I'd introduced along the way, and fuzz-testing my extraction function against the original code), not to generate the implementation. Happy to walk through any part of it in review.

@Rot127 Rot127 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you fill out the PR template?

  • Please, comments should be in English. They also seem to explain unnecessary basic things.
  • The code doesn't seem to handle utf-8 strings if I see correctly?

@wargio wargio left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments are not needed, and they are in spanish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rz-find truncates strings during the -Z mode

3 participants